Sunday, June 21, 2015

No more urgly game loop in Cocos2dx

   while(!done)
   {
      update(); // keep the game content moving
   }
The above code shows the very important mechanic in game development. Without the loop, the game will always static. But in Cocos2d-x framework, things have changed. I don't do this ugly looping anymore, I just need to override the update(float) in the Scene object like this:
class HelloWorld : public cocos2d::Layer
{
public:
   ...
   ...

   virtual bool init();

   void update(float d);
};

void HelloWorld::update(float d)
{
   ...
   ...
}
And then activate it in the init() like this:
bool HelloWorld::init()
{
   ...

   this->scheduleUpdate();

   return true;
}

No comments: