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

Animating a texture-filled polygon


The TexturedPolygon can also be easily animated. This is useful for animated crowds, ocean waves, bubbling lava pits, and so on. In the example we see an animated field of wheat.

Getting ready

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

How to do it...

Execute the following code:

#import "Vector3D.h"
#import "TexturedPolygon.h"

@implementation Ch1_AnimateTexturedPolygon

-(CCLayer*) runRecipe {
  CGSize s = [[CCDirector sharedDirector] winSize];
        
  ccTexParams params = {GL_NEAREST, GL_NEAREST_MIPMAP_NEAREST, GL_REPEAT,GL_REPEAT};
  
  //Create grass animated textured polygon
  CGPoint grassVertexArr[] = { ccp(0,0), ccp(480,0), ccp(480,320), ccp(0,320) };
  int grassNumVerts = 4;
  NSMutableArray *grassVertices = [[NSMutableArray alloc] init];
  for(int i=0; i<grassNumVerts; i++){
    [grassVertices addObject:[NSValue valueWithCGPoint:ccp(grassVertexArr[i].x*1, grassVertexArr[i].y*1)]];
  }

  TexturedPolygon *grassPoly = [TexturedPolygon createWithFile:@"grass_tile_01.png" withVertices:grassVertices];
  [grassPoly.texture setTexParameters:&params];
  grassPoly.position = ccp(32,32);
  [self addChild:grassPoly z:1 tag:1];

  //Create swaying grass animation
  NSMutableArray *grassAnimFrames = [NSMutableArray array];
  
  //This is a two part animation with 'back' and 'forth' frames
  for(int i=0; i<=6; i++){
    [grassPoly addAnimFrameWithFile:[NSString stringWithFormat:@"grass_tile_0%d.png",i] toArray:grassAnimFrames];
  }
  for(int i=5; i>0; i--){
    [grassPoly addAnimFrameWithFile:[NSString stringWithFormat:@"grass_tile_0%d.png",i] toArray:grassAnimFrames];
  }

  CCAnimation *grassAnimation = [[CCAnimation alloc] initWithName:@"grass_tile_anim" delay:0.1f];
  for(int i=0; i<[grassAnimFrames count]; i++){
    [grassAnimation addFrame:[grassAnimFrames objectAtIndex:i]];
  }
  
  CCActionInterval *grassAnimate = [CCSequence actions: [CCAnimate actionWithAnimation:grassAnimation restoreOriginalFrame:NO], 
    [CCDelayTime actionWithDuration:0.0f], nil];
  CCActionInterval *grassRepeatAnimation = [CCRepeatForever actionWithAction:grassAnimate];
  [grassPoly runAction:grassRepeatAnimation];
  
  return self;
}

@end

How it works...

By dynamically changing the texture using CCAnimation we can create very simple tiled animation. The only extra cost of this operation is the extra space allocated for each frame of the animation.