CCTimer Help


#1

I am trying to start a timer, that I have as a public member variable

 cocos2d::CCTimer _growingTimer;

_growingTimer = cocos2d::CCTimer::timerWithTarget(this, schedule_selector(Broccoli::plantStoppedGrowing), 1);

But I am getting:

Unknown type name 'SEL_SCHEDULE'; did you mean 'cocos2d::SEL_SCHEDULE'?

and

No matching function for call to 'timerWithTarget'

But even adding a

cocos2d::

to the

schedule_selector()

doesn’t help

Does anyone have a working example? I just need to run the timer once and check it periodically to see how far in it is.


#2
  1. Make sure the “this” is a CCObject.
  2. Try adding USING_NS_CC before the definitions.

#3

Make sure to schedule the same class function.


#4

Hi Lance,

Good Ideas, that made things compile.

Do you start a timer with >update?
I did:
<pre>*growingTimer = cocos2d::CCTimer::timerWithTarget, 10);
*growingTimer
>update(1.0);

void Broccoli::plantStoppedGrowing()
{
std::cout << “Broccoli Stopped Growing” << std::endl;
}

Hoping that in 10 seconds I would see my
std::cout

#5

Touch Coder,

What do you mean?


#6

I haven’t used CCTimer but it seems that the 3rd parameter would be time needed to wait until the selector is called.
No need to call update().

You could also do this without using CCTimer.

this -> scheduleOnce( schedule_selector( Brocolli::plantStoppedGrowing ) /* method to invoke */, 10 /* delay */ );

#7

Hi Lance,

I am still experiencing some issues.

I tried:

this -> scheduleOnce( schedule_selector( Brocolli::plantStoppedGrowing ) /* method to invoke */, 10 /* delay */ );

But realized that I am not sure I can use scheduleOnce as this class subclasses CCObject (no real reason, it doesn’t beed to, but thought it might be handy later on)

I also saw you replied to another post about delaying/scheduling here: http://www.cocos2d-x.org/boards/6/topics/21647

Can you provide some pointers on what I can so?

Basically the user clicks on a CCMenu item (in this case Broccoli) and after then click it I want to run Brocolli::plantStoppedGrowing after a set amount of time.


#8

Try changing it to CCNode.


#9

I did that, but the nothing fires after 10 seconds. I even have a std::cout in the function being called to verify.

Here is the code:

 this->scheduleOnce(schedule_selector(Broccoli::plantStoppedGrowing ) /* method to invoke */, 10 /* delay */ );
    std::cout << "Past scheduleOnce" << std::endl;

When it hits that I see on the console:

planting broccoli
Past scheduleOnce

In the plantStoppedGrowing() I have:

void Broccoli::plantStoppedGrowing()
{
    std::cout << "Broccoli Stopped Growing" << std::endl;
}

but if I tap the menu item again I do see on the console:

Cocos2d: CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0.0000 to 0.0000

#10

I’m still not figuring this out. Does anyone have any advice?


#11

after some work today, using some C++11 threading works well for this.