How to use CCCallFunc?

Hi, I am beginner and new at cocos2d-x.
I have questions.

I’m now changing codes for “cocos2d for iPhone” to cocos2d-x.
I want use CCCallFunc instead of CCCallBlock.

ActionFactory is inherited from SelectorProtocol, CCObject class.
this line is in the static method and callbackSoundAction is static method too.

How to I remove this error?
and one more question is how to pass argument to callback method.

Thank you in advance :slight_smile:


problem.png (29.0 KB)

Hi, you can see ActionTest sample.

    CCFiniteTimeAction*  action = CCSequence::actions(
        CCCallFunc::actionWithTarget(this, callfunc_selector(ActionSequence2::callback1)),
        NULL);

Why you code use ‘NULL’ as the first parameter?

static CCCallFunc * actionWithTarget(SelectorProtocol* pSelectorTarget, SEL_CallFunc selector)

The first parameter must a ActionFactory instance pointer in your code. Maybe current class is ActionFactory, if so , you can pass ‘this’ as first param.

Thank you James.
I use NULL because this method is static. if I use “this” then error occur.
Is it impossible to use CCCallFunc in static method? :frowning:

It is correct that ‘CCCallFunc::actionWithTarget’ is static method, but the first parameter meaning is where ‘ActionFactory::callbackSoundAction’ belong to ? So you should pass the ActionFactory instance pointer.
Can you tell what the error is when you pass the ActionFactory instance pointer?

I put instance to first parameter so it works. thank you :slight_smile:

one more question is… is there a way to pass parameter to call function?

You can use ‘CCCallFuncND::actionWithTarget’, it allow passing a pointer of void* type.

When I use CCCallFunc, it was okay. but CCCallFuncND makes this error (image)……

You should use

CCCallFuncND::actionWithTarget(this, callfuncND_selector(ActionSequence2::callback3), (void*)0xbebabeba));

and callback function must be declare as follow:

void callback3(CCNode* sender, void* data);

Necroposting!

I need help with some ND callbacks that crash on runtime.

this->runAction(CCSequence::actions(CCPageTurn3D::actionWithDuration(time),
        CCCallFuncND::actionWithTarget(this,
        callfuncND_selector(HelloWorld::stuff), (void *)data)));

void HelloWorld::stuff(CCNode* c, void* s)
{
CCDirector::sharedDirector()->replaceScene(static_cast(s));
}

Thank you!

Thanks dumganhar! This really helped a lot! :slight_smile: