cocos2d-x中的callfunc

时间:2012-08-11 21:48:27

标签: cocos2d-iphone cocos2d-x

在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 个答案:

答案 0 :(得分:5)

删除CCObject*方法中的menuCallbackStart()参数(因为CCCallFunc::actionWithTarget()需要一个没有参数的方法),或者将CCCallFunc更改为CCCallFuncOCCObject*为参数的方法,如下所示:

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

其中myObjectCCObject *,将作为参数传递给您的方法。

请注意,callfunc_selector()只是一个将您的方法强制转换为SEL_CallFunc的宏:

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

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

答案 1 :(得分:3)

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

应该是

void LoadingLevelScreen::menuCallbackStart()
{
}

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

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