How to implement A * pathfinding Algorithm in Cocos2D games (3)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.