各種回撥函式的定義:
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);
關於callFunc 家族的使用,見 http://7097095.blog.51cto.com/7087095/1228526