Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. iOS SDK

Build a Caterpillar Game with Cocos2D: Final Steps

Read Time:8 minsLanguages:
This post is part of a series called Building a Caterpillar Game with Cocos2D.
Build a Caterpillar Game with Cocos2D: Collision Detection

This is the seventh and final installment of our Cocos2D tutorial series on cloning Centipede for iOS. Make sure you have completed the previous parts before beginning.

Last Time...

In the last tutorial, we discussed how to do simple collision detection between all of the objects in our game.

In today's tutorial, we will wrap things up by discussing the scoring, winning conditions, game audio, and game over screen.

Step 1: Advancing The Game

As of right now, when you destroy the caterpillar, nothing happens. The way to progress forward is to increment the level and restart with some new sprouts and a new caterpillar. This is easily accomplished as we have been building the game to support this since the beginning. Open up GameLayer.m and add the following method:

  1. Check to see if there are any caterpillars left
  2. Increment the level
  3. Create a new caterpillar and add it to the game (based on the new level)
  4. Add sprouts to match the minSproutCount

Now that we have the method implemented, we need to call it every time the caterpillar gets hit. Inside of the splitCaterpillar:atSegment: method, add the following line before the return statement inside of the first if statement:

This is the condition when the caterpillar is just a single segment. In addition to adding it here, add it to the very bottom of this method as well. That should cover all of the cases.

If you run the game at this point, you should be able to play infinitely with the speed of the caterpillar increasing on each level.

Step 2: Scoring

Within the game, there are several places where the player might increase their score. They are:

  • Hitting a sprout
  • Hitting the caterpillar
  • Advancing to the next level

We are going to jump around quite a bit in order to add scoring to each of these actions, so bare with me.

Before we begin updating the score, we need to define three more constants which will be the base points for the scoring. Open up GameConfig.h and add the following 3 lines:

You will see how these are used as we get a little further into this tutorial.

Let's start with adding to the player score when they hit a sprout in the game. Open up Missile.m, import Player.h, and add the following code inside of the loop that checks for a collision with a sprout:

This increments the player score based on the base points and some amount of randomness based on the current level. That way, as the player goes up in level, they gain more points for taking out the sprouts.

As we did when we set up the initial score, we need to post a notification which will update the player's score label. This is done on each score update. If you run the game at this point, you should see the player's score label updating every time you hit a sprout.

The next place we are going to add scoring is when the caterpillar is hit. At the very bottom of the update: method in Missile.m, add the following code before splitting the caterpillar:

This code isn't much different than what you saw above. The final place to add scoring is when the player advances in level. This code will be placed inside of the checkNextLevel method that you wrote above. Open GameLayer.m, navigate to the checkNextLevel method and add the following code just after the if statement:

No surprises here. If you want to add some variation to the scoring in your own game, feel free to modify the score values.

Step 3: Game Over

As of right now, your player has the Game Genie on and has infinite lives. We need to change that. First, create a new file called GameOverLayer.m that extends CCLayer.

Add the following code To GameOverLayer.h:

I'll explain what each of these properties and methods do during the implementation. Now add the following code to GameOverLayer.m

  1. This is our standard method to set up a new scene. The only difference is, it takes an NSInteger and sets it to the current score.
  2. Clean up
  3. Render the background
  4. Set up the labels to display the score and high score
  5. Enable touches
  6. Look up the high score
  7. Compare new score to the highest score, save the new score if it's higher.
  8. Start a new game if the player touches the screen

Here is a screenshot of what this screen should look like:

Game Over

The last step is to open up GameLayer.m and add the following lines to the end of the updateLives method:

This checks if the lives are set to 0. If so, we replace the current scene with the Game Over scene.

Step 4: Game Audio

The final step in our game polish is to add sound. First, download the sounds below and add them to your project:


Traditionally, handling sound has been quite a pain in an OpenGL game. You would have to use something like OpenAL or another complicated C++ library. Cocos2D has greatly simplified things with their SimpleAudioEngine library. It gives you the ability to easily play looping background music as well as quick sounds.

Again, we are going to be jumping around quite a bit. So, if the placement of some code is unclear to you, please ask me in the comments or refer to the source code of this tutorial.

Open up AppDelegate.m and import SimpleAudioEngine.h and add the following line to the bottom of the applicationDidFinishLaunching method.

That's it! Only one line is needed to play our background music for the duration of the play time. Now we just need to play sound effects in response to various actions.

In Caterpillar.m, when the Caterpillar collides with the player (towards the end of the update: method):

In Missile.m, when the Missile hits the Sprout:

Also in Missile.m, when the Missile hits the Caterpillar:

In The init method in GameOverLayer.m:

That should cover all of the sounds that I have used in the game. Also, be sure to import SimpleAudioEngine.h in each of the classes above.


This concludes the 7 part tutorial series on creating a Caterpillar game using Cocos2D for the iPhone. By now, you should have a solid understanding of how to design and build a simple game using the Cocos2D game engine. If you have any questions or comments, please feel free to leave them in the comments section here or write them to me on Twitter.

Happy coding!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.