如何在Cocos2D游戏中实现A*寻路算法(五)

前端之家收集整理的这篇文章主要介绍了如何在Cocos2D游戏中实现A*寻路算法(五)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;)


免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流之用,请勿进行商业用途。同时,转载时不要移除本申明。如产生任何纠纷,均与本博客所有人、发表该翻译稿之人无任何关系。谢谢合作!

跟随着黄色砖块前进

现在我们已经找到了我们的路径,我们只需要让猫咪跟随它.

我们接下来要做的是记住整个路径,并且使得猫咪根据路径一步一步的移动.

在CatSprite.h中建立一个存储路径的数组,在CatSprite的@interface的私有段内添加:

  1. NSMutableArray *shortestPath;

然后完成CatSprite.m中的如下修改:

  1. // Add inside the CatSprite private properties and methods section
  2. @property (nonatomic,retain) NSMutableArray *shortestPath;
  3.  
  4. // After the CatSprite @implementation
  5. @synthesize shortestPath;
  6.  
  7. // Inside initWithLayer
  8. self.shortestPath = nil;
  9.  
  10. // Inside dealloc
  11. [shortestPath release]; shortestPath = nil;

现在我们将创建一个存储整个路径并且管理开始动画的方法,在CatSprite.m中完成如下修改:

  1. // Add inside the CatSprite private properties and methods section
  2. - (void)constructPathAndStartAnimationFromStep:(ShortestPathStep *)step;
  3.  
  4. // Inside moveToward,comment out the pathFound BOOL
  5. //BOOL pathFound = NO;
  6.  
  7. // Inside moveToward,replace pathFound = YES with this:
  8. [self constructPathAndStartAnimationFromStep:currentStep];
  9.  
  10. // Also comment all of the debugging statements below that.
  11.  
  12. // Inside moveToward,replace if (!pathFound) with this:
  13. if (self.shortestPath == nil) { // No path found
  14.  
  15. // Add this new method:
  16.  
  17. // Go backward from a step (the final one) to reconstruct the shortest computed path
  18. - (void)constructPathAndStartAnimationFromStep:(ShortestPathStep *)step
  19. {
  20. self.shortestPath = [NSMutableArray array];
  21.  
  22. do {
  23. 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 ;-)
  24. [self.shortestPath insertObject:step atIndex:0]; // Always insert at index 0 to reverse the path
  25. }
  26. step = step.parent; // Go backward
  27. } while (step != nil); // Until there is no more parents
  28.  
  29. for (ShortestPathStep *s in self.shortestPath) {
  30. NSLog(@"%@",s);
  31. }
  32. }

注意在moveToward方法中,我们调用了一个新的方法替换了原来的在控制台中打印结果的代码,并且我们删除了pathFound变量.像往常一样,constructPathAndStartAnimationFromStep方法中的注释详细解释了实际发生了什么.

现在编译运行,如果你触摸和我们之前说过的相同的瓦块,你应该看到如下日志:

  1. <ShortestPathStep: 0x6b37160> pos=[24;1] g=1 h=4 f=5
  2. <ShortestPathStep: 0x6b37340> pos=[23;1] g=2 h=3 f=5
  3. <ShortestPathStep: 0x6b37590> pos=[22;1] g=3 h=2 f=5
  4. <ShortestPathStep: 0x6b395c0> pos=[21;1] g=4 h=3 f=7
  5. <ShortestPathStep: 0x6b37ae0> pos=[20;1] g=5 h=4 f=9
  6. <ShortestPathStep: 0x6b38c60> pos=[20;2] g=6 h=3 f=9
  7. <ShortestPathStep: 0x6b36510> pos=[20;3] g=7 h=2 f=9
  8. <ShortestPathStep: 0x6b3b850> pos=[21;3] g=8 h=1 f=9
  9. <ShortestPathStep: 0x6b3cf30> pos=[22;3] g=9 h=0 f=9

注意它和以前是相似的,除了现在它是从开始到结束(反转以前的结果)并且存放在数组中的数据更便于我们去使用.

最后要做的事情是通过遍历shortestPath数组并且动画显示猫咪跟随的路径.为了实现这个目的,我们将创建一个方法从数组中弹出每一步的数据,使得猫咪可以移动到该位置,并且添加一个回调方法去重复调用这个方法直到路径完成.

在CatSprite.m中完成以下修改:

  1. // Add inside the CatSprite private properties and methods section
  2. - (void)popStepAndAnimate;
  3.  
  4. // Add to bottom of constructPathAndStartAnimationFromStep
  5. [self popStepAndAnimate];
  6.  
  7. // Add new method
  8. - (void)popStepAndAnimate
  9. {
  10. // Check if there remains path steps to go through
  11. if ([self.shortestPath count] == 0) {
  12. self.shortestPath = nil;
  13. return;
  14. }
  15.  
  16. // Get the next step to move to
  17. ShortestPathStep *s = [self.shortestPath objectAtIndex:0];
  18.  
  19. // Prepare the action and the callback
  20. id moveAction = [CCMoveTo actionWithDuration:0.4 position:[_layer positionForTileCoord:s.position]];
  21. id moveCallback = [CCCallFunc actionWithTarget:self selector:@selector(popStepAndAnimate)]; // set the method itself as the callback
  22.  
  23. // Remove the step
  24. [self.shortestPath removeObjectAtIndex:0];
  25.  
  26. // Play actions
  27. [self runAction:[CCSequence actions:moveAction,moveCallback,nil]];
  28. }

编译然后运行…

我们的猫咪自动移动到你点击的位置上了 :-)

猜你在找的Cocos2d-x相关文章