如何在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*寻路算法(五)相关推荐
- 如何在Cocos2D游戏中实现A*寻路算法(二)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(三)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 如何在Cocos2D游戏中实现A*寻路算法(七)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- 即时战略游戏中实用的寻路算法分享
http://www.gameres.com/340777.html GameRes游资网授权发布,文 / 伍一峰 RTS中的寻路系统一般需要满足有以下几个条件: 1. 效率高,因为rts普遍地图大, ...
- 游戏中常用的寻路算法(6):地图表示
在本系列文档大部分内容中,我都假设A*用于某种网格上,其中的"节点"是一个个网格的位置,"边"是从某个网格位置出发的各个方向.然而,A*可用于任意图形,不仅仅是 ...
- 【IOS】如何在cocos2d 游戏中添加 移动广告
最近有需要在一款Cocos2d 游戏里面添加移动广告,大家都知道,现在有很多的移动广告平台,每个平台都有自己的SDK,每个SDK的方法,流程又都不一样,找来找去,找到了 果合移动广告. 它自己的网站上 ...
- 游戏中常用的寻路算法的分享(3):A*算法的实现
概述 剥除代码,A* 算法非常简单.算法维护两个集合:OPEN 集和 CLOSED 集.OPEN 集包含待检测节点.初始状态,OPEN集仅包含一个元素:开始位置.CLOSED集包含已检测节点.初始状态 ...
- 游戏中常用的寻路算法的分享(4)处理移动中的障碍物
一个寻路算法会计算出一条绕过静止障碍物的路径,但如果障碍物会移动呢?当一个单位移动到达某特定点时,原来的障碍物可能不在那点了,或者在那点上出现了新的障碍物.如果路线可以绕过典型的障碍物,那么只要使用单 ...
- 游戏中常用的寻路算法(5)预先计算好的路径的所用空间
有时候,影响计算寻路路径的不是时间,而是计算路径所需的上百个单元格所占的空间.寻路是需要内存来运行寻路算法,还需要额外内存来存储寻到的路径.运行寻路算法(A*,开集或闭集)所需的临时空间经常会比存储这 ...
最新文章
- ios点击大头针气泡不弹出_地图大头针气泡点击事件
- 双边滤波JAVA代码实现
- 组织模式 - Introduction
- 一步一步教你实现iOS音频频谱动画(一)
- 下列选项中不属于python循环语句的是哪一项_下列选项中,不属于字典操作的方法是哪一项?_学小易找答案...
- 僵尸进程的产生,危害和解决方案
- macbook不能进系统 备份数据_不基于备份和表,生产系统数据误删就能完全恢复?!...
- ubuntu下make无法安装的问题
- Vim编辑器显示行号且定义tab键为4个空格
- Odin插件与基于元数据的编辑器实现
- opencv图像灰度化
- Excel如何制作直方图与正态分布曲线
- 批量替换 Word 文档前几页
- Openssl 命令之cer证书转成pem. 利用ptf私钥文件生成公钥
- 灵飞经4 西城八部 第二十章 倒行逆施(五)
- 类和结构体的内存空间占有问题
- idea 无法加载识别本地类
- Java制作五子棋 V1.0
- window下easymock本地部署非常详细流程,及问题解决
- maven源码阅读之二(plexus-classworlds)
热门文章
- 你不一定真正理解的NAT术语
- 仟亿达2016年净利润2184万同比跌46%
- 2.3. 实体(Core Data 应用程序实践指南)
- Java SE(2)
- 【转】Android兼容性测试CTS --环境搭建、测试执行、结果分析
- 改变UITableView的headerView、footerView背景颜色
- CentOS 6.3编译安装Nginx1.2.2+MySQL5.5.25a+PHP5.4.5
- [转载]WPF窗口跳转及window和page区别
- Stack(栈)和Heap(堆)的区别
- Linux挂载共享文件