Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocos2d-x Schedule does not call function

It's very simple schedule code. AMS_Moving Inherited CCNode.

I call the action() in runAction() by schedule. But does not call action().

When I call action() directly, It's normal.

I want know the reason. please help me.

void AMS_Moving::runAction()
{
..
..
    this->schedule(schedule_selector(AMS_Moving::action));
}

void AMS_Moving::action(ccTime dt)
{
..
}
like image 309
Jin-Hwan Kim Avatar asked Nov 22 '25 03:11

Jin-Hwan Kim


1 Answers

I've run into a similiar issue in Cocos2dx-3.8, trying to schedule a selector in one Scene I was developing.

My malfunctioning code was:

void GameScene::onEnter() {
    this->scheduleOnce(CC_SCHEDULE_SELECTOR(GameScene::scheduledSelector), 3.0);
    this->scheduleUpdate();
}

void GameScene::scheduledSelector(float dt) {
    log("Scheduled selector called...");
}

void GameScene::update(float delta) {
    log("update called...");
}

Neither scheduledSelector nor update got called in this situation.

I found the solution to my problem in the Cocos2dx Node documentation: A Node's scheduler will not start scheduling updates until the Node calls its resume method:

/**
 * Resumes all scheduled selectors, actions and event listeners.
 * This method is called internally by onEnter.
 */
virtual void resume(void);

My bad was that I overrided Node's onEnter method without calling through the superclass implementation, so the scheduler never got a signal to start the updates. Fixing my onEnter method with:

void GameScene::onEnter() {
    Node::onEnter();
    this->scheduleOnce(CC_SCHEDULE_SELECTOR(GameScene::scheduledSelector), 3.0);
    this->scheduleUpdate();
}

did the job and both selectors began to be called at the right time.

like image 185
Qbeorama Avatar answered Nov 24 '25 23:11

Qbeorama



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!