COCOS2D API 之 CompositionAction
There are some actions that let you compose actions.
TheCCSequence
action is a list of actions. The actions are executed in the order that they are defined.
Example:
id action1 = [CCMoveTo actionWithDuration:2 position:ccp(100,100)]; id action2 = [CCMoveBy actionWithDuration:2 position: ccp(80,80)]; id action3 = [CCMoveBy actionWithDuration:2 position: ccp(0,80)]; [sprite runAction: [CCSequence actions:action1, action2, action3, nil]];
Theaction1
will be executed first. Whenaction1
finishes, thenaction2
will be executed. And whenaction2
finishes, only thenaction3
will be executed.
*IMPORTANT:* The inner actions should have a non-infinite time (e.g.: You can't add aCCRepeatForever
action in aCCSequence
action).
TheCCSpawn
action lets you run several actions at the same time. The duration of theCCSpawn
action will be the duration of the longest sub-action.
id action = [CCSpawn actions: [CCJumpBy actionWithDuration:2 position:ccp(300,0) height:50 jumps:4], [CCRotateBy actionWithDuration: 2 angle: 720], nil]; [sprite runAction:action];
TheCCRepeat
action lets you repeat an action a limited number of times. Example:
id a1 = [CCMoveBy actionWithDuration:1 position:ccp(150,0)]; id action1 = [CCRepeat actionWithAction: [CCSequence actions: [CCPlace actionWithPosition:ccp(60,60)], a1, nil] times:3]; [sprite runAction:action1];
TheCCRepeatForever
action is a special action. Since it will repeat an action forever, its duration can't be measured.
Example:
id a1 = [CCMoveBy actionWithDuration:1 position:ccp(150,0)]; id action2 = [CCRepeatForever actionWithAction: [CCSequence actions: [[a1 copy] autorelease], [a1 reverse], nil] ]; [sprite runAction:action2];
*IMPORTANT*:CCRepeatForever
is not a validCCIntervalAction
. You can't use aCCRepeatForever
inside aCCSequence
action.