First look at a picture, indicating the inheritance of the Cccallfunc relationship:
1, Cccallfunc, callback, call function without parameters: 2, Cccallfunco, callback, o means Ccobject,cccallfunco can take ccobject parameter function 3, CCCALLFUNCN, callback, n means Ccnode, Indicates that the node* type 4, CCCALLFUNCND, callback, ND represent ccnode and data can be passed, the object itself and a void* type parameter may be passed, and void* can be any type of argument
Cccallfunc CCCALLFUNCN cccallfuncnd is used to create an action with a callback function, the main difference is whether the callback function with a parameter cccallfunc is to execute the corresponding callback function, where the callback function is not parameters. Creating an instance using the static member function, create declares the following:Staticcccallfunc* Create (Ccobject *Pselectortarget,sel_callfunc Selector) callback functions are executed through the Execute method, and execute in Cccallfunc is implemented as follows:voidCccallfunc::execute () {if(M_pcallfunc) {(M_pselectortarget->*M_pcallfunc) (); } if(M_nscripthandler) {Ccscriptenginemanager::sharedmanager ()->getscriptengine ()->executecallfuncactionevent ( This); }} through (M_pselectortarget->*m_pcallfunc) (); You can see that the callback function does not contain parameters
CCCALLFUNCN also executes the corresponding callback function, where the callback function takes a parameter. Creating an instance using the static member function, create declares the following: static cccallfuncn* Create (Ccobject * PSELECTORTARGET,SEL_CALLFUNCN selector) The callback function is executed by the Execute method, Execute in CCCALLFUNCN is implemented as follows: void Cccallfuncn::execute () { if (m_ PCALLFUNCN) {(M_pselectortarget ->*M_PCALLFUNCN) (M_ptarget); if (M_nscripthandler) {Ccscriptengi Nemanager::sharedmanager () ->getscriptengine ()->executecallfuncactionevent ( this ->*M_PCALLFUNCN) (m_ptarget), you can see that the callback function contains a parameter.
CCCALLFUNCND also executes the corresponding callback function, where the callback function can take two parameters. Creating an instance using the static member function, create declares the following: static cccallfuncnd* Create ( ccobject * PSELECTORTARGET,SEL_CALLFUNCND selector,void * D) The callback function is executed by the Execute method, Execute in CCCALLFUNCND is implemented as follows:void Cccallfuncnd::execute () { if (m_ PCALLFUNCND) { (m_pselectortarget->*m_pcallfuncnd) (M_ptarget, m_pdata);} } by (M_pselectortarget->*m_pcallfuncnd) (M_ptarget, m_pdata), you can see that the callback function contains two parameters.
code in TestCallFunc.hclassTestcallfunc: Publiccclayer{protected: Ccsprite*sprite1; Ccsprite*Sprite2; Ccsprite*Sprite3; Public: Virtual voidOnEnter (); voidCallback1 (); voidCallback2 (ccnode*sender); voidCallback3 (ccnode* sender,void*data);}; Code in TestCallFunc.cppvoidTestcallfunc::onenter () {//Use of Cccallfuncccfinitetimeaction* action =ccsequence::create (Ccmoveby::create (2, CCP ( $,0)), Cccallfunc::create ( This, Callfunc_selector (TESTCALLFUNC::CALLBACK1)), NULL); //Use of CCCALLFUNCNccfinitetimeaction* Action2 =ccsequence::create (Ccscaleby::create (2,2), Ccfadeout::create (2), Cccallfuncn::create ( This, Callfuncn_selector (Testcallfunc::callback2)), NULL); //Use of CCCALLFUNCNCccfinitetimeaction* Action3 =ccsequence::create (Ccrotateby::create (3, the), Ccfadeout::create (2), Cccallfuncnd::create ( This, Callfuncnd_selector (TESTCALLFUNC::CALLBACK3), (void*)0xbebabeba), NULL); Sprite1-runaction (action); Sprite2-runaction (Action2); Sprite3-runaction (Action3);}voidTestcallfunc::callback1 () {ccsize s= Ccdirector::shareddirector ()getwinsize (); Cclabelttf*label = Cclabelttf::create ("Callback 1 called","Marker Felt", -); Label->setposition (CCP (s.width/4*1, s.height/2)); AddChild (label);}voidTestcallfunc::callback2 (ccnode*Psender) {ccsize s= Ccdirector::shareddirector ()getwinsize (); Cclabelttf*label = Cclabelttf::create ("Callback 2 called","Marker Felt", -); Label->setposition (CCP (s.width/4*2, s.height/2)); AddChild (label);}voidTestcallfunc::callback3 (ccnode* Ptarget,void*data) {ccsize s= Ccdirector::shareddirector ()getwinsize (); Cclabelttf*label = Cclabelttf::create ("Callback 3 called","Marker Felt", -); Label->setposition (CCP (s.width/4*3, s.height/2)); AddChild (label);}
Comparison of Cccallfunc,cccallfuncn,cccallfuncnd,cccallfunco in COCOS2DX