關於Cocos2d-x中定時器的使用總結


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()函數。

    相關操作如下: 

1 //
2     //開啟默認定時器。刷新間隔為一幀。
3     void scheduleUpdate();
4     void scheduleUpdateWithPriority(int priority); //給予優先級priority。priority越小,優先級越高
5  
6     virtual void update(float delta); //update為scheduleUpdate定時器的刷新函數體.
7 //

 

 

5.schedule

自定義定時器:schedule()。

    該定時器可以自定義指定的刷新函數體、刷新函數體的次數、刷新頻率、以及開始刷新的時間。

    函數體不一定為update(),可以自己定義。

    相關操作如下:

 1 //
 2     //設置自定義定時器。默認刷新間隔為一幀。
 3     //      interval  :   每隔interval秒,執行一次。
 4     //      repeat    :   重復次數。
 5     //      delay     :   延遲時間,即創建定時器delay秒后開始執行刷新。
 6     //schedule( schedule_selector(HelloWorld::myUpdate), 1.0/60.0 );
 7     void schedule(SEL_SCHEDULE selector); //默認刷新間隔為一幀
 8     void schedule(SEL_SCHEDULE selector, float interval); //自定義刷新間隔,單位:秒
 9     void schedule(SEL_SCHEDULE selector, float interval, unsigned int repeat, float delay);
10 //

 

 

6.scheduleOnce

一次性定時器:scheduleOnce()。

    該定時器在等待delay秒延遲時間后,只執行一次刷新函數體,之后就不再刷新。

    相關操作如下:

1  //
2      //只執行一次,delay秒后執行
3      //scheduleOnce( schedule_selector(HelloWorld::myUpdate), 5.0 ); 
4      void scheduleOnce(SEL_SCHEDULE selector, float delay);
5  //

 

 

7.其他操作

定時器的取消、暫停、恢復。

相關操作如下:

1 //
2     //this->unscheduleUpdate();
3     //sprite->unscheduleAllSelectors();
4     void unscheduleUpdate(void);            //取消默認定時器
5     void unschedule(SEL_SCHEDULE selector); //取消自定義函數的定時器
6     void unscheduleAllSelectors(void);      //取消所有定時器
7     void pauseSchedulerAndActions(void);    //暫停所有定時器和動作
8     void resumeSchedulerAndActions(void);   //恢復所有定時器和動作
9 //

 

 

代碼實戰(這是2.x的版本格式,可以自己改為3.x的版本格式)

1、在HelloWorld::init()中創建五個精靈

    精靈和五種定義定時器的方法,一一對應。

 1 //
 2     //創建五個精靈
 3         CCSprite* sp = CCSprite::create("Icon.png");
 4         sp->setPosition( ccp(30, mysize.height - 30) );
 5         this->addChild(sp, 0, 100); //tag標記100
 6  
 7         CCSprite* sp1 = CCSprite::create("Icon.png");
 8         sp1->setPosition( ccp(30, mysize.height - 90) );
 9         this->addChild(sp1, 0, 101); //tag標記101
10  
11         CCSprite* sp2 = CCSprite::create("Icon.png");
12         sp2->setPosition( ccp(30, mysize.height - 150) );
13         this->addChild(sp2, 0, 102); //tag標記102
14  
15         CCSprite* sp3 = CCSprite::create("Icon.png");
16         sp3->setPosition( ccp(30, mysize.height - 210) );
17         this->addChild(sp3, 0, 103); //tag標記103
18  
19         CCSprite* sp4 = CCSprite::create("Icon.png");
20         sp4->setPosition( ccp(30, mysize.height - 270) );
21         this->addChild(sp4, 0, 104); //tag標記104
22           
23  
24     //定義五個定時器,更新精靈
25         this->scheduleUpdate();
26         this->schedule( schedule_selector(HelloWorld::myupdate) );
27         this->schedule( schedule_selector(HelloWorld::myupdate2), 1.0f );
28         this->schedule( schedule_selector(HelloWorld::myupdate3), 1.0f, 5, 3.0f);
29         this->scheduleOnce( schedule_selector(HelloWorld::myupdate4), 5.0f );
30 //

 

2.編寫定時器對應的刷新函數體

 1 //
 2     //scheduleUpdate
 3     void HelloWorld::update(float dt)
 4     {
 5         CCSprite* sp = (CCSprite*)this->getChildByTag(100); //獲取 tag=100 的精靈
 6         sp->setPosition( sp->getPosition() + ccp(1,0) );    //每幀移動1
 7     }
 8  
 9     //schedule(schedule_selector)
10     void HelloWorld::myupdate(float dt)
11     {
12         CCSprite* sp1 = (CCSprite*)this->getChildByTag(101); //獲取 tag=101 的精靈
13         sp1->setPosition( sp1->getPosition() + ccp(1,0) );   //每幀移動1
14     }
15  
16     //schedule(schedule_selector, interval)
17     void HelloWorld::myupdate2(float dt)
18     {
19         CCSprite* sp2 = (CCSprite*)this->getChildByTag(102); //獲取 tag=102 的精靈
20         sp2->setPosition( sp2->getPosition() + ccp(60,0) );  //每秒移動60
21     }
22  
23     //schedule(schedule_selector, interval, repeat, delay)
24     void HelloWorld::myupdate3(float dt)
25     {
26         CCSprite* sp3 = (CCSprite*)this->getChildByTag(103); //獲取 tag=103 的精靈
27         sp3->setPosition( sp3->getPosition() + ccp(60,0) );  //每秒移動60
28     }
29  
30     //scheduleOnce
31     void HelloWorld::myupdate4(float dt)
32     {
33         CCSprite* sp4 = (CCSprite*)this->getChildByTag(104); //獲取 tag=104 的精靈
34         sp4->setPosition( sp4->getPosition() + ccp(100,0) ); //移動100
35     }
36 //

 

3.運行結果

wKioL1Py58PgszrlAA4rf6Q3Yv8996.gif

 

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(),就停止更新了。

 

本文出自 “夏天的風” 博客 http://shahdza.blog.51cto.com/2410787/1542014


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM