Setting up a game loop

Hey all,

Just started using cocos2d-x and I’m trying to figure out how to setup a simple game loop so I can setup a state machine.

Any help is greatly appreciated.

Cheers

You can see ‘void CCDisplayLinkDirector::mainLoop(void)’, this function is called every frame.
If you want a function be called very frame, you can implement ‘update()’ function in your subclass of CCLayer or use ‘CCScheduler::sharedScheduler()->scheduleSelector()’.

Awesome! Thank you!

I setup the virtual function virtual void update(ccTime dt);

I then called this->scheduleUpdate(); to setup the update interval.

I’ve just tried to use this~~>scheduleUpdate and it doesn’t work. My class is a subclass from CCLayer but `update` is never calls. I tried call sheduleUpdate from constructor and from init of my class. Both print “hello” but no one runs my update function.
I use the latest version of Cocos2d-x . Anyway I’ve done this:
this~~>schedule( schedule_selector( MyLayer::update ), 1.0 / 20 );
so it works. But I don’t know what happened to the previous method of using shedule’s update. You all tell to use `update( ccTimer dt )` but I use CCTimer (not ccTimer) cause ccTimer is undefined. Maybe that was my problem? Maybe the developers forgot to rename ccTimer to CCTimer in the last version of the Cocos? :slight_smile:

Currenty update use float not CCTimer so you should declare your update method like this:
virtual void update(float dt);
then scheduleUpdate() should work.

Leszek X wrote:

Currenty update use float not CCTimer so you should declare your update method like this:
virtual void update(float dt);
then scheduleUpdate() should work.

It’s strange but now it works with the float :slight_smile: I tried with the float too - it didn’t work before! But now it works. It’s all about magick :slight_smile:

I have another question :slight_smile:

@
void MyLayer::update( float dt ) {
CCLog( “update: %f”, dt );
}@

why I get everytime:

Cocos2d: update: 0.016667
Cocos2d: update: 0.016667
Cocos2d: update: 0.016667
Cocos2d: update: 0.016667
Cocos2d: update: 0.016667

I change FPS setting but nothing changes. I set FPS = 2 frames per second ( pDirector->setAnimationInterval; ) and I get 0.016667 again. 0.016667 stands always for any FPS I set. I thought the dt is the delta time from the last frame. Is it so? What do I do wrong? What does the dt mean here?

It prints two times per second (right!) but gives me that strange number (0.016667).

I’ve found some discussion on this subject: http://www.cocos2d-x.org/boards/6/topics/23288
“This float variable is time in seconds. If dt == 1.0f then it means that exactly 1 second has passed.”

So what am I doing wrong? :slight_smile:

I use Cocos2d-x (RC0) + MacOS 10.8 + iOS emulator.

I suppose the 0.016667 is 1/60. But why then I get printed “update:” two times per second only when dt shows me FPS=60? :slight_smile:

Airex Rest wrote:

I have another question :slight_smile:
>
@
void MyLayer::update( float dt ) {
CCLog( “update: %f”, dt );
}@
>
why I get everytime:
> Cocos2d: update: 0.016667
> Cocos2d: update: 0.016667
> Cocos2d: update: 0.016667
> Cocos2d: update: 0.016667
> Cocos2d: update: 0.016667
>
I change FPS setting but nothing changes. I set FPS = 2 frames per second ( pDirector->setAnimationInterval; ) and I get 0.016667 again. 0.016667 stands always for any FPS I set. I thought the dt is the delta time from the last frame. Is it so? What do I do wrong? What does the dt mean here?
>
It prints two times per second (right!) but gives me that strange number (0.016667).
>
I’ve found some discussion on this subject: http://www.cocos2d-x.org/boards/6/topics/23288
“This float variable is time in seconds. If dt == 1.0f then it means that exactly 1 second has passed.”
>
So what am I doing wrong? :slight_smile:
>
I use Cocos2d-x (RC0) + MacOS 10.8 + iOS emulator.
>
I suppose the 0.016667 is 1/60. But why then I get printed “update:” two times per second only when dt shows me FPS=60? :slight_smile:

Well. The problem was with the low values of FPS :slight_smile: It doesn’t work with FPS=2 but works fine with FPS=10 :slight_smile:

I tried calling scheduleUpdate() on Layer::init() and it causes error child!=0.

I override update already
void PlayState::update(float dt)
{
}

I’m using cocos2-x 3.0alpha1. Is there some changes on update implementation?

In Cocos2d-x 3.6, I had to call resume on my Layer. update would not get called otherwise. This is what I did:

  1. Implemented void update(float dt);
  2. Called these in OnEnter():
    this->scheduleUpdate();
    this->resume();

I’ve been porting my code from Objective-C Cocos2d 3.0 to Cocos2d-x C++ 3.6 and I just love writing in a language that makes sense again :slight_smile:

1 Like

“this->resume();” after calling “this->scheduleUpdate();” worked for me! Thanks, I was searching for an answer for along time!

Hi all. I just started cocos2d-x few days ago…
What is dt mean in update(float dt) method?

It’s how much time has passed since the last update call(in milliseconds).