各种回调函数的定义:
1 typedef void (CCObject::*SEL_SCHEDULE)(float); 2 typedef void (CCObject::*SEL_CallFunc)(); 3 typedef void (CCObject::*SEL_CallFuncN)(CCNode*); 4 typedef void (CCObject::*SEL_CallFuncND)(CCNode*, void*); 5 typedef void (CCObject::*SEL_CallFuncO)(CCObject*); 6 typedef void (CCObject::*SEL_MenuHandler)(CCObject*); 7 typedef void (CCObject::*SEL_EventHandler)(CCEvent*); 8 typedef int (CCObject::*SEL_Compare)(CCObject*); 9 10 #define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR) 11 #define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR) 12 #define callfuncN_selector(_SELECTOR) (SEL_CallFuncN)(&_SELECTOR) 13 #define callfuncND_selector(_SELECTOR) (SEL_CallFuncND)(&_SELECTOR) 14 #define callfuncO_selector(_SELECTOR) (SEL_CallFuncO)(&_SELECTOR) 15 #define menu_selector(_SELECTOR) (SEL_MenuHandler)(&_SELECTOR) 16 #define event_selector(_SELECTOR) (SEL_EventHandler)(&_SELECTOR) 17 #define compare_selector(_SELECTOR) (SEL_Compare)(&_SELECTOR)
简单分析一下这两句代码:
#define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR)
#define: 只是一个加单的字符串替代宏,#define A B 的意思是:A和B是一样的东西,只不过换了个写法,经常用在:用一个简单的字符串代替一串复杂的字符串、用一些有意义的单词组合来代表某些值。
typedef void (CCObject::*SEL_SCHEDULE)(float);
typedef: 定义一种类型的别名, typedef void (*fff)(float) 表示fff是一个函数,这个函数的返回类型是 void ,只有一个 float 类型的参数。
关于typedef,详见:http://www.cnblogs.com/charley_yang/archive/2010/12/15/1907384.html
由此可以得知每类回调函数的类型:返回类型和参数详情。下面说说cocos2dx里的三种定时器schedule:schedule,scheduleUpdate,scheduleOnce。也可以参考:http://blog.csdn.net/u010229677/article/details/14107903
scheduleUpdate:通过this->scheduleUpdate()把定时器加到节点后,节点会每帧都会调用虚函数:update(void);取消方法:this->unscheduleUpdate(); 只能触发虚函数 update()
schedule:定义是 void CCNode::schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
通过this->schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay) 把定时器加到节点,可以指定回调函数、触发间隔、重复次数、延迟启动时间,第二个参数(触发间隔)为0则表示每帧都触发,相当于scheduleUpdate,但优势在于可以自己指定回调方法;取消方法:this->unschedule(SEL_SCHEDULE selector);
scheduleOnce: 定义是 void CCNode::scheduleOnce(SEL_SCHEDULE selector, float delay)
this->scheduleOnce(selector, delay) 把定时器加到节点,指定回调函数和延迟启动时间,只会触发一次。取消方法:this->unscheduleOnce(selector);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#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__
|
1
2
3
4
5
6
7
8
9
10
11
|
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));
|
1
2
3
4
5
|
//CCCallFunc的功能非常简单,它只能简单地实现在动作序列中帮助我们调用一个函数的功能。
CCCallFunc* call = CCCallFunc::create(
this
, callfunc_selector(HelloWorld::callBack));
//下面这行代码是创建一个动作序列
CCFiniteTimeAction* seq = CCSequence::create(action,call,NULL);
player->runAction(seq);
|
1
2
3
4
5
|
//CCCallFunc的回调函数
void
HelloWorld::callBack()
{
CCLog(
"CCCallFunc"
);
}
|
1
2
3
4
|
//CCCallFuncN 既能够调用一个方法还能够将调用的对象传过去 这里的调用对象就是player 它是个精灵对象
CCCallFuncN* callN = CCCallFuncN::create(
this
, callfuncN_selector(HelloWorld::callNodeBack));
CCFiniteTimeAction* seq2 = CCSequence::create(action,callN,NULL);
player->runAction(seq2);
|
1
2
3
4
5
6
|
//CCCallFuncN的回调函数
void
HelloWorld::callNodeBack(cocos2d::CCNode *sender)
{
CCSprite* player = (CCSprite*) sender;
CCLog(
"%f"
,player->getPosition().x);
}
|
1
2
3
4
5
6
7
8
9
|
//先创建一个字典
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);
|
1
2
3
4
5
6
7
8
|
//CCCallFuncND的回调函数
void
HelloWorld::callNodeBack(cocos2d::CCNode *sender,
void
* data)
{
CCDictionary* dic = (CCDictionary*)data;
CCString* str = (CCString*)(dic->objectForKey(1));
CCLog(
"%s"
,str->getCString());
}
|
1
2
3
4
5
6
7
8
9
|
//我们创建一个精灵
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);
|
1
2
3
4
5
6
7
8
|
//CCCallFuncO的回调方法
void
HelloWorld::callObjectBack(cocos2d::CCObject *data)
{
CCSprite* player = (CCSprite*)data;
player->runAction(CCMoveTo::create(1, ccp(1 ,90)));
}
|