callfunc in cocos2d-x

11,855

Solution 1

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.

Solution 2

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

should be

void LoadingLevelScreen::menuCallbackStart()
{
}

callfunc_selector is different with menu_selector, you don't need the CCObject* to pass in as a variable

if you do need to pass argument, please use callFuncND

Solution 3

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

Share:
11,855
Ben
Author by

Ben

I have just finished a 4-year degree in Physics & Computer Science, and now I am looking to get more into programming. I have been doing Web-Development for seemingly forever. You can talk to me in HTML & CSS, and I also know one thing or another about JS, jQuery, PHP and MySQL. Currently, I am interested in simple 2D game development. I use C++, SDL and OpenGL.

Updated on June 04, 2022

Comments

  • Ben
    Ben almost 2 years

    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
    
  • Ben
    Ben over 11 years
    Thank you, that was really helpful :) And that little "BTW" helped me to resolve a bunch of "deprecated" warnings by replacing various function calls with create().