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

Drawing a life bar


In this section, we will implement a classic red and yellow life bar that will represent changes whenever we have been damaged.

First of all, declare two new draw nodes in GameScene.m by adding the following lines after BOOL _zombieCollisionDetected;:

    // Draw nodes to represent life bar
    CCDrawNode *_lifeBarYellow;
    CCDrawNode *_lifeBarRed;

Initialize them with these lines at the end of the init method just before return self;:

    _lifeBarYellow = [CCDrawNode node];
    _lifeBarRed = [CCDrawNode node];
    [self createLifeBars];

Implement the method with this block of code:

- (void) createLifeBars {
    float rectHeight = 70.0;
    float rectWidth = _zombie.lifePoints * 40;
    float positionX = 20;
    float positionY = [CCDirector sharedDirector].viewSize.height - rectHeight - 20;
    // Creating array of vertices
    CGPoint vertices[4];
    vertices[0] = CGPointMake(positionX, positionY); //bottom-left
    vertices[1] = CGPointMake(positionX, positionY + rectHeight...