1.定義
定時器在大部分遊戲中是不可或缺的,即每隔一段時間,就要執行相應的刷新體函數,以更新遊戲的畫面、時間、進度、敵人的指令等等。
cocos2dx爲我們提供了定時器schedule相關的操作。其操作函數的定義在CCNode中,所以基本上大多數的引擎類都可以設置定時器,如CCLayer、CCSprite、CCMenu等。
2.種類
定時器更新的方式分爲三類:
(1)默認定時器 :scheduleUpdate();
(2)自定義定時器:schedule();
(3)一次性定時器:scheduleOnce();
3.Demo下載
https://github.com/shahdza/Cocos_LearningTest/tree/master/demo_%E5%AE%9A%E6%97%B6%E5%99%A8schedule%E3%80%81update
4.scheduleUpdate
默認定時器:scheduleUpdate()。
該定時器默認刷新次數與屏幕刷新頻率有關。如頻率爲60幀每秒,那麼scheduleUpdate每秒執行60次刷新。
與scheduleUpdate其對應的刷新函數體爲update(),即每一幀會執行一次update()函數。
相關操作如下:
//
//開啓默認定時器。刷新間隔爲一幀。
void scheduleUpdate();
void scheduleUpdateWithPriority(int priority); //給予優先級priority。priority越小,優先級越高
virtual void update(float delta); //update爲scheduleUpdate定時器的刷新函數體.
//
5.schedule
自定義定時器:schedule()。
該定時器可以自定義指定的刷新函數體、刷新函數體的次數、刷新頻率、以及開始刷新的時間。
函數體不一定爲update(),可以自己定義。
相關操作如下:
//
//設置自定義定時器。默認刷新間隔爲一幀。
// interval : 每隔interval秒,執行一次。
// repeat : 重複次數。
// delay : 延遲時間,即創建定時器delay秒後開始執行刷新。
//schedule( schedule_selector(HelloWorld::myUpdate), 1.0/60.0 );
void schedule(SEL_SCHEDULE selector); //默認刷新間隔爲一幀
void schedule(SEL_SCHEDULE selector, float interval); //自定義刷新間隔,單位:秒
void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
//
6.scheduleOnce
一次性定時器:scheduleOnce()。
該定時器在等待delay秒延遲時間後,只執行一次刷新函數體,之後就不再刷新。
相關操作如下:
//只執行一次,delay秒後執行
//scheduleOnce( schedule_selector(HelloWorld::myUpdate), 5.0 );
void scheduleOnce(SEL_SCHEDULE selector, float delay);
7.其他操作
定時器的取消、暫停、恢復。
相關操作如下:
//
//this->unscheduleUpdate();
//sprite->unscheduleAllSelectors();
void unscheduleUpdate(void); //取消默認定時器
void unschedule(SEL_SCHEDULE selector); //取消自定義函數的定時器
void unscheduleAllSelectors(void); //取消所有定時器
void pauseSchedulerAndActions(void); //暫停所有定時器和動作
void resumeSchedulerAndActions(void); //恢復所有定時器和動作
//
代碼實戰(這是2.x的版本格式,可以自己改爲3.x的版本格式)
1、在HelloWorld::init()中創建五個精靈
精靈和五種定義定時器的方法,一一對應。
//
//創建五個精靈
CCSprite* sp = CCSprite::create("Icon.png");
sp->setPosition( ccp(30, mysize.height - 30) );
this->addChild(sp, 0, 100); //tag標記100
CCSprite* sp1 = CCSprite::create("Icon.png");
sp1->setPosition( ccp(30, mysize.height - 90) );
this->addChild(sp1, 0, 101); //tag標記101
CCSprite* sp2 = CCSprite::create("Icon.png");
sp2->setPosition( ccp(30, mysize.height - 150) );
this->addChild(sp2, 0, 102); //tag標記102
CCSprite* sp3 = CCSprite::create("Icon.png");
sp3->setPosition( ccp(30, mysize.height - 210) );
this->addChild(sp3, 0, 103); //tag標記103
CCSprite* sp4 = CCSprite::create("Icon.png");
sp4->setPosition( ccp(30, mysize.height - 270) );
this->addChild(sp4, 0, 104); //tag標記104
//定義五個定時器,更新精靈
this->scheduleUpdate();
this->schedule( schedule_selector(HelloWorld::myupdate) );
this->schedule( schedule_selector(HelloWorld::myupdate2), 1.0f );
this->schedule( schedule_selector(HelloWorld::myupdate3), 1.0f, 5, 3.0f);
this->scheduleOnce( schedule_selector(HelloWorld::myupdate4), 5.0f );
//
2.編寫定時器對應的刷新函數體
//
//scheduleUpdate
void HelloWorld::update(float dt)
{
CCSprite* sp = (CCSprite*)this->getChildByTag(100); //獲取 tag=100 的精靈
sp->setPosition( sp->getPosition() + ccp(1,0) ); //每幀移動1
}
//schedule(schedule_selector)
void HelloWorld::myupdate(float dt)
{
CCSprite* sp1 = (CCSprite*)this->getChildByTag(101); //獲取 tag=101 的精靈
sp1->setPosition( sp1->getPosition() + ccp(1,0) ); //每幀移動1
}
//schedule(schedule_selector, interval)
void HelloWorld::myupdate2(float dt)
{
CCSprite* sp2 = (CCSprite*)this->getChildByTag(102); //獲取 tag=102 的精靈
sp2->setPosition( sp2->getPosition() + ccp(60,0) ); //每秒移動60
}
//schedule(schedule_selector, interval, repeat, delay)
void HelloWorld::myupdate3(float dt)
{
CCSprite* sp3 = (CCSprite*)this->getChildByTag(103); //獲取 tag=103 的精靈
sp3->setPosition( sp3->getPosition() + ccp(60,0) ); //每秒移動60
}
//scheduleOnce
void HelloWorld::myupdate4(float dt)
{
CCSprite* sp4 = (CCSprite*)this->getChildByTag(104); //獲取 tag=104 的精靈
sp4->setPosition( sp4->getPosition() + ccp(100,0) ); //移動100
}
//
3.運行結果
4.分析和總結
(1)scheduleUpdate()和schedule(schedule_selector)的效果一樣,只是schedule可以自定義刷新函數體,不一定是update()。而scheduleUpdate()的刷新函數體只能爲update()。
(2)schedule(schedule_selector, interval):設置了interval=1.0,所以每隔1.0秒執行了一次myupdate2()。
(3)schedule(schedule_selector, interval, repeat, delay):在開始3.0f秒後纔開始執行myupdate3(),並且之後又重複執行了5次,就停止更新了。
(4)scheduleOnce(schedule_selector):在開始5秒後,只執行了一次myupdate4(),就停止更新了。