IOS_31_cocos2d_CCAction, ioscocos2d
IOS_31_cocos2d_CCAction
CCAction [action] is action
Perform operations such as movement, scaling, and rotation within a specific period of time,
CCNode can use the runAction method to [execute an action] to achieve the animation effect,
Each subclass of an action encapsulates different action effects.
Inheritance structure of CCAction
The other two very important subclasses are:
CCActionInstant and CCActionInterval ),
They contain a lot of sub-classes. Each sub-class corresponds to a specific action]
Note that CCAction and CCFiniteTimeAction are abstract classes.,
Only some basic attributes and methods are defined,
In actual use, you need to create its subclass action,
Then call the runAction method of CCNode to execute the required action, animation, and motion.
An interval action, also known as a time-consuming action, consumes a certain amount of time to complete an action.
Actions that take some time to complete,
All the interval actions are inherited from CCActionInterval.
For example, CCActionRotateBy, CCActionRotateTo, CCActionMoveBy, and CCActionMoveTo can rotate the specified angle within the specified time.
Note: By is the relative volume, and To is the absolute volume.
<Span style = "font-size: 18px;"> // clockwise rotation of 360 ° CCActionRotateBy * rotate = [CCActionRotateBy actionWithDuration: 1 angle:]; [sprite runAction: rotate]; </span>
Inheritance structure of the interval action: (the class names of cocos2d V3 have all been changed to the beginning of CCAction)
A subclass of common CCActionInterval:
1. CCActionBlink
Flashing Effect
<Span style = "font-size: 18px;"> // flashes 20 times in 5 seconds. CCActionBlink * blink = [CCActionBlink actionWithDuration: 5 blinks: 20]; [sprite runAction: blink]; </span>
2. CCActionMoveBy and CCActionMoveTo
CCActionMoveBy is to move a fixed distance (relative distance ),
CCActionMoveTo is to move to the specified location (absolute target location)
<Span style = "font-size: 18px;"> // within 1 second, 100 units are moved to the right, and 80 units are moved up simultaneously. CCActionMoveBy * moveBy = [CCActionMoveBy actionWithDuration: 1 position: ccp (100, 80)]; </span>
<Span style = "font-size: 18px;"> // within 1 second, move from the current position of the node to the Cartesian coordinate system (100, 80) this location is CCActionMoveTo * moveTo = [CCActionMoveTo actionWithDuration: 1 position: CGPointMake (100, 80)]; </span>
3. CCActionRotateBy and CCActionRotateTo
CCActionRotateBy is a fixed Rotation Angle Based on the current rotation angle,
CCActionRotateTo is rotated from the current rotation angle to the specified angle
Assume that the sprite has rotated CW clockwise 45 ° degree clockwise during initialization.
<span style="font-size:18px;">sprite.rotation = 45;</span>
If CCActionRotateBy is used
<Span style = "font-size: 18px;"> CCActionRotateBy * rotateBy = [CCActionRotateBy actionWithDuration: 1 angle: 90]; [sprite runAction: rotateBy]; // Rotate 90 ° clockwise within 1 second, so the final rotation angle of sprite is 45 ° + 90 ° = 135 ° </span>
If CCActionRotateTo is used
<Span style = "font-size: 18px;"> CCActionRotateTo * rotateTo = [CCActionRotateTo actionWithDuration: 1 angle: 90]; [sprite runAction: rotateTo]; // within 1 second, rotate clockwise for only 45 </span> <span style = "font-size: 18px; font-family: Arial, Helvetica, sans-serif; "> ° to </span> <span style =" font-size: 18px; "> 90 °, because the final rotation angle of the sprite is 90 ° </span>
4. CCActionScaleBy and CCActionScaleTo
CCActionScaleBy scales a certain percentage based on the current scaling ratio,
CCActionScaleTo scales from the current scale to the specified scale (relative to the original size)
Assume that the zooming ratio during initialization is 0.8.
<span style="font-size:18px;">sprite.scale = 0.8;</span>
If CCActionScaleBy is used
<Span style = "font-size: 18px;"> CCActionScaleBy * scaleBy = [CCActionScaleBy actionWithDuration: 1 scale: 0.5]; [sprite runAction: scaleBy]; // within 1 second, the width and height are reduced by 50%. Then, compared with the original size, the final scale ratio of sprite is 0.8*0.5 = 0.4 </span>
If CCActionScaleTo is used
<Span style = "font-size: 18px;"> CCActionScaleTo * scaleTo = [CCActionScaleTo actionWithDuration: 1 scale: 0.5]; [sprite runAction: scaleTo]; // within 1 second, the width and height are reduced to 0.5 times, so the final scale ratio of sprite is 0.5 </span>
5. CCActionFadeIn, CCActionFadeOut, and CCActionFadeTo
CCActionFadeIn is light-in, that is, from dark to bright, from not to there;
CCActionFadeOut is fade out, that is, from Bright to dark, from there to no;
CCActionFadeTo is used to modify the opacity of a Node.
Opacity 255 is completely opaque, that is, completely visible
<Span style = "font-size: 18px;"> // within 2 seconds, there is no CCActionFadeIn * fadeIn = [CCActionFadeIn actionWithDuration: 2]; // within 2 seconds, the opacity changes from there to no CCActionFadeOut * fadeOut = [CCActionFadeOut actionWithDuration: 2]; // within 2 seconds, the opacity changes to 120, that is, it becomes translucent (the opacity value range is 0-255) CCActionFadeTo * fadeTo = [CCActionFadeTo actionWithDuration: 2 opacity: 120]; </span>
6. CCActionRepeat
Repeats an action. You can specify the number of repetitions.
<Span style = "font-size: 18px;"> // rotate 360 ° CCActionRotateBy * rotateBy = [CCActionRotateBy actionWithDuration: 1 angle:] clockwise in one second. // execute the rotating animation CCActionRepeat * repeat = [CCActionRepeat actionWithAction: rotateBy times: 2]; [sprite runAction: repeat]; </span>
6. CCActionRepeatForever
CCActionRepeatForever directly inherits from CCAction,
You can keep runningInterval action (CCActionInterval).
If you want to keep the genie rotating, you can write as follows:
// Rotate 360 ° CCActionRotateBy * rotate = [CCActionRotateBy actionWithDuration: 1 angle:] clockwise in one second; // use the rotate to repeat the CCActionRotateBy action CCActionRepeatForever * repeat = [rotate actionWithAction: rotate] [sprite runAction: repeat];
It can also use CCActionRepeatForever
Repeats an action sequence (CCActionSequence)
// Change to red CCActionTintTo * tintTo1 = [CCActionTintTo actionWithDuration: 1 red: 255 green: 0 blue: 0]; // change to green CCActionTintTo * tintTo2 = [CCActionTintTo actionWithDuration: 1 red: 0 green: 255 blue: 0]; // Changes to blue CCActionTintTo * tintTo3 = [CCActionTintTo actionWithDuration: 1 red: 0 green: 0 blue: 255]; CCActionSequence * sequence = [CCActionSequence actions: tintTo1, tintTo2, tintTo3, nil]; CCActionRepeatForever * repeat = [Your actionWithAction: sequence]; [sprite runAction: repeat]
You will find that the color status of the genie is: red-> green-> blue-> Red ...,
Always switch between red, green, and blue colors in sequence
7. CCActionSpeed
CCActionSpeed is also directly inherited from CCAction,
It can affect the running speed of the interval action (inherited from the CCActionInterval action.
<Span style = "font-size: 18px;"> // clockwise rotate 360 ° CCActionRotateBy * rotate = [CCActionRotateBy actionWithDuration: 1 angle:] within 1 second; // The speed is half the original CCActionSpeed * speed = [CCActionSpeed actionWithAction: rotate speed: 0.5]; [sprite runAction: speed]; </span>
The rotation was completed in 1 second. After setting speed to 0.5, it takes 2 seconds to complete the rotation.
8. CCActionSequence
Generally, if you add several actions to a node at the same timeRun simultaneously.
For example, the following code is used to rotate and scale one side.
<span style="font-size: 18px;">CCActionRotateBy *rotateBy = [CCActionRotateBy actionWithDuration:1 angle:360];CCActionScaleBy *scaleBy = [CCActionScaleBy actionWithDuration:1 scale:2];[sprite runAction:rotateBy];[sprite runAction:scaleBy];</span>
However, sometimes we want to executeOne after another),
That is, let an action run after another operation is completed,
So we need to useCCActionSequence
The following shows how to change the sprite from red to green and then from green to blue.
<Span style = "font-size: 18px;"> CCActionTintTo * tintTo1 = [CCActionTintTo actionWithDuration: 1 red: 255 green: 0 blue: 0]; CCActionTintTo * tintTo2 = [CCActionTintTo actionWithDuration: 1 red: 0 green: 255 blue: 0]; CCActionTintTo * tintTo3 = [CCActionTintTo actionWithDuration: 1 red: 0 green: 0 blue: 255] // CCActionTintTo is also a subclass of CCActionInterval. It can be used to change the color of the genie. CCActionSequence * sequence = [CCActionSequence actions: tintTo1, tintTo2, tintTo3, nil]; [sprite runAction: sequence]; </span>
CCActionSequence performs all the actions passed in the parameters in sequence,
One after another
9. CCActionEase
When the CCActionMoveTo action is used for a node, it moves to the destination at a constant speed,
If CCActionEase is used, nodes can be created.From slow to fastOrFrom fast to slowTo the destination.
Therefore, CCActionEase is used to change the running speed of an action.
CCActionEase is a very powerful class,Very many subclassHere, only a few of them are described:
CCActionEaseIn: from slow to fast
CCActionEaseOut: From fast to slow
CCActionEaseInOut: first from slow to fast, then from fast to slow
For example:
<span style="font-size: 18px;">CCActionMoveTo *moveTo = [CCActionMoveTo actionWithDuration:4 position:ccp(300, 200)];CCActionEaseInOut *easeInOut = [CCActionEaseInOut actionWithAction:moveTo rate:5];[sprite runAction:easeInOut];</span>
You will see the genie
From slow to fast, then from fast to slow.
The rate parameter determines the obvious degree of speed change. It is valid only when it is greater than 1.
12. instantaneous action (the first is the interval time-consuming action)
CCActionInstant is an action that can be completed instantly,
It can be used to change the node location, flip the node to form an image, and set the nodeVisibility.
The following describes the inheritance structure of an instantaneous action:
You can set the visible attribute of a node instead
CCActionShow, CCActionHide, CCActionToggleVisibility
You can modify the position attribute of a node instead of CCActionPlace.
In fact, only when they often matchCCActionSequenceCombined Use
For example, if you want to move the CCActionMoveTo node to a certain position,
Hide the node after moving,
At this time, we can combine the CCActionMoveTo and CCActionHide actions.
Put in orderCCActionSequenceTo achieve the desired effect.
<Span style = "font-size: 18px;"> // move to (300,200) CCActionMoveTo * moveTo = [CCActionMoveTo actionWithDuration: 2 position: ccp (300,200)]; // hide the node CCActionHide * hide = [CCActionHide action]; CCActionSequence * sequence = [CCActionSequence actions: moveTo, hide, nil]; [sprite runAction: sequence]; </span>
Sometimes, in an action sequence (CCActionSequence,
After an action is completed,
First, call a method to perform some operations,
Then execute the next action.
Then you can combineCCActionCallBlock,CCActionCallFuncAndCCActionSequenceComplete this function.
For example, let the genie first change to red, then from red to green, and then from green to blue,
And after each color changeCall a methodPerform the following operations:
<Span style = "font-size: 18px;"> // red CCActionTintTo * tintTo1 = [CCActionTintTo actionWithDuration: 2 red: 255 green: 0 blue: 0]; // call the turnRed method CCActionCallFunc * fn1 = [CCActionCallFunc actionWithTarget: self selector: @ selector (turnRed)] after becoming red; // change to green CCActionTintTo * tintTo2 = [Export actionWithDuration: 2 red: 0 green: 255 blue: 0]; // call the turnGreen: Method of self after it turns green. The parameter is CCActionCallFunc * fn2 = [CCActionCallFunc actionWithTarget: self selector: @ selector (turnGreen :)]; // Changes to blue CCActionTintTo * tintTo3 = [CCActionTintTo actionWithDuration: 2 red: 0 green: 0 blue: 255]; // call the turnBlue method of self after it turns blue: the parameter is CCActionCallFunc * fn3 = [CCActionCallFunc actionWithTarget: self selector: @ selector (turnBlue :)]; // Finally, call turnDone: method required * fn4 = [CCActionCallFunc actionWithTarget: self selector: @ selector (turnDone :)]; CCActionSequence * sequence = [CCActionSequence actions: tintTo1, fn1, tintTo2, fn2, tintTo3, fn3, fn4, nil]; [sprite runAction: sequence]; </span>
The following is the implementation of the callback method:
<Span style = "font-size: 18px;">-(void) turnRed {NSLog (@ "changed to red ");} // node is the node that runs the current action-(void) turnGreen :( id) node {NSLog (@ "turns green: % @", node );} // node is the node that runs the current action-(void) turnBlue :( id) node {NSLog (@ "turns blue, % @", node);}-(void) turnDone :( id) node {NSLog (@ "transformed: % @", node) ;}</span>
You will find that the turnRed method will be called after the genie turns red,
When it turns green, turnGreen is called: method,
The turnBlue: method will be called first after it turns blue,
Finally, call turnDone: Method
13. CCActionAnimate
Frames can be animated by playing images in sequence.
For example, there are 10 images below:
If the image is displayed in sequence from 1.png to 10.png, a GIF animation is formed.
UseCCActionAnimateAnimation Effect
<Span style = "font-size: 18px;"> // create an Genie and center it. [remember addChild] _ sprite = [CCSprite spriteWithImageNamed: @ "1.png"]; _ sprite. position = ccp (self. contentSize. width/2, self. contentSize. height/2); // used to store all frames NSMutableArray * frames = [NSMutableArray array]; // load all images for (int I = 1; I <= 10; I ++) {// 1. name of the spliced image NSString * name = [NSString stringWithFormat: @ "ii.png", I]; // 2. Based on the image name (or full path) generate a texture. An Image corresponds to a texture object CCTextur. E * texture = [CCTexture textureWithFile: name]; CGRect retct = CGRectMake (0, 0, texture. contentSize. width, texture. contentSize. height); // 3. Create a Sprite frame based on the texture. CCSpriteFrame * frame = [[CCSpriteFrame alloc] initWithTexture: texture rectInPixels: retct rotated: NO offset: ccp (0, 0) originalSize: retct. size]; // 4. Add the sprite frame to the number of Sprite frames group [frames addObject: frame];} // create a CCAnimation based on the number of Sprite frames group. parameter: play the next image CCAnimation * an every 0.1 seconds Imation = [CCAnimation failed: frames delay: 0.1]; // create an action CCActionAnimate * animate = [CCActionAnimate actionWithAnimation: animation] based on the CCAnimation object; then * forever = [reply actionWithAction: animate]; [_ sprite runAction: forever]; // important ~~~ Must be a subnode of the scenario [self addChild: _ sprite]; </span>
The animation effect is as follows:
10 frames are divided into 10 different png images.
For the sake of performance, it is best to Pack 10 frames into one image,
Then, based on the coordinate parameters in the plist file, cut the image required for each frame from this image,
This image can be called a texture album ",
AvailableTexturePacker SoftwareCreate a texture album
IOS cocos2d-x draw a straight line how to make it as a callback function, that is, I want to draw him to execute
You can try this.
Add in header file
Array * _ touches;. cpp (can also be added to ccTouchEnded)
CcTouchBegan (Touch * touch, Event * event) {if (_ touches-> count ()> = 2) {_ touches-> removeAllObjects ();} _ touches-> addObject (touch);} draw () {cocos2d: Layer: draw (); if (_ touches-> count () = 2) {// draw a straight line }}
When a newbie learns cocos2d, he has a question about CCAction?
The situation you are talking about cannot appear unless you stop the action elsewhere.