Search code examples
cocos2d-iphonecocos2d-x

callfunc in cocos2d-x


In cocos2d-x, the following piece of code is supposed to run the callback function after a delay. What do I need to do to fix the error?

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)
{
}

Compiler Error:

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

Solution

  • Either remove the CCObject* parameter in menuCallbackStart() method (because CCCallFunc::actionWithTarget() expects a method with no arguments), or change CCCallFunc to CCCallFuncO which expects a method with a CCObject* as argument, like so:

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

    where myObject is a CCObject * that will be passed to your method as the argument.

    Note that callfunc_selector() is just a macro that typecasts your method to SEL_CallFunc:

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

    BTW ::actionWithTarget() is being deprecated, so use ::create() instead.