Ease Actions allow you to fine tune the actions used in your game using a number of formulae. They can be applied to any action: moving, scaling, fading, and so on. Regarding movement specifically, a small tweak can be applied to allow for independent Easing on both the X and the Y axis. This can be used to create a number of cool effects.
Execute the following code:
@interface CCMoveByCustom : CCMoveBy {} -(void) update: (ccTime) t; @end @implementation CCMoveByCustom -(void) update: (ccTime) t { //Here we neglect to change something with a zero delta. if(delta.x == 0){ [target_ setPosition:ccp( [(CCNode*)target_ position].x, (startPosition.y + delta.y*t ) )]; }else if(delta.y == 0){ [target_ setPosition:ccp( (startPosition.x + delta.x*t ), [(CCNode*)target_ position].y )]; }else{ [target_ setPosition:ccp( (startPosition.x + delta.x*t ), (startPosition.y + delta.y * t ) )]; } } @end @implementation Ch1_EasingActions -(CCLayer*) runRecipe { /*** 1D Movement Ease Action ***/ //Create the basic action to move by a certain X and Y vector CCActionInterval *action1D = [CCMoveBy actionWithDuration:2 position:ccp(200,200)]; //Create a sprite to move CCSprite *spriteEase1D = [CCSprite spriteWithFile:@"colorable_sprite.png"]; [spriteEase1D setPosition:ccp(150,50)]; [self addChild:spriteEase1D z:1 tag:TAG_SPRITE_EASE_1D]; //Create an 'eased' movement action with a CCEase class CCActionInterval *easeAction1D = [CCEaseInOut actionWithAction:action1D rate:2]; //Run the action [spriteEase1D runAction:easeAction1D]; /*** 2D Movement Ease Action ***/ //Create two movement actions, one in each dimension CCActionInterval *action2DX = [CCMoveByCustom actionWithDuration:2 position:ccp(200,0)]; CCActionInterval *action2DY = [CCMoveByCustom actionWithDuration:2 position:ccp(0,200)]; //Create a sprite to move CCSprite *spriteEase2D = [CCSprite spriteWithFile:@"colorable_sprite.png"]; [spriteEase2D setPosition:ccp(150,50)]; [self addChild:spriteEase2D z:1 tag:TAG_SPRITE_EASE_2D]; //Create two 'eased' movement actions, one on each dimension CCActionInterval *easeAction2DX = [CCEaseSineIn actionWithAction:action2DX]; CCActionInterval *easeAction2DY = [CCEaseBounceIn actionWithAction:action2DY]; //Run both actions [spriteEase2D runAction:easeAction2DX]; [spriteEase2D runAction:easeAction2DY]; return self; } @end
Upon executing this code you should see one character moving in a straight line toward the destination while the other moves there in a seemly erratic yet calculated way.
Uses of 2D eased movement actions :
While creating the iOS game GoldenAgeBaseball this summer I used
CCMoveByCustom
to simulate different pitches. A slider moved down and away, a cutter only away and a sinker only down. This variation of pitch styles was crucial to the development of the pitching/batting gameplay mechanic.Overall, Ease Actions give your game a polished and professional feel. Whether you are smoothing out a camera movement or simulating a baseball pitch, Ease Actions are a fine tool to help tweak your game to perfection.