(NO.00005)iOS实现炸弹人游戏(九):游戏主角(二)

简介:

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


上篇介绍了游戏主角的初始化方法,下面我们一次来实现主角的其他方法,首先来看看runAnimation方法,我们使用这个方法来播放主角的动画:

-(void)runAnimation:(CCAnimation*)animation{
    if (_curAnimation == animation) {
        return;
    }

    _curAnimation = animation;
    if (_curAnimate) {
        [self stopAction:_curAnimate];
    }

    _curAnimate = [CCActionRepeatForever actionWithAction:
                   [CCActionAnimate actionWithAnimation:animation]];
    [self runAction:_curAnimate];
}

代码首先检查将要播放的动画是否和当前正在播放的动画相同,如果相同则直接退出.然后如果当前正在播放动画动作,则将其停止.

接下来为需要播放的动画创建一个永久循环的动作,然后运行该动作.

这里写图片描述

上图是玩家控制游戏主角在场景中自由行走所播放的各种动画,最后主角碰上怪物挂掉的动画也是如此.

主角类中还有一个重要的方法,应该还包括该游戏中所有怪物都需要这么一个方法,就是A*的移动算法.不熟悉A*算法的小伙伴可以到我翻译和原创的A*系列博文中观赏:

原创:如何在Cocos2D游戏中实现A*寻路算法(一)
翻译:A*寻路算法入门(一)

该方法就是moveTowardByAStar方法:

//使用A*寻路算法移动至目标坐标
-(void)moveTowardByAStar:(CGPoint)targetLocation{
    if (_currentStepAction) {
        _pendingMove = [NSValue valueWithCGPoint:targetLocation];
        return;
    }

    CGPoint fromTileCoord = [_mainScene tileCoordForPosition:self.position];
    CGPoint toTileCoord = [_mainScene tileCoordForPosition:targetLocation];

    if (CGPointEqualToPoint(fromTileCoord, toTileCoord)) {      return;
    }

    if (![_mainScene isWalkableTile:toTileCoord forRole:self]) {
        return;
    }

    _spOpenSteps = [NSMutableArray array];
    _spClosedSteps = [NSMutableArray array];
    _shortestPath = nil;

    [StarA insertStep:[[ShortestPathStep alloc] initWithPosition:fromTileCoord]
                                                                    toOpenList:_spOpenSteps];
    do{
        ShortestPathStep *currentStep = _spOpenSteps[0];
        [_spClosedSteps addObject:currentStep];
        [_spOpenSteps removeObjectAtIndex:0];
        if (CGPointEqualToPoint(currentStep.position, toTileCoord)) {
            //如果已经找到最短路径则完成该最短路径的移动动作
            [self constructPathAndStartAnimationFromStep:currentStep];
            _spOpenSteps = nil;
            _spClosedSteps = nil;
            break;
        }

        NSArray *adjSteps = [_mainScene walkableAdjacentTilesCoordDiagonallyForTileCoord:
                                                                currentStep.position forRole:self];

        for (NSValue *v in adjSteps) {
            ShortestPathStep *step = [[ShortestPathStep alloc]initWithPosition:[v CGPointValue]];
            if ([_spClosedSteps containsObject:step]) {
                continue;
            }

            int moveCost = [StarA costToMoveDiagonallyFromStep:currentStep toAdjacentStep:step];
            NSUInteger index = [_spOpenSteps indexOfObject:step];
            if (index == NSNotFound) {
                step.parent = currentStep;
                step.gScore = currentStep.gScore + moveCost;
                step.hScore = [StarA computeHScoreFromCoord:step.position toCoord:toTileCoord];
                [StarA insertStep:step toOpenList:_spOpenSteps];
            }else{//已经存在于开放列表
                step = _spOpenSteps[index];
                if ((currentStep.gScore + moveCost) < step.gScore) {
                    step.gScore = currentStep.gScore + moveCost;
                    step.parent = currentStep;
                    [_spOpenSteps removeObjectAtIndex:index];
                    [StarA insertStep:step toOpenList:_spOpenSteps];
                }
            }
        }
    }while (_spOpenSteps.count > 0);
}

其中比较长,这里就不详细说明了,大家需要的信息上面2个系列的博文完全涵盖了.有一点要指出的是,因为我也才开始写这类代码,所以一开始对类元素的把握也不是太强,按理说这个方法是所有游戏角色都需要的,所以应该放在它们的父类中.的确是这样,我在另一个RPG游戏<<熊猫之魂>>中正是这样做的,我们有机会再叙.

相关文章
|
安全 数据安全/隐私保护 iOS开发
iOS小技能:【发红包】使用tweak和lua脚本结合进行实现
我们开发的大部分越狱程序,都是编译成动态链接库(`例如:介绍的越狱程序(Tweak)开发,就是动态链接库。`),然后通过越狱平台的MobileSubstrate(iOS7上叫CydiaSubstrate)来加载进入目标程序(Target),通过对目标程序的挂钩(Hook),来实现相应的功能。
303 0
|
Android开发 iOS开发
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
369 0
iOS开发 - 商品详情页两种分页模式,只提供思路和实现方式。
|
存储 安全 iOS开发
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
424 0
iOS开发 - 继udid,Mac地址等一系列唯一标识无效后,如何用KeyChain来实现设备唯一性
|
Swift 数据安全/隐私保护 iOS开发
iOS开发 - swift通过Alamofire实现https通信
iOS开发 - swift通过Alamofire实现https通信
378 0
iOS开发 - swift通过Alamofire实现https通信
|
开发者 iOS开发
iOS开发 - 用AFNetworking实现https单向验证,双向验证
iOS开发 - 用AFNetworking实现https单向验证,双向验证
361 0
iOS开发 - 用AFNetworking实现https单向验证,双向验证
|
iOS开发
iOS小技能:自动布局实现兄弟控件N等分且宽高比例是1:N(xib 上实现)
本文为 iOS视图约束专题的第三篇:xib上使用自动布局教程
154 0
|
Linux iOS开发 开发者
实现在windows、linux下上传ios app到App Store
实现在windows、linux下上传ios app到App Store
实现在windows、linux下上传ios app到App Store
|
小程序 前端开发 Shell
接入 mPaaS 小程序并实现启动 iOS 版| 学习笔记
快速学习接入 mPaaS 小程序并实现启动 iOS 版。
610 0
接入 mPaaS 小程序并实现启动 iOS 版| 学习笔记
|
小程序 Shell 开发工具
接入 mpaas 小程序并实现启动 IOS 版|学习笔记
快速学习接入 mpaas 小程序并实现启动 IOS 版
210 0
接入 mpaas 小程序并实现启动 IOS 版|学习笔记
|
移动开发 JavaScript weex
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
232 0