2
votes

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)
{
..
}
6
Same problem here how to fix bro ? - ColdSteel

6 Answers

4
votes

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.

3
votes

Try this code:

CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(AMS_Moving::action),this,1.0f,false);
1
votes

Are you sure the AMS_Moving::runAction() getting called or CCNode::runAction() getting called? try to avoid using the same function name with cocos2d-x and see what happens.

1
votes

Solution 1:

CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(Foo::Updater),this,2.0f,false);

One of interesting advantage of this solution is that your scheduler will works independent of scene replacements during game. you just need to retain the Foo's object instead of adding it to some CCLayer or CCScene that may be deleted later.

Solution 2:

  • Use this signature for your member-function: void Foo::Updater(float dt)
  • Call this somewhere in Foo functions to start scheduler this->schedule(schedule_selector(Foo::Updater),2.0f);
  • In this case You must addChild() the object of Foo class to some CCLayer in order to make scheduler really call your Updater function.

--

Presumption:

  1. Cocos2dx v2.2.3
  2. Foo is the class in which you are trying to run some scheduler in it.
0
votes

Try set breakpoints in function 'AMS_Moving::action()'. This code will obviously call 'action'.

0
votes

Just remove ccTime dt from this function AMS_Moving::action. it will work