@H_502_1@
一、schedule 有三种不同的api:
1. scheduleUpdate():
@H_502_1@
2.scheduleOnce(callback,delay,key):
@H_502_1@
@H_502_1@Parameters:
@H_502_1@{function} callback
@H_502_1@A function wrapped as a selector
@H_502_1@{Number} delay
@H_502_1@The amount of time that the first tick will wait before execution.
@H_502_1@{String} key
@H_502_1@The only string identifying the callback
3. schedule(callback,interval,repeat,key):
@H_502_1@
@H_502_1@Parameters:
@H_502_1@{function} callback
@H_502_1@A function wrapped as a selector
@H_502_1@{Number} interval
@H_502_1@Tick interval in seconds. 0 means tick every frame. If interval = 0,it's recommended to use scheduleUpdate() instead.
@H_502_1@{Number} repeat
@H_502_1@The selector will be executed (repeat + 1) times,you can use kCCRepeatForever for tick infinitely.
@H_502_1@{Number} delay
@H_502_1@The amount of time that the first tick will wait before execution.
@H_502_1@{String} key
@H_502_1@The only string identifying the callback
二、schedule三种api的使用:
1. scheduleUpdate()
@H_502_1@
@H_502_1@以layer为例,在初始化的时候(ctor函数return true 前,其他地方也可以,建议@H_502_1@在初始化时)添加:
- this.scheduleUpdate();//开启每帧调用,对应update
@H_502_1@接着重写layer的update方法:(这里先定义了一个layer的成员变量time)
2.scheduleOnce(callback,key)
@H_502_1@
@H_502_1@
- //延迟2秒后,只执行一次
- this.scheduleOnce(this.once,2);
@H_502_1@接着新建一个once的方法:
- once:function(){
- cc.log(@H_404_253@"延迟2秒后,只执行一次");
- },
3.schedule(callback,key)
@H_502_1@
@H_502_1@
- //不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
- bsp;this.schedule(this.notParameter,2,20,5);
接着新建一个notParameter的方法:
三、schedule的代码示例:
- vartest=cc.Layer.extend({
- time:0,
- ctor:function(){
- this._super();
- this.time=0;
- this.scheduleUpdate();//开启每帧调用,对应update
- //不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
- this.schedule(this.notParameter,5);
- //带参数的回调函数haveParameter,每1.5秒执行一次,累积执行10次
- this.schedule(functionnothing(){
- this.haveParameter(@H_404_253@"带参数")
- },1.5,10);
- //每5秒执行一次,无次数限制
- this.schedule(this.notRepeat,5);
- //延迟2秒后,只执行一次
- this.scheduleOnce(this.once,2);
- returntrue;
- },
- notRepeat:function(){
- cc.log(@H_404_253@"每5秒执行一次");
- },
- notParameter:function(){
- cc.log(@H_404_253@"不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次");
- },
- haveParameter:function(s){
- cc.log(s+@H_404_253@"的回调函数haveParameter,每1.5秒执行一次,累积执行10次");
- },
- once:function(){
- cc.log(@H_404_253@"延迟2秒后,只执行一次");
- },
- update:function(dt){
- this.time+=dt;//dt为上一帧到当前帧的时长
- if(this.time>=3){
- cc.log(@H_404_253@"每3秒在调试框中输出一次");
- this.time=0;
- };
- },
- onExit:function(){
- this._super();
- this.unscheduleUpdate();//移除schedule
- this.unschedule(this.notRepeat);//移除没有次数限制的schedule
- this.unscheduleAllCallbacks();//移除所有schedule的回调函数
- }
- });