The Ultimate Guide to Iphone 2D Game Development

This document is designed to help those who are beginners and trying to get their foot out the door for the first time, since we've done it, we will try to make your life infinitely easier. If you find this document helpful, please support us by trying our game Summation, feel free to Email us any questions (please include Iphone Dev as subject line or we will ignore you), and keep checking back for the latest update to this document and our progress with other games, we will be updating this frequently.

The first thing you need to determine is what kind of game you are making, you are at the right place if you are planning on making a 2D game which uses sprites. I wouldn't suggest a beginner to go 3D on his first try. And you should know that Google is your best friend, you can find an answer to almost everything if you know what to look for. I found most of my answers googling but I have organized it here to make your life easier, so I don't really take credit for myself for the information here, but iphone develoeprs out there who has contributed to the community.

There are 2 ways to begin, one is to do everything on your own, the other thing is to use a framework. I would go with the first option first, for those who would like to read about frameworks, you may skip this part. Doing everything yourself means you will mainly be using UIViews and IB development on Xcode, using IB is an option, but perosonally I feel that having a visual on placements of object helps one greatly on their first project, when they are unfmiliar with Iphone layouts.

I didn't find books especially helpful, but if you want to have something on hand to reference, this one works better than others, Beginning iPhone Development: Exploring the iPhone SDK by Dave Mark and Jeff LaMarche

Using UIViews and IB

Now, onto the actual Turorial. This is the first, and the best Tutorial, for using Xcode to build a project and IB to do most of the visual placements of the coding, Iphone Game Programming, a 4-part tutorial by Brandon. I learned most of the programming needs for Summation from that tutorial.

There was a few things that tutorial didn't teach you which I found paramount to game programming, first thing is learning how to use two NSMutableArray to build a matrix that can store your data. Here's how to initiate one.

self.imageMatrix = [NSMutableArray arrayWithCapacity: 10 ] ;
self.imageMatrix = [[NSMutableArray alloc]init];
for (int x = 0 ; x < 9 ; x++ )
{
[self.imageMatrix insertObject: [NSMutableArray arrayWithCapacity: 9 ] atIndex: x] ;

for (int y = 0 ; y < 11 ; y++)
{
// initialize each element with something
[[self.imageMatrix objectAtIndex: x]insertObject:@" " atIndex: y ];
}
}

There are many more elegant ways to play music, but to me using AVplayer was much easier than what was in the Tutorial, here's the code to get you started:

NSString *path = [[NSBundle mainBundle] pathForResource:@"background" ofType:@"WAV"];
MySong=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[MySong prepareToPlay];
[MySong play];

Note that there are some problems with the library playing 2 sounds concurently if they are Mp3 files, wav seems to work fine for us.

After you have built a framework of your game, you are probably wonder how iphone works with the different screens, learning how to use your application delegate to load different screens is useful, here's the sample code to help you, put that in your application delegate and use your game screen to call that function.

- (void)gameOver {
[GameViewController release];
self.gameOverViewController = [[GameOverViewController alloc] initWithNibName:@"GameOverViewController" bundle: nil];
[self.window addSubview:[gameOverViewController view]];
[self displayNewView:gameOverViewController.view];
}

A useful Tutorial for learning NSUserDefaults, which means saving information about the user so it may be carried ontoward the next session.

Last but not least, here's the ipmortant part, how to build a global scoreboard, for that CocosLive is a simple solution, simply sign in with your google account and create a game account there and go through the steps there to access a global scoreboard. CocosLive is part of the Cocos2D framework, but for this part of the tutorial we are not using the whole framework. There are many examples from the CocosLive link that will give you sample code to implement your highscore table with their API. And that concludes this part of the tutorial

Cocos 2D Framework

On selecting a framework, there are many frameworks out there, Torque (good for 2D games) and Unity (good for 3D games) both have Iphone support and it seems the Iphone deveopment community favors Cocos2D, a free framework. The advantage of selecting a framework over building everything yourself is that it saves you a lot of time with pre-built engines. And you may want to select the framework based on the kind of game you are doing, your budget, and how much total control you would like over your raw code. For our next project, we will be utilizing this framework, though the lack of documention out there seems to be a stumbling block for many developers, but here are links and steps to make your life infinitely easier.

Download Cocos2D, learn more about it there with their Wikis and discussion groups

Cocos2D API reference and documentation, useful for reference

A step-by-step from the beginning tutorial by the guys who did Touch Defense

A quick overview, how to utilize cocos2D

A very good 4 part tutorial

Cocos2D best practices

Chipmunk physics documentation, Chipmunk physicis is a must-learn if you want to implement physics in your cocos2D project

An example of how to animate a sprite with touch events

An introduction to understanding chipmunk

How to do image Labels, Menus in Cocos2D

That's it for now, keep checking back with us for more tutorial links, sample codes, and just to find out what we are doing. Support us with our release, follow us on Twitter and read about our gaming life on our blogs, or simply ask us technical questions. Happy Development and hope this document helps pave a way to your success.

©2003-2009 JetFable Studio - Contact Us Game Design, iPhone App, iPhone Development, Cocos2D, Publishing, Pillars, Mystery/Thriller/Science-Fiction, Puzzle Game, Brain Teaser, iPhone App Summation. iPhone App Tiki Smasher


Read the ultimate Iphone 2D Development Guide, a must for beginner developers looking for the right place to take the first step.