Game Programming Tutorial – Beginners Action Game

Welcome to my game programming tutorial on how to create a beginners action game for the iOS. This is a step by step tutorial for creating an iPhone action game. The principles taught also apply to the iPad.

You can see the result of a game running the code in the video below:

The graphics are a bit different with the tutorial (as some of those in the video are being used in an actual project) but that is the result of running the actual code within the tutorial.

Specificially the tutorial features:

  • Loading/Placing Images
  • Animation
  • Touch Events
  • Text
  • Sound Effects
  • Background Music
  • Buttons
  • Resetting The Game

Pre-requisites For This Game Programming Tutorial

Now before we start there are a few pre-requisites. You should have a basic understanding of Objective-C and will need a Mac with the iPhone SDK installed. The game was created using iPhone SDK 4.0, and the Sparrow Framework. Also note that I created this to be as easy to follow as possible so I used a minimum number of classes/methods to make it as easy as possible for a beginner to follow – I’ve even exclusively used autorelease objects  so you don’t need to worry about memory management (one of the facets of Objective-C many beginners have trouble with).

Why Am I Using The Sparrow Framework In This Game Programming Tutorial?

In case you’re wondering why I chose to go the route of using the Sparrow Framework it’s because I believe it is the easiest iPhone game development framework available for free for a beginner to start with, and it has already been used in a number of games in the app store.

Please share this tutorial by tweeting it or sharing using one of the buttons at the bottom.

You can navigate through the tutorial by using the page numbers at the bottom of each page.

If you’d like to jump right in you can download a project with all the resources loaded in here.

Getting The Code

You can download the code using the download links at the end of each page, or you can use the Github project found here:
https://github.com/maniacdev/iOS-Balloon-Game-Tutorial

Otherwise if you’d like to go the route of installing Sparrow, and setting everything up you can visit the Sparrow Framework getting started page here which explains the process of setting up an Xcode project that utilizes the Sparrow Framework. You can get just the game assets (graphics/sound) here.

The tutorial is definitely not perfect, and there are probably some spelling/grammar mistakes, but it is the most extensive single tutorial I’ve seen around and might never be released if I tried to perfect it :)

The first thing we’re going to do is start the game scene by displaying in a background image, adding the score and level text, and playing some music.  To navigate through this game programming tutorial use the page numbers at bottom, now go to page 2 by clicking that number at the bottom of this article to go to that page.

DeliciousTwitterTechnoratiFacebookLinkedInEmail
Tags: , , , , , , , , ,

Pages: 1 2 3 4 5 6

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can send a Tweet to @maniacdev on Twitter or submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

  • http://www.sparrow-framework.org Daniel

    Thanks a lot for that great tutorial! It's very well written and as easy to follow as possible, for a complex topic like that. I'm sure this was a lot of hard work …!

  • http://maniacdev.com maniacdev

    Thanks.. it was a lot more than i anticipated.. I hacked out the original code in about 20 minutes, and wrote the comments pretty quickly.

    Next time I'll tackle a tutorial like this in more bite-sized chunks. I did want to see though if a more encompassing tutorial would be more popular.

  • Rio

    It was better tutorial than all my iPhone books about development. Thank You Very Much.

    BTW I reach level 19 :)

  • Pingback: Beginner Action Game Tutorial « iPhone Open Source - Nick Vellios

  • marciokoko

    Gr8 tutorial! Could u pls just explain how the SPDisplayObject *currentBalloon=(SPDisplayObject*)[event target] assigns the clicked-balloon?

  • http://maniacdev.com maniacdev

    Hi Marcio.. that gets you the clicked balloon because [event target] returns the object that threw the event which in that case is the balloon that was clicked.

    The balloon is an image which is an SPDisplayObject.. (SPDisplayObject*) casts the target as an SPDisplayObject (we need to do this here because [event target] returns an id and we want to do some things that require it to be an SPDisplayObject).

  • marciokoko

    ok one last thing. about sparrow. it says to create a new folder to which all builds will be placed into. so from now on, the build products will not be placed into each apps corresponding project folder but rather into this new directory? Isnt that avoidable? it just seems neater to have everything in the same directory.

  • marciokoko

    I dl sparrow and placed it in /documents/iphoneapps/ which is where all my project folders are. I then created a new folder in iphoneapps called sparrow builds and set it from my build folders in preferences as directed and added the sparrow/src as instructed. But when I run the simulator it just gave me a black screen and hung and crashed Xcode. Everytime I opened Xcode it would crash, I'd reopen, crash, 3x until the 4th reopen it would open correctly so I'm reinstalling Xcode. This ever happen before? :-(

  • http://maniacdev.com maniacdev

    They are still placed into the project folder.

  • http://maniacdev.com maniacdev

    What does it say in the terminal window when it crashes?

  • Anonymous

    thx, i got it working after reinstalling xcode again. something must have gone haywire during the original install. got the game working fine now. thx for the tutorial. this is my first intro into a game. i did look into the iphonecoredatarecipes tutorial linked in your site and id like to ask, could i ask u some questions about that?

  • http://www.straydogstrut.co.uk/ straydogstrut

    This is a really good tutorial, thank you!

    However, i'm having a little trouble displaying the score and level textfields. The score one shows at the top right fine (and i've experimented with different values to see how to place it in other areas), but the level one doesn't show at all. I've been over and over my code and I don't think i've omitted anything.

    Also, in your example, why do you initialise the level textfield again just before you set the fontName etc? Hasn't it already been initialised a long with the score in the previous statements? I've tried mine with and without this line and it makes no difference, “level: 1″ still won't show.

    Thanks, i've read over the whole tutorial to get a general idea. It's really good, and I hope you'll do more of these.

  • http://maniacdev.com maniacdev

    The re-declaration was a mistake thank you for pointing that out, I corrected the page. Can't check the code sample right now as I am away from home without a Mac at my disposal.

    What happens when you run the project on the bottom of that page? Should display both the score and level when run.

    All I can think of is that the [self addChild:levelTextField]; line is missing from your code.

  • http://www.straydogstrut.co.uk/ straydogstrut

    Thanks for the reply.

    Yes, the project at the bottom of the post runs fine and I can see both textfields. I do have [self addChild: levelTextField]; in my code so i'm at a loss to explain it myself.

    Don't worry about it though, i'll see how I get on when I need to do something similar in my own project. Enjoy the rest of your day=)

  • http://maniacdev.com maniacdev

    Hey, it was no problem.. I was just taking care of a friend's kids got bored watching them play Wii so I decided to check for comments :)

    Hope you were able to find what was missing.

  • http://maniacdev.com maniacdev

    You’d need to refresh my memory on which tutorial that is!

  • Anonymous

    IphoneCoreDataRecipes is the apple sample code…for storing cooking recipes…?

    MV

  • Guest

    Write a platform tutorial please. Sparrow is already a more elegent framework than cocos.

  • Aaron

    Maybe this was mentioned somewhere, but I couldn’t find it…

    I had to add the media files to my project before any reference to them would work (the app just crashes until then).

    Add them by choosing Add > Existing Files from the Actions menu in your main project window, or by choosing Project > Add to Project in the main menu.

    Maybe this isn’t necessary for all users, but it took me a while to figure it out.

    I’m in XCode 3.2.3 using SDK 4.0.2

  • http://maniacdev.com maniacdev

    Hadn’t seen that or had it mentioned.. but thankyou for mentioning it. I will mention it in the documentation.

  • http://twitter.com/oscarolim oscarolim

    Thanks for this tutorial. I was looking for a simple game engine that would be easy to learn, and this tutorial manage to do that :)

  • Pingback: iphone – ipad 3d engines

  • http://maniacdev.com maniacdev

    What file format are you replacing it with?

  • James33707

    Pretty Good… Very Helpful!

  • Pingback: Programmation de jeu iPhone | ISIC Weblog

  • Pingback: TechLab.105 » Blog Archive » Intent M3

  • Kalle

    Very helpful, thanks!

    However, the app will not start in XCode4 in the iPhone Simulator. I read the comment about adding the media files again.. not sure that worked in Xcode4 .. or am I doing something wrong?

  • Richard

    I downloaded the project with all the resources loaded. I followed the instruction on the page 2 that added the background image, textfields for score and level, and music. But when I build and run on the simulator, it just display black screen and terminate the program. I’m not sure what I missed. I’m running the xCode 4. Any help would be appreciated. Thanks !!!

  • John

    Interesting.. I will check it out. I have modified the project on my end so I need to try the original.

  • Antonio

    I got the following messages on the console:

    2011-04-11 10:36:28.111 BalloonGameTutorial[1521:207] One Tap (Touch Event)
    AudioStreamBasicDescription: 2 ch, 44100 Hz, ‘lpcm’ (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved
    2011-04-11 10:36:30.299 BalloonGameTutorial[1521:207] failed to create framebuffer: 8cd6

    Thankfull for help!

  • boney

    On XCode 4 its displaying the empty screen with a console message

    AudioStreamBasicDescription: 2 ch, 44100 Hz, ‘lpcm’ (0x00000C2C) 8.24-bit little-endian signed integer, deinterleaved

    Dont ahve any idea. I am a nob in game development…

  • John

    Thanks for the great tutorial. It worked and it was easy to follow.

  • http://NA Brad

    Outstanding! This really sells Sparrow for me.
    The only issue I encountered was in the for loop for the addBalloons method. You show the entity for less than, not the less than sign. Easy and obvious fix:

    for(int i = 0; i < level; i++)
    …should be…
    for(int i = 0; i < level; i++) {

    I will be showing this to my iPhone class.
    Thanks so much!

  • John

    Thanks Brad, it looks like one of the many WP or plugin updates has messed things up in the code area.. will have to look as to why that is happening.

  • John

    Hi.. don’t get that, but I assume this error is in the simulator? Unfortunately the simulator has issues with sound.

  • John

    I am unable to duplicate that error.. was that in the simulator?

  • Andy

    Very nice game……

  • Gar1G

    everything worked except when i touch the balloon, the app crashes. What do i need to fix because xcode can’t find any errors. i am testing this in the iphone simulator

  • John

    Sounds like a sound issue – the iOS simulator has quite a few problems playing sound. I will look into it. Which version of the SDK/Simulator were you running in?

  • http://twitter.com/glacialheart Luke Brady

    This is such a huge help to me.  Thank you for posting this.  Exactly what I need to jump start my iOS development.

    I downloaded the game files and got it to work on the first try. OSX 10.5 iOS SDK 4.3

  • Jack Duthen

    Hello!  Thank you for the tutorial!
    I noticed just a small detail: the page #2 and the following ones are tagged as “c++” as they should be tagged as “objective-C”.

  • http://maniacdev.com maniacdev

    Thanks for the tip, checking it out. Also don’t know why Disqus made me moderate this post.

  • http://maniacdev.com maniacdev

    Thanks for the tip, checking it out. Also don’t know why Disqus made me moderate this post.

  • http://maniacdev.com maniacdev

    Ahh.. the auto tagging plugin I used for about a day tagged this post and some others.. thankyou!

  • Leona Eden

    Super tutorial! Thanx a lot!

  • Davide Taini

    Here with Xcode 4.2.1 and IOS 5 SDK, to make the tutorial compile, i had to comment out one line in SPPoolObject.m

  • Davide Taini

    This one, to be precise:

      //      memset((id)object + 2, 0, malloc_size(object) – 8);

  • http://twitter.com/KillerkoUK Peter P

    great tutorial! easy to follow and understand.. do you have more tutorials like this one?

  • http://www.facebook.com/people/Vijyendra-Sisodia/100000214321787 Vijyendra Sisodia

    very helpful ….tnx