Developing a pathfinding algorithm
One way to provide nodes with intelligence is to predefine their behavior in response to external stimuli, or to give them a goal to reach.
In this case, we are going to make our enemies follow a path, which we will subdivide into steps. For this purpose, let's create a new class by following these steps:
Right-click on the Classes group in the project navigator and select New File….
Click on iOS | cocos2d v3.x | CCNode class and make this class a subclass of
CCNode
.Call it
PathStep
and click on Create.
Replace the contents of PathStep.h
with the following lines:
#import <Foundation/Foundation.h> #import "cocos2d.h" @interface PathStep : CCNode { } // Property for the next step @property (nonatomic, assign) PathStep *nextPathStep; // Property for the sprite @property (readwrite, nonatomic) CCSprite *pathStepSprite; // Init method - (PathStep *) initWithPosition:(CGPoint)position; @end
We are extending the class from CCNode
because we need it to...