Schedule not working when switching scenes

I can’t for the life of me work out why, but as soon as I “replaceScene”, schedule functions seem to mysteriously not work in the scene I’ve just switched to!
I’ve read that placing them into the onEnter function solves the problem, but unfortunately it doesn’t for me. I’ve tried just about everything I can think of but I can’t get it working.

I’ve also read that people have experienced similar problems when their classes extended CCScene, my class is extending CCLayerColor.

I’ve also tried a different approach using CCDelayTime, CCCallFunc, and CCSequence, then calling runAction, but this doesn’t work either, am I missing something!?

FYI, all I’m doing at the moment is calling a very basic function and logging it when it is called, like so:

this->schedule(schedule_selector(MyNewScene::testMe), 1);

….

void MyNewScene::testMe(float dt) {
CCLog(“called…”);
}

This exact function in the very first scene that is loaded works fine, but doesn’t when I try to get it working in a new scene.
If anyone knows why this is, please take the time to let me know, I’m starting to lose my hair over this!

Thanks,
Tim

Try removing the second argument or using a smaller value, like 0.01. I believe the second argument is in seconds.

http://www.cocos2d-x.org/reference/native-cpp/d0/ded/classcocos2d_1_1_c_c_node.html#a201de3de18ef592a3dd7564bbe449117

Thanks for the response Lance.

You’re right, the argument is in seconds and that is what I am passing in, 1 second. I did try with 0.01, but unsurprisingly it didn’t work.
As I mentioned before, this code works perfectly in the first scene I load, but as soon as I switch to another scene via sharedDirector()->replaceScene(…), it no longer works.
As I also mentioned before, the approach using CCSequence also doesn’t work, which to me suggests a timer somewhere has been paused?

Anyone have any ideas? I’m sure it’s a quick fix and I’m missing something.

Thanks

You could try to unschedule it first and then schedule again in onEnter.

Thanks for the suggestion, but I think perhaps people are not understanding the problem I am experiencing.

The schedule call is not available to be unscheduled, because it hasn’t been scheduled by the time onEnter is called in the new scene. It is a brand new call to a completely different function, in a completely different class than the first scene. The schedule call that works in the first scene was only put in there as a test to see if it worked, which it did. After that I removed it since it wasn’t doing anything except logging that it was being called. Anyway, even if it was still written in, it shouldn’t effect a completely fresh schedule in a different class.

I’ll say it again in case people haven’t read my previous posts, CCSequence and runAction doesn’t seem to work either which to me suggests a timer has stopped or been paused somewhere when the scene is replaced.

To make it even clearer, this is essentially what I have:

Inside class MyFirstScene:

void MyFirstScene::myFunction() {
CCDirector::sharedDirector()->replaceScene(MySecondScene::scene());
}

Inside class MySecondScene:

void MySecondScene::onEnter() {
// this definitely gets called
this->schedule(schedule_selector(MySecondScene::testMe), 1);
}

void MySecondScene::testMe(float dt) {
// this does not get called!
}

MySecondScene::testMe should accept a float parameter.

Yes it does in my solution, the example above was purely to illustrate how I am going about it, perhaps I should’ve made that clearer, and in my very first post you will have noticed that I was passing in a float parameter. Any more ideas? This still isn’t working. The syntax is exactly correct, I’ve implemented this countless times in previous builds but for some reason, in this particular case, it doesn’t work

Please help! Someone must know how to fix this! Perhaps I am not setting something properly?!

Oh, i found the problem.
You should invoke *CCNode::onEnter()* before schedule.

void MySecondScene::onEnter() {
CCNode::onEnter();   // This code will set m_bRunning to true.

// this definitely gets called
this->schedule(schedule_selector(MySecondScene::testMe), 1);
}

The implementation of CCNode::schedule is

void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay)
{
    CCAssert( selector, "Argument must be non-nil");
    CCAssert( interval >=0, "Argument must be positive");

    m_pScheduler->scheduleSelector(selector, this, interval , repeat, delay, !m_bRunning);   // so if m_bRunning is false, schedule will not start.
}

Fantastic! Thanks very much, working fine now :slight_smile:

Spasibo! This works! Is it kind of magic? :wink: