Actions are lightweight classes that are used on nodes to perform certain, well, actions. They allow you to move, rotate, scale, tint, fade, and do a lot of other things with a node.
There are only three actions that directly derive from CCAction:
CCFollow (allows a node to follow another node)
CCRepeatForever (repeats an action indefinitely)
CCSpeed (changes the update frequency of an action while it is running)
eg: [label runAction:[CCFollow actionWithTarget:playerSprite]]; //to have a label follow the player character sprite
Interval Actions
在某个时间内作actioin.
// have myNode move to 100, 200 and arrive there in 3 seconds
CCMoveTo* move = [CCMoveTo actionWithDuration:3 position:CGPointMake(100, 200)];
[myNode runAction:move];
Action Sequences
Here’s how to cycle a label’s colors from red to blue to green:
CCTintTo* tint1 = [CCTintTo actionWithDuration:4 red:255 green:0 blue:0];
CCTintTo* tint2 = [CCTintTo actionWithDuration:4 red:0 green:0 blue:255];
CCTintTo* tint3 = [CCTintTo actionWithDuration:4 red:0 green:255 blue:0];
// actions only objects of interval actions.
CCSequence* sequence = [CCSequence actions:tint1, tint2, tint3, nil];
[label runAction:sequence];
Ease Actions
// I want myNode to move to 100, 200 and arrive there in 3 seconds
CCMoveTo* move = [CCMoveTo actionWithDuration:3 position:CGPointMake(100, 200)];
// this time the node should slowly speed up and then slow down as it moves
CCEaseInOut* ease = [CCEaseInOut actionWithAction:move rate:4];
[myNode runAction:ease];
Cocos2d implements the following CCActionEase classes:
CCEaseBackIn, CCEaseBackInOut, CCEaseBackOut
CCEaseBounceIn, CCEaseBounceInOut, CCEaseBounceOut
CCEaseElasticIn, CCEaseElasticInOut, CCEaseElasticOut
CCEaseExponentialIn, CCEaseExponentialInOut, CCEaseExponentialOut
CCEaseIn, CCEaseInOut, CCEaseOut
CCEaseSineIn, CCEaseSineInOut, CCEaseSineOut
Grid Actions
The downside is that the 3D effects may show visual artifacts unless you enable depth buffering, which requires more memory and has a negative impact on rendering performance, particularly on first- and second-generation devices.
Enable depth buffering:
//GL_DEPTH_COMPONENT16_OES for a 16-bit depth buffer or GL_DEPTH_COMPONENT24_OES for a //24-bit depth buffer
EAGLView *glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGB565
depthFormat:GL_DEPTH_COMPONENT16_OES];
Instant Actions