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

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...

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