一次性定时器
注:
①14为秒数
this.scheduleOnce(function(){xxx },14)
例:
this.scheduleOnce(function(){
//scene7在2s的时候执行动画s7move
this.scene7.getComponent(cc.Animation).play("s7move");
},2)
循环定时器
注:
①function() { this.doSomething();}是回调方法。一般来说,如果要用当前组件中的方法,需要写成function(){}.bind(this)的形式,否则会报错(这个还没试过)
②interval为时间间隔,单位为s
③repeat就是重复执行的次数,默认repeat为0,执行一次,repeat为1就是两次
④delay是延迟时间,总共会执行里面的方法repeat+1次,第一次在delay时间之后执行。如果将delay置为0,默认情况下,第一次执行仍然会延时,而且延时时间是interval。
component.schedule(function() {
this.doSomething();
}, interval, repeat, delay);
例:
//将str的内容在txt04上逐个打印出来
let str = "这是需要逐字打印的文字?";
let j = 0;
this.txt04.getComponent(cc.Label).string = "";
this.schedule(function () {
this.txt04.getComponent(cc.Label).string += str[j];
j++;
}, 0.1, str.length - 1, 0.2);
停止本页所有定时器
this.unscheduleAllCallbacks();
cocos笔记链接:
cocos creator学习(一)页面+动画
cocos creator学习(二)js绑定动画
cocos creator学习(三)预制节点
cocos creator学习(四)全局变量+类之间的函数调用
cocos creator学习(五)定时器
cocos creator学习(六)组件Mask(超出部分隐藏)
cocos creator学习(七)音乐
cocos creator学习(八)自动图集+构建发布h5
cocos creator学习(九)构建后更改图片和层级设置
cocos creator学习(十)cocos自带扩展插件-压缩