How to implement A * pathfinding Algorithm in Cocos2D games (3)
Create open and closed lists
Next we will use two NSMutableArray to track and save our open and closed lists.
You may wonder why NSMutableSet is not used. Well, there are two reasons:
NSMutableSet is not ordered, but we want the list to be sorted by F value for quick search. NSMutableSet does not call the isEqual method in the ShortestPathStep class to test whether the two elements are the same (but we need it to do this ).
Let's add the definitions of these arrays in CatSprite. h:
@interface CatSprite : CCSprite { //...@private NSMutableArray *spOpenSteps; NSMutableArray *spClosedSteps;}
Then, modify CatSprite. m as follows:
// Add to top of file// Private properties and methods@interface CatSprite () @property (nonatomic, retain) NSMutableArray *spOpenSteps;@property (nonatomic, retain) NSMutableArray *spClosedSteps;@end// Add after @implementation CatSprite@synthesize spOpenSteps;@synthesize spClosedSteps;// Add inside initWithLayerself.spOpenSteps = nil;self.spClosedSteps = nil;//Add dealloc method to CatSprite- (void)dealloc{ [spOpenSteps release]; spOpenSteps = nil; [spClosedSteps release]; spClosedSteps = nil; [super dealloc];}
Note: because the original text was written earlier, some of the instance variable declaration methods and the processing for destruction are no longer needed, you can keep this article in mind when reading the code in this series of blog posts;) Cat and pig note.