Book Image

Cocos2d for iPhone 1 Game Development Cookbook

By : Nathan Burba
Book Image

Cocos2d for iPhone 1 Game Development Cookbook

By: Nathan Burba

Overview of this book

Cocos2d for iPhone is a robust but simple-to-use 2D game framework for iPhone. It is easy to use, fast, flexible, free, and Appstore approved. More than 2500 AppStore games already use it, including many best-seller games. Do you want to take your cocos2d game development skills to the next level and become more professional in cocos2d game design? Cocos2d for iPhone 1 Game Development Cookbook will help you reach that next level. You will find over 100 recipes here that explain everything from the drawing of a single sprite to AI pathfinding and advanced networking. Full working examples are emphasized. Starting with the first chapter, Graphics, you will be taken through every major topic of game development. You will find both simple and complex recipes in the book. Each recipe is either a solution to a common problem (playing video files, accelerometer steering) or a cool advanced technique (3D rendering, textured polygons). This cookbook will have you creating professional quality iOS games quickly with its breadth of working example code.
Table of Contents (15 chapters)
Cocos2d for iPhone 1 Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

1D and 2D Ease Actions


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.

Getting ready

Please refer to the project RecipeCollection01 for full working code of this recipe.

How to do it...

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

How it works...

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.