詳解CocosCreator中幾種計(jì)時(shí)器的使用方法
一、setTimeOut
3秒后打印abc。只執(zhí)行一次。
setTimeout(()=>{console.log("abc"); }, 3000);
刪除計(jì)時(shí)器,3秒后不會(huì)輸出abc。
let timeIndex; timeIndex = setTimeout(()=>{console.log("abc"); }, 3000); clearTimeout(timeIndex);
setTimeout這樣寫,test函數(shù)中輸出的this是Window對(duì)象
@ccclass export default class Helloworld extends cc.Component { private a = 1; start() { setTimeout(this.test, 3000); } private test(){ console.log(this.a); //輸出undefined console.log(this); //Window } }
使用箭頭函數(shù)
@ccclass export default class Helloworld extends cc.Component { private a = 1; start() { setTimeout(()=>{this.test()}, 3000); } private test(){ console.log(this.a); //輸出1 console.log(this); //Helloworld } }
二、setInterval
1秒后輸出abc,重復(fù)執(zhí)行,每秒都會(huì)輸出一個(gè)abc。
setInterval(()=>{console.log("abc"); }, 1000);
刪除計(jì)時(shí)器,不會(huì)再輸出abc。
let timeIndex; timeIndex = setInterval(()=>{console.log("abc"); }, 1000); clearInterval(timeIndex);
三、Schedule
每個(gè)繼承cc.Component的都自帶了這個(gè)計(jì)時(shí)器
schedule(callback: Function, interval?: number, repeat?: number, delay?: number): void;
延遲3秒后,輸出abc,此后每隔1秒輸出abc,重復(fù)5次。所以最終會(huì)輸出5+1次abc?!?/p>
this.schedule(()=>{console.log("abc")},1,5,3);
刪除schedule(若要?jiǎng)h除,則不能再使用匿名函數(shù)了,得能訪問(wèn)到要?jiǎng)h除的函數(shù))
private count = 1; start() { this.schedule(this.test,1,5,3); this.unschedule(this.test); } private test(){ console.log(this.count); }
全局的schedule
相當(dāng)于一個(gè)全局的計(jì)時(shí)器吧,在cc.director上。注意必須調(diào)用enableForTarget()來(lái)注冊(cè)id,不然會(huì)報(bào)錯(cuò)。
start() { let scheduler:cc.Scheduler = cc.director.getScheduler(); scheduler.enableForTarget(this); //延遲3秒后,輸出1,此后每1秒輸出1,重復(fù)3次。一共輸出1+3次 scheduler.schedule(this.test1, this, 1, 3,3, false); //延遲3秒后,輸出1,此后每1秒輸出1,無(wú)限重復(fù) scheduler.schedule(this.test2, this, 1, cc.macro.REPEAT_FOREVER,3, false); } private test1(){ console.log("test1"); } private test2(){ console.log("test2"); }
//刪除計(jì)時(shí)器 scheduler.unschedule(this.test1, this);
以上就是詳解CocosCreator中幾種計(jì)時(shí)器的使用方法的詳細(xì)內(nèi)容,更多關(guān)于CocosCreator計(jì)時(shí)器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Unity3D實(shí)現(xiàn)攝像機(jī)鏡頭移動(dòng)并限制角度
- CocosCreator學(xué)習(xí)之模塊化腳本
- 怎樣在CocosCreator中使用物理引擎關(guān)節(jié)
- 如何在CocosCreator中使用JSZip壓縮
- CocosCreator入門教程之用TS制作第一個(gè)游戲
- 解讀CocosCreator源碼之引擎啟動(dòng)與主循環(huán)
- CocosCreator通用框架設(shè)計(jì)之資源管理
- 如何在CocosCreator中做一個(gè)List
- 如何在CocosCreator中使用http和WebSocket
- 剖析CocosCreator新資源管理系統(tǒng)
- CocosCreator怎樣使用cc.follow進(jìn)行鏡頭跟隨
相關(guān)文章
詳解bootstrap-fileinput文件上傳控件的親身實(shí)踐
這篇文章主要介紹了詳解bootstrap-fileinput文件上傳控件的親身實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03javascript 原型與原型鏈的理解及實(shí)例分析
這篇文章主要介紹了javascript 原型與原型鏈的理解,結(jié)合實(shí)例形式分析了javascript 原型與原型鏈的原理、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-11-11一文教你徹底學(xué)會(huì)JavaScript手寫防抖節(jié)流
其實(shí)防抖和節(jié)流不僅僅在面試中會(huì)讓大家手寫,在實(shí)際項(xiàng)目中也可以起到性能優(yōu)化的作用,所以還是很有必要掌握的。本文就帶大家徹底學(xué)會(huì)JavaScript手寫防抖節(jié)流,需要的可以參考一下2022-11-11IE6/7/8中Option元素未設(shè)value時(shí)Select將獲取空字符串
可以看到當(dāng)忘記寫option的value時(shí)這些現(xiàn)代瀏覽器都會(huì)盡量返回正確的(客戶端程序員想要的)結(jié)果value,其容錯(cuò)性比IE6/7/8做的更好。2011-04-04