大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.

如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! 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. // After the CatSprite @implementation
  4. @synthesize shortestPath;
  5. // Inside initWithLayer
  6. self.shortestPath = nil;
  7. // Inside dealloc
  8. [shortestPath release]; shortestPath = nil;

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

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

注意在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. // Add to bottom of constructPathAndStartAnimationFromStep
  4. [self popStepAndAnimate];
  5. // Add new method
  6. - (void)popStepAndAnimate
  7. {
  8. // Check if there remains path steps to go through
  9. if ([self.shortestPath count] == 0) {
  10. self.shortestPath = nil;
  11. return;
  12. }
  13. // Get the next step to move to
  14. ShortestPathStep *s = [self.shortestPath objectAtIndex:0];
  15. // Prepare the action and the callback
  16. id moveAction = [CCMoveTo actionWithDuration:0.4 position:[_layer positionForTileCoord:s.position]];
  17. id moveCallback = [CCCallFunc actionWithTarget:self selector:@selector(popStepAndAnimate)]; // set the method itself as the callback
  18. // Remove the step
  19. [self.shortestPath removeObjectAtIndex:0];
  20. // Play actions
  21. [self runAction:[CCSequence actions:moveAction, moveCallback, nil]];
  22. }

编译然后运行…

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

如何在Cocos2D游戏中实现A*寻路算法(五)的更多相关文章

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

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

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  8. 如何在cocos2d项目中enable ARC

    如何在cocos2d项目中enable ARC 基本思想就是不支持ARC的代码用和支持ARC的分开,通过xcode中设置编译选项,让支持和不支持ARC的代码共存. cocos2d是ios app开发中 ...

  9. 游戏AI之A*寻路算法(3)

    前言:寻路是游戏比较重要的一个组成部分.因为不仅AI还有很多地方(例如RTS游戏里操控人物点到地图某个点,然后人物自动寻路走过去)都需要用到自动寻路的功能. 本文将介绍一个经常被使用且效率理想的寻路方 ...

随机推荐

  1. 计科1702冯亚杰C语言程序设计预备作业

    阅读邹欣老师的博客--师生关系,针对文中的几种师生关系谈谈你的看法,你期望的师生关系是什么样的? 答:首先老师和学生之间要互相尊重,我认为这是必要的.在第一点的基础上师生要互相帮助,互相配合,共同进步 ...

  2. Thread类中的静态方法

    1.currentThread() currentThread()方法返回的是对当前正在执行线程对象的引用. package thread; /** * 线程类的构造方法.静态块是被main线程调用的 ...

  3. js修改伪类元素样式

    <style type="text/css"> .htmlbox_close::before, .htmlbox_close::after { content: ''; ...

  4. easing--缓动函数--贝塞尔函数--圆盘转动抽奖应用

    http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.js http://www.robertpenner.com/easing/penn ...

  5. axios的兼容性处理

    一.简介 看看官网的简介: "Promise based HTTP client for the browser and node.js" 译:基于 Promise 的 HTTP ...

  6. 排序算法的C语言实现(上 比较类排序:插入排序、快速排序与归并排序)

    总述:排序是指将元素集合按规定的顺序排列.通常有两种排序方法:升序排列和降序排列.例如,如整数集{6,8,9,5}进行升序排列,结果为{5,6,8,9},对其进行降序排列结果为{9,8,6,5}.虽然 ...

  7. centos 7安装pycharm

    1.首先安装jdk: yum install java 结果: [root@controller bin]# java -version openjdk version "1.8.0_131 ...

  8. Android自定义View(CustomCalendar-定制日历控件)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/54020386 本文出自:[openXu的博客] 目录: 1分析 2自定义属性 3onMeas ...

  9. Linux Shell编程参考大全

    本文记录Linux Shell编程中常用基本知识,方便快速入门以及查询使用. 本文主要分为以下几个部分: 一.Shell中的变量 任何编程语言中,有关变量的定义,作用范围,赋值等都是最最基础的知识. ...

  10. Android开发学习之路--Java和Js互相调用

      随着前端的火热,以前开发的快速,越来越多的native app在其中融合了h5,就拿淘宝就是很多的h5组成的,一旦出现什么节日,他都可以不用通过更新app来实现界面的改变,而且android和io ...