Archive for August, 2012

Implementing CCCallFunc Cocos2d-X 2.0

Thursday, August 2nd, 2012

This is a simple way of implementing CCCallFunc.

ClassA wants to pass a function reference to ClassB.

CCCallFunc calls a function without any parameters. If you want to get the CCObject that called it then use CCCallFuncN and if you want to pass your own data use CCCallFuncND

In ClassB header create a variable to hold the callFunction.

[cpp]
//ClassB.h
public:
CCCallFunc callFuncHolder;
[/cpp]

In ClassB implementation call the passed in function.
[cpp]
//ClassB.cpp

void someMethod()
{
//this calls the passed in method if there is no set method it will not error
callFuncHolder.execute();
}

[/cpp]

In class ClassA implementation.

[cpp]
//ClassA.cpp
void init()
{
ClassB *cb = new ClassB();
cb->callFuncHolder = *CCCallFunc::create(this, callfunc_selector(methodPassed));

//when ClassB is ready it calls methodPassed
}

//this is the method passed into the CCCallFunc
void methodPassed()
{
CCLog(“methodPassed call form else where”);
}
[/cpp]