Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Cocos2d Game Development Blueprints

You're reading from   Cocos2d Game Development Blueprints Design, develop, and create your own successful iOS games using the Cocos2d game engine

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher
ISBN-13 9781783987887
Length 440 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Jorge Jord√°n Jorge Jord√°n
Author Profile Icon Jorge Jord√°n
Jorge Jord√°n
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Sprites, Sounds, and Collisions 2. Explosions and UFOs FREE CHAPTER 3. Your First Online Game 4. Beat All Your Enemies Up 5. Scenes at the Highest Level 6. Physics Behavior 7. Jump and Run 8. Defend the Tower 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!

The solution
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image