我有一个对象数组(objArray)和一系列动作(actArray)。两个数组都是有序的,我的意思是,objArray的索引0处的对象必须在actArray上执行动作0。
为了使解释清楚,让我们想象两个数组都有3个对象,obj0,obj1和obj2。
obj0 has to perform action 0 on actArray
obj1 has to perform action 1 on actArray
obj2 has to perform action 2 on actArray
这3个操作(或n,在阵列的情况下)必须同时发生。
当所有动画结束时,我需要调用方法animationsFinished。
我该怎么做?
我是在科科斯开始的。我google了一圈,没有找到任何实际的例子。我找到了CCSpan但是我没有看到它如何与多个对象一起使用,每个对象都有自己的动作。感谢。
答案 0 :(得分:2)
您可以按顺序执行操作,它们将同时发生。
int yourAnimationDuration; //this needs to be set to whatever your animation speed is
for(int idx = 0; idx < 3; idx++) {
[[objArray objectAtIndex:idx] runAction:[actArray objectAtIndex:idx]];
[self performSelector:@selector(someMethodToBeExecutedWhenAnimationFinishes) withObject:nil afterDelay:yourAnimationDuration];
}
或者如果你想让动画完成的方法只执行一次,就把它从for循环中取出来。
编辑:
id finalAnimation = [CCSequence actionOne:[actArray objectAtIndex:idx] two:someMethod];
这(我相信)将在第一个操作完成后执行您的方法。
答案 1 :(得分:1)
在上面提到的情况中,someMethodToBeExecutedWhenAnimationFinishes将被执行三次。只需在循环后使用一次。 'yourAnimationDuration'将是具有最长持续时间的动作时间,因为所有其他动作必须在具有最长持续时间的动作之前结束。
'CCSequence'是一个不错的选择,但您只需要使用具有最长持续时间的操作而不是所有对象来执行CCSequence。
所以在第一种情况下
int yourAnimationDuration; //this needs to be set to duration of action with maximum time
for(int idx = 0; idx < 3; idx++) {
[[objArray objectAtIndex:idx] runAction:[actArray objectAtIndex:idx]];
}
[self performSelector:@selector(someMethodToBeExecutedWhenAnimationFinishes) withObject:nil afterDelay:yourAnimationDuration];
在第二个案例中 假设您的第二个动作需要最长时间。
int actionNum = 2;
for(int idx = 0; idx < 3; idx++) {
if(idx == (actionNum-1))
{
[[objArray objectAtIndex:idx] runAction:[CCSequence actions:[actArray objectAtIndex:idx],[CCCallFuncN actionWithTarget:self selector:@selector(someMethodToBeExecutedWhenAnimationFinishes:)],nil];
}
else{
[[objArray objectAtIndex:idx] runAction:[actArray objectAtIndex:idx]];
}
}
希望有所帮助。我没有运行此代码。所以请检查语法(如果有)。 :)
答案 2 :(得分:1)
您可能需要此代码才能运行多个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 );