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

Giving AI to the defenses


Now that we have a way to place the defenses, we should provide them with some intelligence so they know when they have to shoot.

For that reason, we will define an area of influence for each defense so they can attack enemies that move into this area.

First let's represent it visually by adding a new property to Defense.h:

// Property for the covered area
@property (readwrite, nonatomic) CCDrawNode *coveredArea;

We have defined a draw node, as the area covered will be a circle around the defense.

In Defense.m, initialize it by adding the following lines at the end of the initDefenseWithLevel:

    // Initialize covered area
    _coveredArea = [CCDrawNode node];
    _coveredArea.opacity = 0.15f;
    _coveredArea.anchorPoint = CGPointMake(0.0f, 0.0f);
    [_coveredArea drawDot:_position radius:1.5f*self.contentSize.width color:[CCColor colorWithRed:0.0f green:1.0f blue:0.0f alpha:0.25f]];

This will create a green circle with 1.5 times the defense size of the radius and decreased...