使用计时器
const {ccclass, property} = cc._decorator;

@ccclass
export default class ScheduleStudy extends cc.Component {

    timer:number = 0

    callback()
    {
        this.timer += 1
        console.log("定时器执行:" + this.timer)
        if(this.timer == 10)
        {
            this.unschedule(this.callback)
            //this.unscheduleAllCallbacks() //取消这个组件所有的定时器
        }
    }

    start () {
        let interval = 2
        let repeat = 20
        let delay = 3
        //3秒后 每隔2秒执行一次callback 重复20次
        this.schedule(this.callback,interval,repeat,delay)
        //this.scheduleOnce(this.callback,delay) 只执行定时器一次
    }
}

首页 我的博客
粤ICP备17103704号