如何在Cocos2D游戏中实现A*寻路算法(五)
大熊貓豬·侯佩原創或翻譯作品.歡迎轉載,轉載請注明出處.
如果覺得寫的不好請告訴我,如果覺得不錯請多多支持點贊.謝謝! hopy ;)
免責申明:本博客提供的所有翻譯文章原稿均來自互聯網,僅供學習交流之用,請勿進行商業用途。同時,轉載時不要移除本申明。如產生任何糾紛,均與本博客所有人、發表該翻譯稿之人無任何關系。謝謝合作!
跟隨著黃色磚塊前進
現在我們已經找到了我們的路徑,我們只需要讓貓咪跟隨它.
我們接下來要做的是記住整個路徑,并且使得貓咪根據路徑一步一步的移動.
在CatSprite.h中建立一個存儲路徑的數組,在CatSprite的@interface的私有段內添加:
NSMutableArray *shortestPath;然后完成CatSprite.m中的如下修改:
// Add inside the CatSprite private properties and methods section @property (nonatomic, retain) NSMutableArray *shortestPath;// After the CatSprite @implementation @synthesize shortestPath;// Inside initWithLayer self.shortestPath = nil;// Inside dealloc [shortestPath release]; shortestPath = nil;現在我們將創建一個存儲整個路徑并且管理開始動畫的方法,在CatSprite.m中完成如下修改:
// Add inside the CatSprite private properties and methods section - (void)constructPathAndStartAnimationFromStep:(ShortestPathStep *)step;// Inside moveToward, comment out the pathFound BOOL //BOOL pathFound = NO;// Inside moveToward, replace pathFound = YES with this: [self constructPathAndStartAnimationFromStep:currentStep];// Also comment all of the debugging statements below that.// Inside moveToward, replace if (!pathFound) with this: if (self.shortestPath == nil) { // No path found// Add this new method:// Go backward from a step (the final one) to reconstruct the shortest computed path - (void)constructPathAndStartAnimationFromStep:(ShortestPathStep *)step {self.shortestPath = [NSMutableArray array];do {if (step.parent != nil) { // Don't add the last step which is the start position (remember we go backward, so the last one is the origin position ;-)[self.shortestPath insertObject:step atIndex:0]; // Always insert at index 0 to reverse the path}step = step.parent; // Go backward} while (step != nil); // Until there is no more parentsfor (ShortestPathStep *s in self.shortestPath) {NSLog(@"%@", s);} }注意在moveToward方法中,我們調用了一個新的方法替換了原來的在控制臺中打印結果的代碼,并且我們刪除了pathFound變量.像往常一樣,constructPathAndStartAnimationFromStep方法中的注釋詳細解釋了實際發生了什么.
現在編譯運行,如果你觸摸和我們之前說過的相同的瓦塊,你應該看到如下日志:
<ShortestPathStep: 0x6b37160> pos=[24;1] g=1 h=4 f=5 <ShortestPathStep: 0x6b37340> pos=[23;1] g=2 h=3 f=5 <ShortestPathStep: 0x6b37590> pos=[22;1] g=3 h=2 f=5 <ShortestPathStep: 0x6b395c0> pos=[21;1] g=4 h=3 f=7 <ShortestPathStep: 0x6b37ae0> pos=[20;1] g=5 h=4 f=9 <ShortestPathStep: 0x6b38c60> pos=[20;2] g=6 h=3 f=9 <ShortestPathStep: 0x6b36510> pos=[20;3] g=7 h=2 f=9 <ShortestPathStep: 0x6b3b850> pos=[21;3] g=8 h=1 f=9 <ShortestPathStep: 0x6b3cf30> pos=[22;3] g=9 h=0 f=9注意它和以前是相似的,除了現在它是從開始到結束(反轉以前的結果)并且存放在數組中的數據更便于我們去使用.
最后要做的事情是通過遍歷shortestPath數組并且動畫顯示貓咪跟隨的路徑.為了實現這個目的,我們將創建一個方法從數組中彈出每一步的數據,使得貓咪可以移動到該位置,并且添加一個回調方法去重復調用這個方法直到路徑完成.
在CatSprite.m中完成以下修改:
// Add inside the CatSprite private properties and methods section - (void)popStepAndAnimate;// Add to bottom of constructPathAndStartAnimationFromStep [self popStepAndAnimate];// Add new method - (void)popStepAndAnimate { // Check if there remains path steps to go throughif ([self.shortestPath count] == 0) {self.shortestPath = nil;return;}// Get the next step to move toShortestPathStep *s = [self.shortestPath objectAtIndex:0];// Prepare the action and the callbackid moveAction = [CCMoveTo actionWithDuration:0.4 position:[_layer positionForTileCoord:s.position]];id moveCallback = [CCCallFunc actionWithTarget:self selector:@selector(popStepAndAnimate)]; // set the method itself as the callback// Remove the step[self.shortestPath removeObjectAtIndex:0];// Play actions[self runAction:[CCSequence actions:moveAction, moveCallback, nil]]; }編譯然后運行…
我們的貓咪自動移動到你點擊的位置上了 :-)
總結
以上是生活随笔為你收集整理的如何在Cocos2D游戏中实现A*寻路算法(五)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 注解 @Scheduled
- 下一篇: A SPI class of type