(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游戏<<熊猫之魂>>中正是这样做的,我们有机会再叙.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值