Cocos2d-x调度不调用函数

时间:2021-05-27 14:36:29

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

这是非常简单的调度代码。AMS_Moving CCNode继承。

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

我按计划调用runAction()中的action()。但是不调用action()。

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

当我直接调用action()时,它是正常的。

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 个解决方案

#1


3  

Try this code:

试试这段代码:

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

#2


2  

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

我在Cocos2dx-3.8中遇到了类似的问题,试图在我正在开发的一个场景中安排一个选择器。

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.

在这种情况下,没有调用scheduledSelector或update。

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:

我在Cocos2dx节点文档中找到了我的问题的解决方案:在节点调用其resume方法之前,节点的调度器不会开始调度更新:

/**
 * 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:

我的缺点是没有通过超类实现调用就覆盖了节点的onEnter方法,所以调度器从来没有收到启动更新的信号。用以下方法固定我的onEnter方法:

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


1  

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.

您确定要调用AMS_Moving: runAction()还是要调用CCNode:::runAction() ?尽量避免在cocos2d-x中使用相同的函数名,看看会发生什么。

#4


1  

Solution 1:

解决方案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.

这个解决方案的一个有趣的优点是,您的调度程序将独立于游戏期间的场景替换。您只需保留Foo的对象,而不是将其添加到稍后可能被删除的CCLayer或CCScene中。

Solution 2:

解决方案2:

  • Use this signature for your member-function: void Foo::Updater(float dt)
  • 使用此签名为您的成员函数:void Foo::Updater(float dt)
  • Call this somewhere in Foo functions to start scheduler this->schedule(schedule_selector(Foo::Updater),2.0f);
  • 在Foo函数中调用这个来启动调度器这个->调度(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.
  • 在这种情况下,您必须将Foo类的对象添加到一些CCLayer中,以便使调度器真正调用您的更新函数。

--

- - -

Presumption:

假设:

  1. Cocos2dx v2.2.3
  2. Cocos2dx v2.2.3
  3. Foo is the class in which you are trying to run some scheduler in it.
  4. Foo是您试图在其中运行调度程序的类。

#5


0  

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

尝试在函数“AMS_Moving::action()”中设置断点。这段代码显然会调用“action”。

#6


0  

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

只需从这个函数AMS_Moving: action中删除ccTime dt。它将工作

#1


3  

Try this code:

试试这段代码:

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

#2


2  

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

我在Cocos2dx-3.8中遇到了类似的问题,试图在我正在开发的一个场景中安排一个选择器。

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.

在这种情况下,没有调用scheduledSelector或update。

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:

我在Cocos2dx节点文档中找到了我的问题的解决方案:在节点调用其resume方法之前,节点的调度器不会开始调度更新:

/**
 * 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:

我的缺点是没有通过超类实现调用就覆盖了节点的onEnter方法,所以调度器从来没有收到启动更新的信号。用以下方法固定我的onEnter方法:

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


1  

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.

您确定要调用AMS_Moving: runAction()还是要调用CCNode:::runAction() ?尽量避免在cocos2d-x中使用相同的函数名,看看会发生什么。

#4


1  

Solution 1:

解决方案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.

这个解决方案的一个有趣的优点是,您的调度程序将独立于游戏期间的场景替换。您只需保留Foo的对象,而不是将其添加到稍后可能被删除的CCLayer或CCScene中。

Solution 2:

解决方案2:

  • Use this signature for your member-function: void Foo::Updater(float dt)
  • 使用此签名为您的成员函数:void Foo::Updater(float dt)
  • Call this somewhere in Foo functions to start scheduler this->schedule(schedule_selector(Foo::Updater),2.0f);
  • 在Foo函数中调用这个来启动调度器这个->调度(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.
  • 在这种情况下,您必须将Foo类的对象添加到一些CCLayer中,以便使调度器真正调用您的更新函数。

--

- - -

Presumption:

假设:

  1. Cocos2dx v2.2.3
  2. Cocos2dx v2.2.3
  3. Foo is the class in which you are trying to run some scheduler in it.
  4. Foo是您试图在其中运行调度程序的类。

#5


0  

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

尝试在函数“AMS_Moving::action()”中设置断点。这段代码显然会调用“action”。

#6


0  

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

只需从这个函数AMS_Moving: action中删除ccTime dt。它将工作