Book Image

Cocos2d Game Development Blueprints

By : Jorge Jordán
Book Image

Cocos2d Game Development Blueprints

By: Jorge Jordán

Overview of this book

Table of Contents (15 chapters)
Cocos2d Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

2-star challenge – having three lives


We have developed a way to end the game using a top score to reach, but you know most of the time games end because you've lost all your lives. I want you to take what you learned when creating the score label on the screen and put a lives label in the top-left corner of the screen that will represent the lives left. This label will show the number of lives as a countdown starting from 3, and this counter will decrease when a snowball hits the yeti. Then, as soon as the counter arrives at 0, you will stop the game and show a red GAME OVER label in the center of the screen, similar to the previous LEVEL COMPLETED label.

The solution

This is very similar to what we did to make the score count and game over labels. To achieve this, you will need an integer variable for the lives counter and a label to show its value. So in GameScene.m, add the following lines after int _gameOverScore;:

int _numLives;
CCLabelTTF *_livesLabel;

Initialize their values by adding the following code lines to the init method, just before return self;:

_numLives = 3;


_livesLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Lives: %i", _numLives] fontName:@"Chalkduster" fontSize:15];

_livesLabel.color = [CCColor orangeColor];
_livesLabel.anchorPoint = CGPointMake(0.0, 1.0);
_livesLabel.position = CGPointMake(0, screenSize.height);

[self addChild:_livesLabel];

We are setting the number of lives counter to 3 and initializing the label with the appropriate text formatted to show this counter. Then we configure the label to be orange colored and we set its anchor point to the top-left because we want it left and top-aligned. Once we have set the anchor point, we can set its position to the top-left corner of the screen, and don't forget to add it to the scene.

Until now we've just initialized the variables, so let's make them work! As we want the counter to decrease when a snowball hits the yeti, we will perform this update on the manageCollision method. Go there and add the following lines at the very beginning of the method:

_numLives--;
[_livesLabel setString:[NSString stringWithFormat:@"Lives: %i", _numLives]];
if (_numLives == 0) {
    [self gameOverLives];
    return;
}

The first thing to do is decrease the counter and update the label. Then, if the counter arrives at 0, we need to finish the game and exit from the method. We just need to declare the gameOverLives method by adding the following line to GameScene.h:

-(void) gameOverLives;

Implement the method in GameScene.m:

-(void) gameOverLives{

    CGSize screenSize = [CCDirector sharedDirector].viewSize;
    // Initializing and positioning the game over label
    CCLabelTTF *gameOverLabel = [CCLabelTTF labelWithString:@"GAME OVER" fontName:@"Chalkduster" fontSize:50];

    gameOverLabel.color = [CCColor redColor];
    gameOverLabel.position = CGPointMake(screenSize.width/2, screenSize.height/2);

    [self addChild:gameOverLabel];

    [self removeChild:_livesLabel];

    // Removing score label
    [self removeChild:_scoreLabel];

    // Stop throwing snowballs
    [self unscheduleAllSelectors];

    // Disable touches
    self.userInteractionEnabled = FALSE;

    // Stop background music and sound effects
    [[OALSimpleAudio sharedInstance] stopEverything];
}

This method is very similar to the one we implement to finish the game due to the achievement of the score target, which is why I will just focus on the differences. In this case, we create a red label with the text GAME OVER and put it in the center of the screen. Then we remove both score and lives labels and stop scheduled selectors and sounds. That's it!