How to Make a Game Like Jetpack Joyride using LevelHelper, SpriteHelper [Cocos2D 2.X edition] – Part 3

In this part of the series, you’ll make your game fully capable of handling collisions. In other words, by the end of this part you’ll be able to reward and kill the mouse! By .

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Tweaking Gameplay

Your game is looking good so far, but there are still a number of problems.

The first of these problems is that if you touch the screen after the mouse dies, he starts flying around like he’s been resurrected! Although “Zombie Mouse” might make for a popular game on the App Store, this is probably not the effect you’re going for ;]

Let’s put a stop to this zombie behavior before it gets out of hand! Just add the following check to the beginning of ccTouchesBegan::

    
if(playerIsDead)
    return;

Basically, you test to see if the player is dead. If he is, then you do nothing further.

If you run the game now, the zombie behavior won’t occur anymore. But there are other issues.

For example, in my level there are places where the mouse can’t get past a laser because it’s active, but he can’t go under it because there’s a cat in the way. Your level may have similar problems.

It’s time to play-test the level and make any necessary modifications to be sure it’s possible for the player to successfully finish the game.

Open LevelHelper, switch to level03, and drag the cats, dogs, and lasers around (as necessary) so that the player has a way through. (But don’t make it too easy!)

Make sure to save your changes to the level!

Since you’re modifying the level anyway, let’s create a way to know when the player touches the ground. To do this we’ll define a new tag and add that tag to the bottom border of the physic boundary. Click the Define Tag button (as you did in Part Two) and add a tag named “GROUND.”

Next, add the new tag to the bottom part of the Physic Boundary shape. To do this, click the Physic Boundaries button and select the tag for the correct boundary as per the image below:

Save your level in LevelHelper.

Since you added a new tag (GROUND), make sure you also regenerate the supporting code by going to File/Generate Code.

Next, you have to define the collision callback between the player and the ground. Add the following to the end of setupCollisionHandling:

  
    [loader registerPreCollisionCallbackBetweenTagA:PLAYER
                                            andTagB:GROUND
                                         idListener:self
                                        selListener:@selector(mouseGroundCollision:)];

Now add the mouseGroundCollision: implementation:

  
-(void)mouseGroundCollision:(LHContactInfo*)contact {
    if(playerIsDead)
        return;
    if(playerWasFlying) {
        [[SimpleAudioEngine sharedEngine] playEffect:@"ground.wav"];
        [player prepareAnimationNamed:@"mouseFall" fromSHScene:@"Animations"];
        [player playAnimation];
        [player setAnimationHasEndedObserver:self selector:@selector(fallAnimHasEnded:)];
    }
    playerWasFlying = false;
}

You first test if the player is dead since you don’t want to do anything further if the mouse is dead. You then test if the player was flying when they touched the ground. If they were, you play the landing sound.

Then you start the mouseFall animation. You also register for a notification. That way, when the animation ends, you get notified so that you can set another animation on the player sprite.

Now add the method that gets called when the mouseFall animation ends as follows:

  
-(void)fallAnimHasEnded:(NSNotification*)notif {
    LHSprite* sprite = [notif object]; //get the sprite on which the animation has ended
    
    if(sprite == player){
        [player prepareAnimationNamed:@"mouseRun" fromSHScene:@"Animations"];
        [player playAnimation];
        [player removeAnimationHasEndedObserver];
    }
}

This new method receives a notification object sent by the animation as a parameter. You access the sprite which triggered this notification using the “object” method. Then you can find all the info about the animation from the LHSprite object.

If the sprite is the player, you prepare and play the mouseRun animation and you remove the notification observer (this is not mandatory but its best to do it).

Compile and play.

It looks like you have almost everything in place, doesn’t it? But there is still one thing you need to fix!

You may have noticed that if you run the game now, coins taken from the level don’t reappear when the parallax restarts. If you recall, you hide them after player collisions. So you need a way to make them visible again when the parallax starts over.

Inside the retrieveRequiredObjects method declaration, add the following line, after you’ve taken the pointer to the parallax node:

  
-(void) retrieveRequiredObjects
{
    // existing lines
    paralaxNode = [lh paralaxNodeWithUniqueName:@"Parallax_1"];
    NSAssert(paralaxNode!=nil, @"Couldn't find the parallax!");
    
    // add this new line
    [paralaxNode registerSpriteHasMovedToEndListener:self 
        selector:@selector(spriteInParallaxHasReset:)]; 

    // rest of code...

The new code sets up a call to a new method (spriteInParallaxHasReset:) that will be executed whenever a sprite in the parallax is reset to the back of the parallax. This method gets called when the sprite exits the view.

Add the new method as follows:

  
-(void) spriteInParallaxHasReset:(LHSprite*)sprite {    
    if(COIN == [sprite tag]){
        [sprite setVisible:YES];
    }
}

The method’s pretty simple – test the tag of the sprite that was reset by the parallax, and if it’s tagged as a COIN, make it visible again.

Compile and run. See how long you can play without dying! :]

Rocket mouse part 3 complete!

Where to Go From Here?

If you’ve followed along this far, you should have a smoothly-functioning game that is very similar to Jetpack Joyride! It’s not quite complete (that happens in Part Four), but very close, with animations, collisions, sounds, and correct gameplay.

In case you need it, here is a project that includes all of the code for this tutorial series up to this point.

In the fourth and final part of this tutorial series, we’ll increase the difficulty of the game by making some of the lasers rotate. But we’ll also add another point-scoring opportunity in the form of bunnies running through the level that the player can kill, and display the score as it increases.

Until then, feel free to participate in forum discussion both below and on the LevelHelper site.