Book Image

Cocos2d-X Game Development Blueprints

By : Karan Sequeira
Book Image

Cocos2d-X Game Development Blueprints

By: Karan Sequeira

Overview of this book

Table of Contents (17 chapters)
Cocos2d-x Game Development Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

The update loop


The update function for Inverse Universe will be similar to most other games where we update all the game elements and check for collisions. The code looks like this:

void GameWorld::update(float dt)
{
  // don't process if player is dying
  if(player_->is_dying_)
    return;

  // update each enemy
  CCObject* object = NULL;
  CCARRAY_FOREACH(enemies_, object)
  {
    Enemy* enemy = (Enemy*)object;
    if(enemy)
    {
      enemy->Update(player_->getPosition(), player_->GetShield() == NULL);
    }
  }

  // update each power-up
  object = NULL;
  CCARRAY_FOREACH(powerups_, object)
  {
    PowerUp* powerup = (PowerUp*)object;
    if(powerup)
    {
      powerup->Update();
    }
  }

  CheckCollisions();
  CheckRemovals();
}

We skip processing anything if the player's death animation is playing. We then iterate over the enemies_ and powerups_ arrays and update each object they contain. Finally, we check for collisions and for objects that must be removed. We will...