stack of actions for one sparite in cocos2d

2019-08-17 18:59发布

问题:

I've got a question: My games hero can jump (CCJumpTo) and move to forward and backward direction (CCMoveTo).

I faced with problem: if game recived a command from user to move the hero, but in this time hero is jumping (CCJumpTo), how can I do this command after the end of jumping. (can I use some stack of actions for one sparite in cocos2d)

I can't use immediately [self.sprite stopAllActions]; to begin CCMoveTo because the hero have to finish the jump action and if I use CCMoveTo the action CCJumpTo will not finish. Hero can't jump and start to move to forward and backward at height.

sorry for my English

回答1:

For example, you can store your stack of actions in array, then run action like this

- (void) runNextAction
{
    id actionFromStack = [stack objectAtIndex: 0];
    [stack removeObjectAtIndex: 0];
    id callback = [CCCallFunc actionWithTarget: self selector: @selector(runNextAction)];
    id sequence = [CCSequence actionOne: actionFromStack two: callback];
    [self runAction: sequence];
}