Now that you have made the connections, close Interface Builder and return to XCode. We need to set up some constants. You can hardcode these values in somewhere, but making them constants makes them easier to change when you are tweaking the mechanics of your game. Open up iTennisViewController.m and add the following code.
Let me explain the purpose of the constants. The first 2 are game states. We use these to determine what is to be shown on screen. Is the game paused, is it running, is it at the title screen? We will add more states later on in the game. The next 2 variables are speeds for the ball to travel. Since the game is 2D, the ball can have an X and Y speed to make up its velocity vector. The next line synthesizes the variables we will use to create their getter and setter methods automatically.
Now let’s write the viewDidLoad method. Add the following code:
First, we are setting our game state to a paused game state. This is so the game doesn’t start right away when the view loads. Next, we create a vector for the ball’s velocity. Adjust the constants that we defined earlier for a faster or slower ball speed. Finally, we create an NSTimer instance. The first parameter is the timestep. We have set the time step to 0.05 seconds. Adjusting this will change the speed of the entire game. Next we set the target. This is telling the timer where the callback is located. The callback is the next parameter. Basically, we are telling the timer to call self.gameLoop every 0.05 seconds. We don’t need to worry about the userInfo param. The last parameter just tells the timer to repeat.
Now that we have initialized our game, let’s create the game loop. Add the following method:
First, we are checking to see if the game is in a running state (we don’t want to move the ball in a paused state). If the game is not running, we will show the tapToBegin label. This is just the label defined earlier that tells the user to tap the screen to begin.
If the game is running, we start by moving the ball according to its velocity vector. The next few lines do some bounds checking. If the ball hits the side of the screen, we want to reverse its velocity so that it “bounces”. Without this code, the ball would fly off the screen.
The last bit of code we will need to write is the touchesBegan method. We will use this method to start the game when the game is in a paused state. Add the following code:
What this does is first checks to see if we are in a paused state. If so, we hide the tapToBegin label and set the game in a running state. This will get the game running. Eventually, we will write the code to control the player paddle in this function.
Finally, being good iPhone developers, we need to clean up the resources that we allocated. Add the following code to the dealloc method.
That concludes part 1 of our game development tutorial series. If you have any questions or comments, feel free to leave them in the comments section of this post. You can download the source code here. Happy iCoding!
It is an informative tutorial. Thanks for sharing code. It will help the beginner for creating an iPhone game. You explain every step in very simple way.
I have read through your tutorial and it is very well done! I have one question. I have done everything the same as the tutorial suggests and when I click on the app on the simulator it loads fine and I click the button to start the game and then the ball moves but the racquets don’t please help! Thanks best regards Ollie
I have gone through this tutorial maybe 2 or 3 times now even with copy paste to make sure I did it right looked at it front and back and when it loads on the Ipod Touch its gets to the iTennis loading png and it seems to hang there is there anything that I am doing wrong that I can not seem to catch?
hello, great tutorial here… but i just cant seem to get the connections inspector to work, i closely followed your steps and after i had clicked on the connections inspector under the tools menu and when i went to drag the referencing outlet into the files owner, all that comes up is “view”
I searches so many pages for iPhone Programming but can’t find satisfied answer. when i come across your page my problem is solve and i found my solution from this tutorial. Thanks a lot for supply this amazing information.
Hey,
Im 12 and I have a bit of a problem: when I run this app on the simulator, it stays on the loading page and in the Debugger Console it says GDB: Stopped at breakpoint 13 (hit count: 1)-’line 9′
Please help me this is my first app!
Great tutorial by the way.
Thanks!
227 Comments
It is an informative tutorial. Thanks for sharing code. It will help the beginner for creating an iPhone game. You explain every step in very simple way.
Great Information and post! It is very informative and suggestible for the user of solar May I think it can be beneficial in coming days…
This post is different from what I read on most blog. And it have so many valuable things to learn.
Hi there
I have read through your tutorial and it is very well done! I have one question. I have done everything the same as the tutorial suggests and when I click on the app on the simulator it loads fine and I click the button to start the game and then the ball moves but the racquets don’t please help! Thanks best regards Ollie
I have gone through this tutorial maybe 2 or 3 times now even with copy paste to make sure I did it right looked at it front and back and when it loads on the Ipod Touch its gets to the iTennis loading png and it seems to hang there is there anything that I am doing wrong that I can not seem to catch?
hello, great tutorial here… but i just cant seem to get the connections inspector to work, i closely followed your steps and after i had clicked on the connections inspector under the tools menu and when i went to drag the referencing outlet into the files owner, all that comes up is “view”
I searches so many pages for iPhone Programming but can’t find satisfied answer. when i come across your page my problem is solve and i found my solution from this tutorial. Thanks a lot for supply this amazing information.
Hey,
Im 12 and I have a bit of a problem: when I run this app on the simulator, it stays on the loading page and in the Debugger Console it says GDB: Stopped at breakpoint 13 (hit count: 1)-’line 9′
Please help me this is my first app!
Great tutorial by the way.
Thanks!
40 Trackbacks