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

Sharing information between nodes


As defenses have no way to know when enemies are destroyed, or if they have arrived at the tower by themselves, we are going to develop a solution to share this information between all the nodes.

We will give the Enemy class the responsibility of informing the defenses of their situation, so in Enemy.h, declare the following property:

// Property for the attacking defenses
@property (readwrite, nonatomic) NSMutableArray *attackingDefenses;

This is an array of defenses that are attacking the enemy and that should be updated with any modification of its status.

Also, let's declare two new methods:

// Declare add attacker method
- (void) addAttackingDefense:(Defense *)attackingDefense;
// Declare out of range method
- (void) outOfRangeFromDefense:(Defense *)attacker;

To avoid compilation errors, add the following import:

#import "Defense.h"

Then in Enemy.m, declare the following constant:

// Number of defenses
const int kNUMDEFENSES = 30;

We will use this constant to...