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

AI flocking using Boids


By placing many more enemies in a game we begin to require some group-based AI. A popular algorithm used in both video games and films is the Boids algorithm. It simulates flocking behavior. In this recipe, we will create a large number of enemies who flock together and chase the player.

Getting ready

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

How to do it...

Execute the following code:

@interface Ch7_AIFlocking : Ch7_LineOfSight {}

/* CODE OMITTED */

@end

@implementation Ch7_AIFlocking

-(void) step:(ccTime)delta {
  [super step:delta];

  //Process the 'boids' flocking algorithm
  [self processBoids];
}

/* Make the flock of 'boids' follow the actor */
-(void) followActorWithEnemies {
  //All enemies constantly follow the actor
  for(int i=0; i<enemies.count; i++){
    //Align enemies
    GameActor *enemy = [enemies objectAtIndex:i];
    CGPoint directionVector = CGPointMake(actor.body->GetPosition().x - enemy.body-...