如何在Cocos2D游戏中实现A*寻路算法(二)
2015-11-22 10:49
453 查看
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;)
免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流之用,请勿进行商业用途。同时,转载时不要移除本申明。如产生任何纠纷,均与本博客所有人、发表该翻译稿之人无任何关系。谢谢合作!
猫咪迷宫和A*概述
正如你所看到的,现在当你在地图某处触摸的时候,猫咪将会跳到你触摸方向的相邻瓦格中去.我们想要修改为猫咪连续移动直到你点击的位置,就像一些RPG或者点击的探险游戏一样.
让我们看一下当前触摸处理代码是如何工作的.如果你打开HelloWorldLayer,你将发现其像下面代码一样实现触摸回调:
- (void)registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; } - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { if (_gameOver) return NO; CGPoint touchLocation = [_tileMap convertTouchToNodeSpace:touch]; [_cat moveToward:touchLocation]; return YES; }
你可以看到它仅仅调用了cat sprite中的一个方法,让猫咪在地图上朝着触摸的点方向去移动.
所以我们将去修改CatSprite.m中的以下方法去找寻到目的点的最短路径,如下所示:
- (void)moveToward:(CGPoint)target { // Figure out the shortest path to the target, and start following it! }
创建ShortestPathStep类
让我们从创建一个描述路径中的step的内部类中开始.在我们的例子中,这是一个瓦块,并且它的F,G和H分值都由A*算法来计算.So添加以下代码到CatSprite.m的开头(在CatSprite的@implementation之上):
// A class that represents a step of the computed path @interface ShortestPathStep : NSObject { CGPoint position; int gScore; int hScore; ShortestPathStep *parent; } @property (nonatomic, assign) CGPoint position; @property (nonatomic, assign) int gScore; @property (nonatomic, assign) int hScore; @property (nonatomic, assign) ShortestPathStep *parent; - (id)initWithPosition:(CGPoint)pos; - (int)fScore; @end
正如你所见的,这是一个非常简单的类,其中跟踪保存了以下内容:
瓦块的坐标
分值G(注意,在这里是开始到当前位置的瓦片个数)
分值H(注意,在这里它是当前到结束位置估计的瓦块数量)
ShortestPathStep来自哪里
分值F,就是该瓦块的分值(用F + G来计算).
现在我们可以在CatSprite.m最后(在@end下面)写出实现代码:
@implementation ShortestPathStep @synthesize position; @synthesize gScore; @synthesize hScore; @synthesize parent; - (id)initWithPosition:(CGPoint)pos { if ((self = [super init])) { position = pos; gScore = 0; hScore = 0; parent = nil; } return self; } - (NSString *)description { return [NSString stringWithFormat:@"%@ pos=[%.0f;%.0f] g=%d h=%d f=%d", [super description], self.position.x, self.position.y, self.gScore, self.hScore, [self fScore]]; } - (BOOL)isEqual:(ShortestPathStep *)other { return CGPointEqualToPoint(self.position, other.position); } - (int)fScore { return self.gScore + self.hScore; } @end
正如你看到的那样,其内容非常直截了当.我们在这里重新定义了description方法,为的是更容易去调试,并且创建了一个isEqual方法,因为2个ShortestPathStep只有在它们的posititon相同时才相同(比如:它们表示同一个瓦块).
相关文章推荐
- 如何在Cocos2D游戏中实现A*寻路算法(一)
- 如何在Cocos2D游戏中实现A*寻路算法(一)
- 如何在Cocos2D游戏中实现A*寻路算法(一)
- A*寻路算法入门(二)
- cocos2d-x v3.9 与ActionInterval的孩子们之间的对话(2)
- cocos2d-x 植入广告(二) 有米横屏、插屏广告和积分墙广告
- 实习小白::(转) Cocostudio动画编辑器的使用
- 实习小白::(转) 【CocosBuilder 开发系列之一】cocos2dx使用CocosBuilder(官方编辑器)完成基础骨骼动画
- 实习小白::(转) 骨骼动画详解-Cocos Studio
- 实习小白::(转) 在cocos2d-x中使用CCArmature实现骨骼动画
- 实习小白::(转) Cocos2d-x 更改文字换行风格 ( cocos2dx change line )
- 实习小白::(转) Cocos2d-x 3.x 开发(十八)10行代码看自动Batch,10行代码看自动剔除 --------其实就是渲染图片机制
- 实习小白::(转) Cocos2d-x 3.0 开发(十七)使用Cocos2d-x 3.0rc,一条命令打包Android
- COCOS学习笔记之定时器
- 实习小白::(转) Cocos2d-x 3.0 开发(十六)cocos2dx-3.0beta版建立新项目并加载CocoStudio导出文件
- 实习小白::(转) Cocos2d-x 3.0 开发(十五)使用UILayout布局,制作对话界面
- 实习小白::(转) Cocos2d-x 3.0 开发(十四)使用UIScrollView 实现大小不同物品拖动展示
- 实习小白::(转) Cocos2d-x 3.0开发(十三)使用CocoStudio编辑帧事件并关联到程序
- 实习小白::(转) Cocos2d-x 3.0 开发(十二)在CocoStudio中使用粒子挂载与曲线动画
- Cocos2D将v1.0的tileMap游戏转换到v3.4中一例(八)