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

Placing defenses


Once the predefined positions have been loaded, let's add the defenses.

This will happen when the player touches the screen at one of the available positions, so add the following import at the top of GameScene.h:

#import "Defense.h"

Then in GameScene.m, declare an array of defenses:

// Declare array of defenses
NSMutableArray *_defenses;

Initialize it by adding the following lines at the end of the init method:

    // Initialize array of defenses
    _defenses = [[NSMutableArray alloc] initWithCapacity:kNUM_DEFENSES];
    
    // Enable touches management
    self.userInteractionEnabled = TRUE;

We are also enabling touch handling, so we can detect when the player is touching the screen.

Finally, let's implement the touchBegan method by adding the following lines:

-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    // Get touch position
    CGPoint touchLocation = [touch locationInNode:self];
    
    // Iterate defense positions
    for (CCSprite *defensePosition...