2

在 cocos2d-x 中,下面这段代码应该会在延迟后运行回调函数。我需要做什么来修复错误?

bool LoadingLevelScreen::initialise() {

        // set up the time delay
    CCDelayTime *delayAction = CCDelayTime::actionWithDuration(0.5f);

    // perform the selector call
    CCCallFunc *callSelectorAction = CCCallFunc::actionWithTarget( 
        this, callfunc_selector( LoadingLevelScreen::menuCallbackStart ) );

    // run the action
    this->runAction( CCSequence::actions(
        delayAction, callSelectorAction, NULL ) );
}

void LoadingLevelScreen::menuCallbackStart(CCObject * pSender)
{
}

编译器错误:

error C2440: 'type cast' : 
cannot convert from 'void (__thiscall LoadingLevelScreen::* )(cocos2d::CCObject *)' 
to 'cocos2d::SEL_CallFunc'
Pointers to members have different representations; cannot cast between them
4

4 回答 4

5

要么删除方法中的CCObject*参数menuCallbackStart()(因为CCCallFunc::actionWithTarget()需要一个没有参数的方法),要么更改CCCallFuncCCCallFuncO哪个需要一个CCObject*作为参数的方法,如下所示:

CCCallFuncO * callSelectorAction =
    CCCallFuncO::create(this, &LoadingLevelScreen::menuCallbackStart, myObject);

将作为参数传递给您的方法的myObjecta在哪里。CCObject *

请注意,这callfunc_selector()只是一个将您的方法类型转换为的宏SEL_CallFunc

#define callfunc_selector(MYSELECTOR) (SEL_CallFunc)(& (MYSELECTOR))

BTW::actionWithTarget()已被弃用,因此请::create()改用。

于 2012-08-14T18:58:48.780 回答
3
void LoadingLevelScreen::menuCallbackStart(CCObject * pSender)
{
}

应该

void LoadingLevelScreen::menuCallbackStart()
{
}

callfunc_selector 与 menu_selector 不同,您不需要 CCObject* 作为变量传入

如果您确实需要传递参数,请使用 callFuncND

于 2012-08-12T22:59:59.320 回答
1

this->runAction(Sequence::create(CallFunc::create(std::bind(&CNm::MNm, this)),NULL));

于 2015-10-02T05:55:05.140 回答
0

this->runAction(Sequence::create(CallFunc::create(std::bind(&ClassName::MethodName, this)),NULL));

于 2015-09-30T13:14:27.380 回答