Original article, reproduced please indicate the source: http://blog.csdn.net/zhy_cheng/article/details/8271154
The animation provided by the Cocos2d-x is mainly instantaneous animation and delayed animation.
An instantaneous action is an action completed immediately without time. The common base class of the instantaneous action is ccactioninstant.
A delayed action takes some time to complete. Therefore, almost all delayed actions use execution time as the first parameter, and they share a common base class ccactioninterval.
1. Instantaneous Animation
I implement x flip and Y flip the source image. Use the following code to implement X and Y flip.
Ccsprite * s = ccsprite: Create ("iconccc.png"); // create the genie S-> setposition (CCP (200,200); // set the position addchild (s ); // Add ccactioninstant * IX = ccflipx: actionwithflipx (1); // X flip ccactioninstant * Iy = ccflipy: actionwithflipy (1 ); // y flip S-> runaction (IX); // run S-> runaction (Iy );
Running effect:
2. There is a simple class naming rule for delayed Animation:
Ccxxxxto-absolute action. The execution result is not closely related to the current status;
Ccxxxxby-relative action: executes an action in the current state. The execution result is closely related to the current state.
Move to-ccmoveto
Mobile-ccmoveby
Jump to-ccjumpto
The parameter specifies the destination position, jump height, and number of hops.
Skip-ccjumpby
Betel curve-ccbezierby
The three-time besell curve is supported: P0-start point, P1-start point tangent direction, P2-end point tangent direction, and P3-end point.
First set the Bessert parameter and then execute.
Zoom in to-ccscaleto
Zoom in-ccscaleby
If the parameter is decimal, It is reduced.
Rotate to-ccrotateto
Rotate-ccrotateby
Flashing-ccblink
Tone changed to-cctintto
Tone conversion-cctintby
Darken to-ccfadeto
From non-Brightening-ccfadein
From bright to non-ccfadeout
CCActionInterval *ac=CCRotateBy::actionWithDuration(2,270);s->runAction(ac);
Rotate 270 degrees.
There are also some composite animations
Ccsequence
Sequence usage is very simple. This class is derived from ccactioninterval and can be executed by the ccnode object itself. This class is used to linearly arrange several actions and then execute them one by one in order.
CCActionInterval *ac=CCRotateBy::actionWithDuration(2,270);CCActionInterval *bc=CCBlink::actionWithDuration(3,10);CCActionInterval *mc=CCMoveTo::actionWithDuration(2,CCPoint(300,200));CCActionInterval *jc=CCJumpTo::actionWithDuration(3,CCPoint(400,400),20,4);CCFiniteTimeAction *se=CCSequence::actions(ac,bc,mc,jc,NULL);s->runAction(se);
The preceding code is executed once.
CcspawnThe action itself must be executed simultaneously, such as mobile flip, color change, scaling, and so on. Note that the final completion time of synchronization execution is determined by the time spent in the basic action.
CCActionInterval *ac=CCRotateBy::actionWithDuration(2,270);CCActionInterval *sc=CCScaleBy::actionWithDuration(2,2,2);CCFiniteTimeAction *se=CCSpawn::actions(ac,sc,NULL);s->runAction(se);
The above animation is executed simultaneously.
CcrepeatCcrepeat is used to repeat an action for a limited number of times.
CCActionInterval *sc=CCScaleBy::actionWithDuration(2,2,2);CCFiniteTimeAction *se=CCSpawn::actions(ac,sc,NULL);CCActionInterval *rc=CCRepeat::actionWithAction(se,4);
The above code is executed four times
ReverseAction is to reverse (reverse) an action to execute an action, supporting the reactionary action sequence for the action sequence. Reactionary action is not a special class, but an interface introduced by ccfinitetimeaction. Not all classes support reactionary actions. The ccxxxxto class usually does not support reactionary actions, but the ccxxxxby class is generally supported. The example is as follows:
CCActionInterval *ac=CCRotateBy::actionWithDuration(2,270);CCActionInterval *sc=CCScaleBy::actionWithDuration(2,2,2);CCFiniteTimeAction *se=CCSpawn::actions(ac,sc,NULL);CCFiniteTimeAction *rrc=se->reverse();CCFiniteTimeAction *lc=CCSequence::actions(se,rrc,NULL);
The above animation will be restored.
CcdelaytimeWith ccdelaytime, we can add a time interval in the action sequence.
CCActionInterval *ac=CCRotateBy::actionWithDuration(2,270);CCActionInterval *sc=CCScaleBy::actionWithDuration(2,2,2);CCFiniteTimeAction *se=CCSpawn::actions(ac,sc,NULL);CCFiniteTimeAction *rrc=se->reverse();CCFiniteTimeAction *lc=CCSequence::actions(se,CCDelayTime::actionWithDuration(3),rrc,NULL);
Cccallfunc
During the running process, you can also add a function call function with no return value or parameters.
CCActionInterval *ac=CCRotateBy::actionWithDuration(2,270);CCActionInterval *sc=CCScaleBy::actionWithDuration(2,2,2);CCFiniteTimeAction *se=CCSpawn::actions(ac,sc,NULL);CCFiniteTimeAction *rrc=se->reverse();CCFiniteTimeAction *func=CCCallFunc::actionWithTarget(this,callfunc_selector(HelloWorld::backFunc));CCFiniteTimeAction *lc=CCSequence::actions(se,func,CCDelayTime::actionWithDuration(3),rrc,NULL);
Call the function after the se animation is completed.
Cccallfuncn
The function has no return value. It has a ccnode * pointer.
CCSprite *s=CCSprite::create("sprite.png");s->setPosition(ccp(0,0));addChild(s);CCActionInterval *move=CCMoveBy::create(3,ccp(450,290));CCFiniteTimeAction *fn=CCCallFuncN::actionWithTarget(this,callfuncN_selector(HelloWorld::funcN));CCFiniteTimeAction *a=CCSequence::actions(move,fn,NULL);s->runAction(a);
The callback function is
void HelloWorld::funcN(CCNode *pSender){CCLog("funcN");CCSprite *s=(CCSprite*)pSender;CCLog("x=%f,y=%f",s->getPosition().x,s->getPosition().y);}
Through testing, the passed psender is actually a pointer to the genie that runs the function.
Cccallfuncnd
When calling this function, you can pass a void * parameter.
Use:
CCSprite *s=CCSprite::create("sprite.png");s->setPosition(ccp(0,0));addChild(s);CCActionInterval *move=CCMoveBy::create(3,ccp(450,290));CCFiniteTimeAction *fn=CCCallFuncND::actionWithTarget(this,callfuncND_selector(HelloWorld::funcN),(void*)7);CCFiniteTimeAction *a=CCSequence::actions(move,fn,NULL);s->runAction(a);
The callback function is:
void HelloWorld::funcN(CCNode *pSender,void* data){CCLog("funcN");CCSprite *s=(CCSprite*)pSender;CCLog("x=%f,y=%f,*data=%d",s->getPosition().x,s->getPosition().y,int(data));}
In this way, the transmitted data can be obtained.
Cccallfunco
This function is similar to cccallfuncn, except that the callback function parameter is of the ccobject * type.