最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解CocosCreator中幾種計時器的使用方法

 更新時間:2021年04月16日 15:26:11   作者:gamedaybyday  
這篇文章主要介紹了CocosCreator中幾種計時器的使用方法,推薦使用schedule,功能多些,銷毀時還能自動移除

一、setTimeOut

3秒后打印abc。只執(zhí)行一次。

setTimeout(()=>{console.log("abc"); }, 3000);

刪除計時器,3秒后不會輸出abc。

let timeIndex;
timeIndex = setTimeout(()=>{console.log("abc"); }, 3000);
clearTimeout(timeIndex);

setTimeout這樣寫,test函數(shù)中輸出的this是Window對象

@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í)行,每秒都會輸出一個abc。

setInterval(()=>{console.log("abc"); }, 1000);

刪除計時器,不會再輸出abc。

let timeIndex;
timeIndex = setInterval(()=>{console.log("abc"); }, 1000);
clearInterval(timeIndex);

三、Schedule

每個繼承cc.Component的都自帶了這個計時器

schedule(callback: Function, interval?: number, repeat?: number, delay?: number): void;

延遲3秒后,輸出abc,此后每隔1秒輸出abc,重復(fù)5次。所以最終會輸出5+1次abc?!?/p>

this.schedule(()=>{console.log("abc")},1,5,3);

刪除schedule(若要刪除,則不能再使用匿名函數(shù)了,得能訪問到要刪除的函數(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)于一個全局的計時器吧,在cc.director上。注意必須調(diào)用enableForTarget()來注冊id,不然會報錯。

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,無限重復(fù)
    scheduler.schedule(this.test2, this, 1, cc.macro.REPEAT_FOREVER,3, false);
}
 
private test1(){
    console.log("test1");
}
 
private test2(){
    console.log("test2");
}
//刪除計時器
scheduler.unschedule(this.test1, this);

以上就是詳解CocosCreator中幾種計時器的使用方法的詳細(xì)內(nèi)容,更多關(guān)于CocosCreator計時器的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

锦屏县| 玉门市| 西贡区| 浑源县| 惠水县| 延津县| 吉安县| 九江县| 溧阳市| 四川省| 隆尧县| 宜阳县| 满城县| 奉贤区| 定边县| 原平市| 稻城县| 凉山| 来安县| 吐鲁番市| 徐闻县| 尖扎县| 德昌县| 元朗区| 延安市| 彰武县| 宁德市| 温宿县| 南木林县| 黑水县| 互助| 武功县| 广水市| 内乡县| 出国| 西吉县| 毕节市| 自治县| 蒙阴县| 邢台县| 固原市|