2012-02-07 78 views
1

我有一個對象數組(objArray)和一個數組操作(actArray)。這兩個數組都是按順序排列的,我的意思是,在objArray的索引0處的對象必須在actArray上執行操作0。Cocos2D iPhone - 在多個目標上運行操作+回調

爲了讓解釋清楚,可以想象兩個數組都有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,在數組的情況下)必須同時發生。

當所有動畫結束時,我需要方法動畫完成調用。

我該怎麼做?

我開始在科科斯。我一直在搜索,並沒有找到任何實際的例子。我找到了CCSpan,但我沒有看到它如何與多個對象一起使用,並且每個對象都有自己的操作。謝謝。

+0

是你想要在obj1上調用動作2的錯字嗎?那應該是行動1? – 2012-02-07 02:12:56

+0

對不起,這是一個錯字。 – SpaceDog 2012-02-07 02:19:37

回答

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]; 

這(我相信)一旦第一個動作完成之後將執行你的方法。

+0

是的,我在考慮回調機制,但我認爲這將是最好的選擇。謝謝 – SpaceDog 2012-02-07 02:45:35

+0

我用另一種可能性編輯了我的答案。 – 2012-02-07 03:06:09

+0

至於'performSelector:withObject:afterDelay:' - 最好避免這種情況,因爲cocos2d有自己的調度器(可以暫停/恢復)。 – brigadir 2012-02-07 07:10:15

1

在上面的例子中你提到過,有些方法要執行動畫完成時會執行三次。在循環後一次使用它。 '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]]; 
} 
} 

希望有所幫助。我沒有運行這個代碼。因此,檢查是否有語法。 :)

+0

謝謝!!!!!!!!! – SpaceDog 2012-02-08 15:18:02

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);