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

如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! 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 parents

    for (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 through
    if ([self.shortestPath count] == 0) {
        self.shortestPath = nil;
        return;
    }

    // Get the next step to move to
    ShortestPathStep *s = [self.shortestPath objectAtIndex:0];

    // Prepare the action and the callback
    id 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*寻路算法(五)的更多相关文章

  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. 5-15 QQ帐户的申请与登陆 (25分) HASH

    实现QQ新帐户申请和老帐户登陆的简化版功能.最大挑战是:据说现在的QQ号码已经有10位数了. 输入格式: 输入首先给出一个正整数NN(\le 10^5≤10​5​​),随后给出NN行指令.每行指令的格 ...

  2. 微信小程序适配iphonex

      // 在app.js中判断是否是哪种设备 globalData: { isIphoneX: false, userInfo: null }, onShow:function(){ let that ...

  3. 解决$.ajax()在IE10以下失效的问题

    一.问题重现 本地的脚本请求测试环境的接口 $.ajax({ url: ***, type: GET, data: data, success: function (data) { **** } }) ...

  4. 线性回归(Linear Regression)均方误差损失函数最小化时关于参数theta的解析解的推导(手写)

    第一页纸定义了损失函数的样子, theta, X 和 y 的 shape, 以及最终的损失函数向量表现形式. 第二页纸抄上了几个要用到的矩阵求导公式,以及推导过程和结果. 要说明的是:推导结果与the ...

  5. Docker学习系列(二)Docker初体验

    一.系统要求 Docker的安装,需要在CentOS 7.0+版本,内核至少3.10,64-bit uname --r [randy@randysun ~]$ uname --r -.el7.x86_ ...

  6. Docker常见仓库CentOS

    CentOS 基本信息 CentOS 是流行的 Linux 发行版,其软件包大多跟 RedHat 系列保持一致. 该仓库提供了 CentOS 从 5 ~ 7 各个版本的镜像. 使用方法 默认会启动一个 ...

  7. Docker镜像的实现原理

    Docker 镜像是怎么实现增量的修改和维护的? 每个镜像都由很多层次构成,Docker 使用 Union FS 将这些不同的层结合到一个镜像中去. 通常 Union FS 有两个用途, 一方面可以实 ...

  8. Java内存泄漏分析系列之二:jstack生成的Thread Dump日志结构解析

    原文地址:http://www.javatang.com 一个典型的thread dump文件主要由一下几个部分组成: 上图将JVM上的线程堆栈信息和线程信息做了详细的拆解. 第一部分:Full th ...

  9. 解读Batch Normalization

    原文转自:http://blog.csdn.net/shuzfan/article/details/50723877 本次所讲的内容为Batch Normalization,简称BN,来源于<B ...

  10. jboss规则引擎KIE Drools 6.3.0 Final 教程(3)

    在前2部教程中,介绍了如何在本地运行.drools文件以及使用stateless的方法访问远程repository上的规则. KIE Drools还提供了一种叫有状态-stateful的访问方式. 运 ...