2

所以,我有一个基本的游戏场景:

一些单位靠近敌方单位,发射弹丸,然后调整敌方单位的生命。我的问题是我不确定如何安排这三个事件一个接一个地运行。如果所有这些操作都在同一个目标上完成,那么这将非常容易,但是有两个不同的目标。

这样做的最佳方法是什么?

代码如下所示:

Unit* unit = [self getActiveUnit];
Unit* enemy = [self getEnemyInRange:unit];

CGpoint A = unit.sprite.position;
CGPoint B = [self getPositionClose:enemy for:unit];

CCSequence* unitMove = [self generateUnitMoveFrom:A to:B];

Projectile* proj = [self generateProjectile];
CCSequence* projMove = [self generateProjMoveFrom:A to:B];

CCSequence* attackDone = [self generateAttackDoneFor:unit enemy:enemy];

// This is the part that i don't know how to do
// Execute these in order and sequentially, not at the same time
[unit.sprite runAction:unitMove];
[proj.sprite runAction:projMove];
[proj.sprite runAction:removeSprite];
[self runAction:attackDone];

做到这一点的最佳方法是什么?即使使用 CCActionManager 它似乎仍然相当复杂,因为我认为我必须在所有这些操作之间添加一个额外的回调才能恢复下一个目标的预定操作。

有任何想法吗?

谢谢!

4

4 回答 4

4

我会尝试使用CCSequence

[self runAction:[CCSequence actions:
  [CCCallFuncO actionWithTarget:unit.sprite selector:@selector(runAction:) object:unitMove],
  [CCCallFuncO actionWithTarget:proj.sprite selector:@selector(runAction:) object:projMove],
  [CCCallFuncO actionWithTarget:proj.sprite selector:@selector(runAction:) object:removeSprite],
  [CCCallFunc actionWithTarget:self selector:@selector(attackDone)],
                     nil]];
于 2012-06-14T14:29:06.530 回答
2

您可能需要此代码来运行 Multiple CCAction每个操作针对不同的 CCNode/CCSprite/...);但是按顺序开始!

CCMoveBy *moveUpAction = CCMoveBy::create(0.5,ccp(0,400));
CCMoveBy *moveRightAction = CCMoveBy::create(1,ccp(300,0));
CCMoveBy *moveDownAction = CCMoveBy::create(1,ccp(0,-400));

CCTargetedAction *mm = CCTargetedAction::create(someNode_1,moveUpAction);
CCTargetedAction *rr = CCTargetedAction::create(someNode_2,moveRightAction);
CCTargetedAction *dd = CCTargetedAction::create(someNode_3,moveDownAction);

// Here we first run 'moveUpAction' on someNode_1.
// After finishing that Action we start 'moveRightAction' on someNode_2
// After finishing the Action, we start ...
CCSequence *targetedSeq = CCSequence::create(mm,rr,dd,NULL);
whateverNode->runAction(targetedSeq );
于 2014-07-13T07:25:26.090 回答
1

you can create an array of your actions, then scedule some method to call them one by one. For example

- (void) playManyActionsOneByOne
{
    // create some actions and add them to the
    // mutable array m_actionsContainer

    [self runNextActionInArray];
}

- (void) runNexActionInArray
{
    if( [m_actionsContainer count > 0] )
    {
        id nextAction = [m_actionsContainer objectAtIndex:0];
        id callback = [CCCallFunc actionWithTarget: self selector: @selector(runNextActionInArray)];
        id sequence = [CCSequence actionOne: nextActon two: callback];
        [neededNode runAction: sequence];

        [m_actionsContainer removeObjectAtIndex:0];
    }    
}

it will run actions one by one and you even can add actions to your array while other actions aren't done yet.

于 2012-06-14T15:37:58.940 回答
1

同意詹姆斯。另外,如果您想在其他操作运行之前多花一点时间,您可以添加延迟...

[CCDelayTime actionWithDuration:0.5];

于 2012-06-14T14:35:37.510 回答