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

Keeping the main character in view


We will need to constantly check whether the zombies are in view, so we need to implement the update method that will perform the appropriate calculations. So add the following block to GameScene.m:

- (void)update:(NSTimeInterval)delta {
    // Keep the zombie on view
    [self setZombieOnView:_zombie.position];
}

Implement the setZombieOnView method with the following lines:

- (void)setZombieOnView:(CGPoint)position {
    // Establish the maximum positions on screen
    NSInteger positionX = MAX(position.x, _screenSize.width / 2);
    NSInteger positionY = MAX(position.y, _screenSize.height / 2);
    // Calculate the limits in both axes
    float backgroundWidth = _background0.contentSize.width + _background1.contentSize.width + _background2.contentSize.width + _background3.contentSize.width;
    positionX = MIN(positionX, backgroundWidth - (_screenSize.width / 2));
    positionY = MIN(positionY, _screenSize.height / 2);

    // Initialize current position...