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!
im having trouble with the labels connection, when i drag down toward the file owner it only come up with View, and that it there is no player score or computers score, i dont know why this has happened, it works with the images but it wont work with the labels and even the texts
In response to ash’s question, i had the same problem but by saving the iTennisViewController.h file when you’ve added the code, then entering Interface Builder it works just fine.
Tom’s answer may have worked, but are you dragging from the label to the file’s owner, or the file’s owner to the label? The only way it works it to drag the file owner’s outlet to the label.
1. this is supposed to be for a tutorial- he/she has gone to the trouble of making it for you- how much more can you ask for???
2. bluetoothing is incredibly hard
Im having trouble Connecting “New Referencing Outlet” and “File’s Owner”, which is displayed in the video. This is needed to connect the racquets, scores and ball. Obviously this is one of the most vital parts in this application, though I cant do it.
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UIViewController _loadViewFromNibNamed:bundle:] loaded the “iTennisViewController” nib but the view outlet was not set.’
The chances are you followed the tutorial but havn’t set the view connection itself.
If you look through your connections, all your major objects should be connected, but i found with mine that I was missing the “view” connection at the bottom. You want to drag this one to the “view” box in your iTennisViewController.xib window.
Once this is connected, save, and rebuild and run your project!
You da’ man!.. Amazing walk-through Xcode.. As a first time app developer I was pretty intimidated..(still kind of am actually), but this tutorial definitely opened my eyes… Props! and I could help notice the inconsiderate moron that tried to sell your exact game to apple..lol.. hope you have him on his knees begging for mercy.
Question, I’m trying to change the game from portrait to landscape, I’ve already rotated my images and switched interface builder to landscape, but what do I need to alter in the code? P.S. when I simulate it, it is in portrait with the court squeezed into landscape. Thanks!
Andy, for adjusting from portrait to landscape (to be used in your tennisViewController.m and SplashViewController.m files):
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Hello,
I am new to xcode and c programming and iphone programming. I have sucessfully “built” this tutorial but it says that it “failed to launch simulated application: iPhone Simulator failed to install the application.” I have it set to iPhone Simulator 3.1 and have tried 3.0 as well.
I completed this first tutorial. The program builds and loads into the iPhone simulator without problem. It briefly shows the load screen, then displays the tennis court which I built according to the video. However, nothing happens when I click to continue! I might be wrong, but it would seem that, based on my understanding of the code, that it should start the ball moving.
Should it?
Or do I have to do step 2 before anything actually happens?
I’m reluctant to continue before I know that I have completed these steps properly. Thanks!
Hey Brandon, great tutorial, very helpful and easy to understand. I have an idea for an iPhone game, sports training related. I know you probably get this a lot, lol, but I do not want to go into too much detail on a blog, but if you could take a moment and check out my website, listed above, you will see the type of training I do, it may give you a hint as to what I am looking to develop. Please feel free to email me at my email address listed above and I can go into more detail.
Hey, I finally got all the coding to be working and debugged!
But now when I build and run it, all the simulator shows me is the court pic. I’ve tried messing around in the interface builder but nothing seems to let me see any other object.
Is there some sort of arrangement order that I need to setup?
If anyone could help that would be awesome!
I’d like to start off by saying AWESOME tutorial(s) I’ve been finding out more things I can do via the apple developer program now.
One problem though:
I’m attempting to reproduce a game like this with a few more complications but am running into an interface error. The code appears to be correct (producing no errors according to Xcode 3.2.2. When the app starts up in simulator, it loads the default.png image without a problem and then the splash.png image. At first there were some fading errors but I managed to fix those when I upgraded Xcode from 3.1.3 to 3.2.2. In any case it still fades the splash screen to a solid white display. I’ve attempted to find help via apple’s technical support, ect. and have no success. Has ANYONE here ever had this problem and if so how did you find a solution?
I have had the exact same problem. The fade only works for the default or splash image but for no other image. I even photoshopped an image to get the the same pixel dimensions as the court.png and still no luck
I’d like to start off by saying AWESOME tutorial(s) I’ve been finding out more things I can do via the apple developer program now.
One problem though:
I’m attempting to reproduce a game like this with a few more complications but am running into an interface error. The code appears to be correct (producing no errors according to Xcode 3.2.2. When the app starts up in simulator, it loads the default.png image without a problem and then the splash.png image. At first there were some fading errors but I managed to fix those when I upgraded Xcode from 3.1.3 to 3.2.2. In any case it still fades the splash screen to a solid white display. I’ve attempted to find help via apple’s technical support, ect. and have no success. Has ANYONE here ever had this problem and if so how did you find a solution?
If you have an answer you can contact me via my business site at CrawfordWorks.ca
Thank you in advanced !
Hi. I’m trying to add the court.png file to the imageview– the first step of the video. however, when I try to click the dropdown menu to get court.png, none of my pictures are there.
It’s like my iTennisViewController.xib isn’t linked to the resource images that I imported. Is there some sort of linking step that I’m missing? I can’t type it manually either because I just get a giant blue question mark.
Anybody else having problems with the video part of the tutiorial (building the interface) buffering? I cant seem to view the movie due to it only buffering a few seconds.
Are there any other ways to view the movie?
Just starting this tutorial and would be a real shame to not be able to continue.
Hi I write the code perfectly but i hate this error:
Code Sign error: The identity ‘iPhone Developer’ doesn’t match any valid certificate/private key pair in the default keychain
Is it real that i need to get the paid version?
or i can correct that?
Followed the tutorial and all I see is the court.png no ball or racquets. I’ve seen others with the same problem and they said they fixed it but how did they do it?
Just to let everybody who is having the problem when they link to the File’s Owner, you must select the object e.g the racquet, then drag the line across.
However, when I do build and go, I get 18 errors, I deleted most of the stuff where the errors were coming from but I think they were the most important bits, but I wanted to try build and go again, but I got one error saying:
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
Open the interface builder. In the window with 3 icons (File’s owner, First Responder, View), click on the View icon.
Go to the View Connections tab and make a new connection to the File’s Owner.
Open the interface builder. In the window with 3 icons (File’s owner, First Responder, View), click on the View icon.
Go to the View Connections tab and make a new connection to the File’s Owner.
hi
suddenly i am facing problems with my iphons games that is wheneevr i click on the game icon for a second it opens a game but automatically it gets close so please help me with it
Hi, great tutorial! I want to know one thing for future reference, though: what (if any) is the difference between UIImage and UIImageView? I know this is a simple question, but I would greatly appreciate an answer.
Sorry to double post, but there is a problem with mine:
when I run it, the ball moves even though it is paused and clicking the screen on the simulator does not get rid of the label. Any help would be very appreciated.
Thanks a lot for this article! You might want to add: For people who do not own a Mac, you can install Mac OS Snow Leopard in a virtual environment with VirtualBox and get this to work. Google “mac os snow leopard virtualbox”.
Brandon
Can you please help. I have got to the first stage of your tutorial where you add the court.png image but no files appear in the drop down menu. On looking at previous questions I see that others have had this problem but I have tried saving and even doing the whole thing from scratch again but I just cannot get this image to go to view. Any suggestions as to what I am doing wrong? Regards Neil
i got a problem with the interface built, cause the video you provide cut off when add UILabel “taptobegin”. The rest of the video is missing. Please tell me what the rest contain.
^^
I have completed the tutorial and build and run successfully.But my problem is when I click on Tap To Begin Label,the ball starts moving but can’t stop it once it started moving and the racquets are not moving also the player’s and computers score’s are still the same as 0. Can you please help me to solve this problem or we need to write code for this to work properly ?
Hey! wasnt sure of you where answering questions via email, but i had to try…
i finished the tutorial and when i attempt to simulate the game it immediately exits back to the menu.. this is my first game so maybe its a setting?
231 Comments
im having trouble with the labels connection, when i drag down toward the file owner it only come up with View, and that it there is no player score or computers score, i dont know why this has happened, it works with the images but it wont work with the labels and even the texts
please get back to me a soon as possible
In response to ash’s question, i had the same problem but by saving the iTennisViewController.h file when you’ve added the code, then entering Interface Builder it works just fine.
Tom’s answer may have worked, but are you dragging from the label to the file’s owner, or the file’s owner to the label? The only way it works it to drag the file owner’s outlet to the label.
Also, try right clicking on the file owner and dragging to the label.
Thanks very helpful !
Hi,
how can i modify the app to play against your friends through bluetooth?
Tommy
1. this is supposed to be for a tutorial- he/she has gone to the trouble of making it for you- how much more can you ask for???
2. bluetoothing is incredibly hard
Hey guys,
Im having trouble Connecting “New Referencing Outlet” and “File’s Owner”, which is displayed in the video. This is needed to connect the racquets, scores and ball. Obviously this is one of the most vital parts in this application, though I cant do it.
All help will be greatly appreciated!
Try right clicking on the file owner. If you did the stuff in the iTennisViewController.h, then you should be able to see them there.
don’t use the New Referencing Outlet
In answer to a lot of the problems with
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[UIViewController _loadViewFromNibNamed:bundle:] loaded the “iTennisViewController” nib but the view outlet was not set.’
The chances are you followed the tutorial but havn’t set the view connection itself.
If you look through your connections, all your major objects should be connected, but i found with mine that I was missing the “view” connection at the bottom. You want to drag this one to the “view” box in your iTennisViewController.xib window.
Once this is connected, save, and rebuild and run your project!
Hey Brandon,
You da’ man!.. Amazing walk-through Xcode.. As a first time app developer I was pretty intimidated..(still kind of am actually), but this tutorial definitely opened my eyes… Props! and I could help notice the inconsiderate moron that tried to sell your exact game to apple..lol.. hope you have him on his knees begging for mercy.
N.
Great writeup!
Question, I’m trying to change the game from portrait to landscape, I’ve already rotated my images and switched interface builder to landscape, but what do I need to alter in the code? P.S. when I simulate it, it is in portrait with the court squeezed into landscape. Thanks!
Andy, for adjusting from portrait to landscape (to be used in your tennisViewController.m and SplashViewController.m files):
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
press control, and then a side arrow key in the simulator. That rotates it.
Hi Brandon,
how would you limit the width that the paddles could move ?
so the ball could squeeze between the paddle and the side of the screen ?
thanks
Chimpy
Greta site man! Keep the tutorials coming!
Hello,
I am new to xcode and c programming and iphone programming. I have sucessfully “built” this tutorial but it says that it “failed to launch simulated application: iPhone Simulator failed to install the application.” I have it set to iPhone Simulator 3.1 and have tried 3.0 as well.
@Brandon quicktime movie wont play to show the setting up of the UI!
Hii,
Can u please send me the whole code on my E-mail address that i can study n learn more thing.
Hi Narender,
I will Try to do this And its working. can you please send me the racquet_yellow code….
on my email add please.
Hello,
Why do we call :
self.gameState = kGameStatePaused;
ballVelocity = CGPointMake(kBallSpeedX,kBallSpeedY);
et not : (missing “self”)
self.gameState = kGameStatePaused;
self.ballVelocity = CGPointMake(kBallSpeedX,kBallSpeedY);
then here (missing self)
if(gameState == kGameStatePaused) {…
end not
if(self.gameState == kGameStatePaused) {….
Thank you.
I completed this first tutorial. The program builds and loads into the iPhone simulator without problem. It briefly shows the load screen, then displays the tennis court which I built according to the video. However, nothing happens when I click to continue! I might be wrong, but it would seem that, based on my understanding of the code, that it should start the ball moving.
Should it?
Or do I have to do step 2 before anything actually happens?
I’m reluctant to continue before I know that I have completed these steps properly. Thanks!
Well it was a – instead of an = that was making it fail… works now
Great tutorial.
I have finished part 1, but the tennis ball is not moving?
Any ideas?
Thanks
I have the problem where only my tennis court shows up. Anyone know what I’ve done wrong? Thanks in advance for any tips!
Hey Brandon, great tutorial, very helpful and easy to understand. I have an idea for an iPhone game, sports training related. I know you probably get this a lot, lol, but I do not want to go into too much detail on a blog, but if you could take a moment and check out my website, listed above, you will see the type of training I do, it may give you a hint as to what I am looking to develop. Please feel free to email me at my email address listed above and I can go into more detail.
Thanks
Rick
i downloaded the source code but the points don’t count up and the racquet wont move?
thanks
Hey, I finally got all the coding to be working and debugged!
But now when I build and run it, all the simulator shows me is the court pic. I’ve tried messing around in the interface builder but nothing seems to let me see any other object.
Is there some sort of arrangement order that I need to setup?
If anyone could help that would be awesome!
Thank you.
Hey Guys,
I’d like to start off by saying AWESOME tutorial(s) I’ve been finding out more things I can do via the apple developer program now.
One problem though:
I’m attempting to reproduce a game like this with a few more complications but am running into an interface error. The code appears to be correct (producing no errors according to Xcode 3.2.2. When the app starts up in simulator, it loads the default.png image without a problem and then the splash.png image. At first there were some fading errors but I managed to fix those when I upgraded Xcode from 3.1.3 to 3.2.2. In any case it still fades the splash screen to a solid white display. I’ve attempted to find help via apple’s technical support, ect. and have no success. Has ANYONE here ever had this problem and if so how did you find a solution?
I have had the exact same problem. The fade only works for the default or splash image but for no other image. I even photoshopped an image to get the the same pixel dimensions as the court.png and still no luck
Hey Guys,
I’d like to start off by saying AWESOME tutorial(s) I’ve been finding out more things I can do via the apple developer program now.
One problem though:
I’m attempting to reproduce a game like this with a few more complications but am running into an interface error. The code appears to be correct (producing no errors according to Xcode 3.2.2. When the app starts up in simulator, it loads the default.png image without a problem and then the splash.png image. At first there were some fading errors but I managed to fix those when I upgraded Xcode from 3.1.3 to 3.2.2. In any case it still fades the splash screen to a solid white display. I’ve attempted to find help via apple’s technical support, ect. and have no success. Has ANYONE here ever had this problem and if so how did you find a solution?
If you have an answer you can contact me via my business site at CrawfordWorks.ca
Thank you in advanced !
Hi. I’m trying to add the court.png file to the imageview– the first step of the video. however, when I try to click the dropdown menu to get court.png, none of my pictures are there.
It’s like my iTennisViewController.xib isn’t linked to the resource images that I imported. Is there some sort of linking step that I’m missing? I can’t type it manually either because I just get a giant blue question mark.
Thanks!
Have you tried saving everything, including your xCode files?
This is great, you are the best of the best
thanks alot
Anybody else having problems with the video part of the tutiorial (building the interface) buffering? I cant seem to view the movie due to it only buffering a few seconds.
Are there any other ways to view the movie?
Just starting this tutorial and would be a real shame to not be able to continue.
Please reply, Thanks.
@Simon88
Thanks a lot for your tip! You save my night
@author
very very well work
grazie anche dall’Italia
Never mind. My Quicktime is playing up on my mac so loaded it up on my PC instead and hey presto its worked.
This doesnt make PC’s better than Macs in anyway though lol.
Hi I write the code perfectly but i hate this error:
Code Sign error: The identity ‘iPhone Developer’ doesn’t match any valid certificate/private key pair in the default keychain
Is it real that i need to get the paid version?
or i can correct that?
Followed the tutorial and all I see is the court.png no ball or racquets. I’ve seen others with the same problem and they said they fixed it but how did they do it?
Just to let everybody who is having the problem when they link to the File’s Owner, you must select the object e.g the racquet, then drag the line across.
Hi
Easy to understand tutorial, well done!
However, when I do build and go, I get 18 errors, I deleted most of the stuff where the errors were coming from but I think they were the most important bits, but I wanted to try build and go again, but I got one error saying:
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
Any ideas what I might be doing wrong?
J.F
@those where only the court shows up.
I had the same problem and solved it like this.
Open the interface builder. In the window with 3 icons (File’s owner, First Responder, View), click on the View icon.
Go to the View Connections tab and make a new connection to the File’s Owner.
@those where only the court shows up.
I had the same problem and solved it like this.
Open the interface builder. In the window with 3 icons (File’s owner, First Responder, View), click on the View icon.
Go to the View Connections tab and make a new connection to the File’s Owner.
hi
suddenly i am facing problems with my iphons games that is wheneevr i click on the game icon for a second it opens a game but automatically it gets close so please help me with it
So nice tutorial , I can run this game on iphone simulator successfully. Thank you very much
hey where’s the part 2 of the tutorial ???
X-(
PS. part 1 was really great!
Hi, great tutorial! I want to know one thing for future reference, though: what (if any) is the difference between UIImage and UIImageView? I know this is a simple question, but I would greatly appreciate an answer.
your a douche
andy thats not nice
Sorry to double post, but there is a problem with mine:
when I run it, the ball moves even though it is paused and clicking the screen on the simulator does not get rid of the label. Any help would be very appreciated.
how do you put this on the iphone/itouch
cool andy that is so cool it smells like chocolate and limes at the same time
Thanks a lot for this article! You might want to add: For people who do not own a Mac, you can install Mac OS Snow Leopard in a virtual environment with VirtualBox and get this to work. Google “mac os snow leopard virtualbox”.
Hey, any1 got the links to the other parts of this? Would be greatly appreciated
Hey, any1 got the links to the other parts of this? Would be greatly appreciated
Brandon
Can you please help. I have got to the first stage of your tutorial where you add the court.png image but no files appear in the drop down menu. On looking at previous questions I see that others have had this problem but I have tried saving and even doing the whole thing from scratch again but I just cannot get this image to go to view. Any suggestions as to what I am doing wrong? Regards Neil
i got a problem with the interface built, cause the video you provide cut off when add UILabel “taptobegin”. The rest of the video is missing. Please tell me what the rest contain.
^^
Good stuff, but where is part two ?
10x but the video file for the outlets connections is missing!!
Excellent and unselfish tutorial! Great Stuff!
Love this cant wait to see more
I have completed the tutorial and build and run successfully.But my problem is when I click on Tap To Begin Label,the ball starts moving but can’t stop it once it started moving and the racquets are not moving also the player’s and computers score’s are still the same as 0. Can you please help me to solve this problem or we need to write code for this to work properly ?
Hey man great tutorial, but I got all these errors saying kballspeed undefined, or gameloop undefined, how can i fix that?
thanks
Hey! wasnt sure of you where answering questions via email, but i had to try…
i finished the tutorial and when i attempt to simulate the game it immediately exits back to the menu.. this is my first game so maybe its a setting?
@Bob,
You may have made the same mistake as me:
In the viewDidLoad function I had:
selector:@selector(gameloop)
instead of:
selector:@selector(gameLoop)
(lowercase ‘l’ instead of uppercase ‘L’.
Changing to the correct case sorted the problem.
Cheers,
Martin.
41 Trackbacks