您好,登錄后才能下訂單哦!
CCCallFunC家族
當(dāng)我們需要在一個(gè)動(dòng)作序列中某一個(gè)動(dòng)作執(zhí)行結(jié)束之后,調(diào)用某個(gè)函數(shù)用于執(zhí)行一個(gè)任務(wù)的時(shí)候,我們可以使用CCCallFunC家族函數(shù)。CCCallFunC是CCActionInstant類的子類。值得注意的是,雖然CCCallFunC家族函數(shù)是瞬時(shí)動(dòng)作函數(shù)的子類,但是所謂的瞬時(shí),也只是指函數(shù)調(diào)用的一瞬間,而關(guān)于函數(shù)內(nèi)部怎么執(zhí)行,耗用多久,則完全與瞬時(shí)沒有任何關(guān)系。CCCallFunC家族函數(shù)可以將函數(shù)調(diào)用的過程封裝成一個(gè)動(dòng)作類,從而放入動(dòng)作序列中以供我們調(diào)用。
HelloWorldScene.h如下:
#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(); //先聲明四個(gè)動(dòng)作的回調(diào)方發(fā) 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__
HelloWorldScene.cpp文件中的
boolHelloWorld::init()函數(shù)中加入如下代碼:
if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) ) { return false; } //CCCallFunc家族函數(shù):當(dāng)我們需要在一個(gè)動(dòng)作完成之后需要調(diào)用某個(gè)函數(shù)時(shí)使用 CCSprite* player = CCSprite::create("Icon.png"); player->setPosition(ccp(100, 100)); this->addChild(player); CCMoveTo* action = CCMoveTo::create(1, ccp(200, 200));
//CCCallFunc的功能非常簡單,它只能簡單地實(shí)現(xiàn)在動(dòng)作序列中幫助我們調(diào)用一個(gè)函數(shù)的功能。 CCCallFunc* call = CCCallFunc::create(this, callfunc_selector(HelloWorld::callBack)); //下面這行代碼是創(chuàng)建一個(gè)動(dòng)作序列 CCFiniteTimeAction* seq = CCSequence::create(action,call,NULL); player->runAction(seq);
//CCCallFunc的回調(diào)函數(shù) void HelloWorld::callBack() { CCLog("CCCallFunc"); }
//CCCallFuncN 既能夠調(diào)用一個(gè)方法還能夠?qū)⒄{(diào)用的對(duì)象傳過去 這里的調(diào)用對(duì)象就是player 它是個(gè)精靈對(duì)象 CCCallFuncN* callN = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::callNodeBack)); CCFiniteTimeAction* seq2 = CCSequence::create(action,callN,NULL); player->runAction(seq2);
//CCCallFuncN的回調(diào)函數(shù) void HelloWorld::callNodeBack(cocos2d::CCNode *sender) { CCSprite* player = (CCSprite*) sender; CCLog("%f",player->getPosition().x); }
//先創(chuàng)建一個(gè)字典 CCDictionary* dic = CCDictionary::create(); dic->retain(); dic->setObject(CCString::create("zxcc"), 1); //CCCallFuncND可以傳遞一個(gè)任意數(shù)據(jù)類型 例如,我們可以傳遞一個(gè)字典 CCCallFuncND* callND = CCCallFuncND::create(this, callfuncND_selector(HelloWorld::callNodeBack),(void*)dic); CCFiniteTimeAction* seq3 = CCSequence::create(action,callND,NULL); player->runAction(seq3);
//CCCallFuncND的回調(diào)函數(shù) void HelloWorld::callNodeBack(cocos2d::CCNode *sender, void * data) { CCDictionary* dic = (CCDictionary*)data; CCString* str = (CCString*)(dic->objectForKey(1)); CCLog("%s",str->getCString()); }
//我們創(chuàng)建一個(gè)精靈 CCSprite* player2 = CCSprite::create("player2.png"); player2->setPosition(ccp(300, 300)); this->addChild(player2); //在例子中我先移動(dòng)一個(gè)精靈 ,再移動(dòng)另一個(gè)精靈 // CCCallFuncND傳值的類型只能為CCObject類型 CCCallFuncO* callO = CCCallFuncO::create(this, callfuncO_selector(HelloWorld::callObjectBack), player2); CCFiniteTimeAction* seq4 = CCSequence::create(action,callO,NULL); player->runAction(seq4);
//CCCallFuncO的回調(diào)方法 void HelloWorld::callObjectBack(cocos2d::CCObject *data) { CCSprite* player = (CCSprite*)data; player->runAction(CCMoveTo::create(1, ccp(1 ,90))); }
代碼例子: http://pan.baidu.com/share/link?shareid=611826954&uk=3189484501
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。