Cocos2d-x 2.2 Study------------------------Cccallfunc family

Source: Internet
Author: User

Cccallfunc Family

We can use the Cccallfunc family function when we need to call a function to perform a task after the execution of an action in an action sequence is completed . Cccallfunc is a subclass of the Ccactioninstant class. It is worth noting that although the Cccallfunc family function is a subclass of instantaneous action functions, the so-called instantaneous is simply the moment of function invocation, and how long it takes to perform within the function is completely not related to the instantaneous. the Cccallfunc family function encapsulates the procedure of a function call into an action class, which is then put into an action sequence for our invocation.

HelloWorldScene.h as follows:

123456789101112131415161718 #include "cocos2d.h"class HelloWorld : public cocos2d::CCLayerColor{public:    // Method ‘init‘ in cocos2d-x returns bool, instead of ‘id‘ in cocos2d-iphone (an object pointer)    virtual bool init();    // there‘s no ‘id‘ in cpp, so we recommend to return the class instance pointer    static cocos2d::CCScene* scene();                                                                             //先声明四个动作的回调方发    void callBack();    void callNodeBack(CCNode* sender);    void callNodeBack(cocos2d::CCNode *sender, void * data);    void callObjectBack( CCObject * data);    // preprocessor macro for "static create()" constructor ( node() deprecated )    CREATE_FUNC(HelloWorld);};#endif // __HELLOWORLD_SCENE_H__

In the HelloWorldScene.cpp file

BOOLHelloWorld:: The init () function adds the following code:

1234567891011 if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )   {       return false;   }                                                              //CCCallFunc家族函数:当我们需要在一个动作完成之后需要调用某个函数时使用                                                              CCSprite* player = CCSprite::create("Icon.png");   player->setPosition(ccp(100, 100));   this->addChild(player);   CCMoveTo* action = CCMoveTo::create(1, ccp(200, 200));

12345 //CCCallFunc的功能非常简单,它只能简单地实现在动作序列中帮助我们调用一个函数的功能。   CCCallFunc* call  = CCCallFunc::create(this, callfunc_selector(HelloWorld::callBack));   //下面这行代码是创建一个动作序列   CCFiniteTimeAction* seq = CCSequence::create(action,call,NULL);   player->runAction(seq);

12345 //CCCallFunc的回调函数voidHelloWorld::callBack(){    CCLog("CCCallFunc");}

1234 //CCCallFuncN  既能够调用一个方法还能够将调用的对象传过去  这里的调用对象就是player  它是个精灵对象    CCCallFuncN* callN = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::callNodeBack));    CCFiniteTimeAction* seq2 = CCSequence::create(action,callN,NULL);    player->runAction(seq2);

123456 //CCCallFuncN的回调函数voidHelloWorld::callNodeBack(cocos2d::CCNode *sender){    CCSprite* player = (CCSprite*) sender;    CCLog("%f",player->getPosition().x);}

123456789 //先创建一个字典    CCDictionary* dic = CCDictionary::create();    dic->retain();    dic->setObject(CCString::create("zxcc"), 1);                                  //CCCallFuncND可以传递一个任意数据类型  例如,我们可以传递一个字典    CCCallFuncND* callND = CCCallFuncND::create(this, callfuncND_selector(HelloWorld::callNodeBack),(void*)dic);    CCFiniteTimeAction* seq3 = CCSequence::create(action,callND,NULL);    player->runAction(seq3);

12345678 //CCCALLFUNCND callback function void helloworld::callnodeback (Cocos2d::ccnode *sender, void * data) {      ccdictionary* dic = (ccdictionary*) data;      ccstring* str = (ccstring*) (Dic->objectforkey (1));                                      cclog ( ,str->getcstring ()); }

123456789 //我们创建一个精灵    CCSprite* player2 = CCSprite::create("player2.png");    player2->setPosition(ccp(300, 300));    this->addChild(player2);    //在例子中我先移动一个精灵 ,再移动另一个精灵   // CCCallFuncND传值的类型只能为CCObject类型    CCCallFuncO* callO = CCCallFuncO::create(this, callfuncO_selector(HelloWorld::callObjectBack), player2);    CCFiniteTimeAction* seq4 = CCSequence::create(action,callO,NULL);    player->runAction(seq4);

12345678 //CCCallFuncO的回调方法voidHelloWorld::callObjectBack(cocos2d::CCObject *data){    CCSprite* player = (CCSprite*)data;                   player->runAction(CCMoveTo::create(1, ccp(1 ,90)));            }

code example: http://pan.baidu.com/share/link?shareid=611826954&uk=3189484501

Original link: http://7097095.blog.51cto.com/7087095/1228526

Cocos2d-x 2.2 Study------------------------Cccallfunc family

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.