Welcome to Cocos2d-x chat group: 193411763
Reprinted please indicate the original source: http://blog.csdn.net/u012945598/article/details/17882791
In the Cocos2d-x, we provide many types of callback functions. As we mentioned earlier, CCCallFunc is an invisible action, which is defined in CCActionInstant. in the H file, it is a kind of instantaneous action. Its function is to call back methods in a class. Today we will learn how to use the CCCallFunc series functions.
1. The CCCallFunc class is derived from the CCActionInstant class. typedef void (CCObject: * SEL_CallFunc) () is used to create a callback without parameters.
2. The CCCallFuncN class is derived from the CCCallFunc class. typedef void (CCObject: * SEL_CallFuncN) (CCNode *) is used to create a callback with executor.
3. both the CCCallFuncND and CCCallFuncO classes are derived from the CCCallFuncN class. The callback function created by the former can contain an executor and a void * parameter, the callback function created by the latter is often seen in the Observer mode of the Cocos2d-x, with a CCObject * type parameter
For the void * type, we need to note that we should not consider pointing to the void type as pointing to any type of data, it should be understood as pointing to null type or not pointing to definite type data. If the pointer variable does not specify a specific data type, it cannot access any specific data. It only provides one address. In other words, when using the void * type, we must convert it into a certain data type to access the actual data.
The following code shows how to use the four callback functions.
To create a new project, we declare four corresponding callback methods in the HelloWorld. h file:
Then, create four callback functions in the init () method in the HelloWorld. cpp file:
The first parameter of the create () method used to create CCCallFunc is the target object. In the create () method, an initWithTarget function is actually executed, the current target object is retained to prevent it from being released (the following code is in CCActionInstant. cpp file)
Run the project and check the console output: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.bkjia.com/uploadfile/Collfiles/20140106/2014010609171273.png" alt = "">
Here we can see that the parameter is successfully passed. The executors of the four actions, three of which are this and one is sprite. Based on the priority drawn by the node, the first action is the one executed by the three actions, sprite executes the last action.