COCOS2D API 之 CompositionAction
Actions: Composition
There are some actions that let you compose actions.
-
Sequence action
-
Spawn action
-
Repeat action
-
RepeatForever action
Sequence
The CCSequence
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]];
The action1
will be executed first. When action1
finishes, then action2
will be executed. And whenaction2
finishes, only then action3
will be executed.
*IMPORTANT:* The inner actions should have a non-infinite time (e.g.: You can't add a CCRepeatForever
action in a CCSequence
action).
Spawn
The CCSpawn
action lets you run several actions at the same time. The duration of the CCSpawn
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];
Repeat
The CCRepeat
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];
RepeatForever
The CCRepeatForever
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 valid CCIntervalAction
. You can't use a CCRepeatForever
inside a CCSequence
action.
posted on 2012-03-02 16:57 jackbutler 阅读(164) 评论(0) 编辑 收藏 举报