cocos2d-js浅谈schedule的用法

前端之家收集整理的这篇文章主要介绍了cocos2d-js浅谈schedule的用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
展性及实用性上,建议大家统一使用cocos2d自身提供的schedule方法。下面就为大家深入浅出的介绍下schedule:

@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@在初始化时)添加

[javascript] view plain copy
  1. this.scheduleUpdate();//开启每帧调用,对应update
@H_502_1@接着重写layer的update方法:(这里先定义了一个layer的成员变量time)
[javascript] view plain copy
  1. update:function(dt){
  2. this.time+=dt;//dt为上一帧到当前帧的时长
  3. if(this.time>=3){
  4. cc.log(@H_404_253@"每3秒在调试框中输出一次");
  5. this.time=0;
  6. };
  7. },
@H_502_1@

2.scheduleOnce(callback,key)

@H_502_1@

以layer为例,在初始化或其他非解析方法添加

@H_502_1@

[javascript] view plain copy
  1. //延迟2秒后,只执行一次
  2. this.scheduleOnce(this.once,2);
@H_502_1@接着新建一个once的方法
[javascript] view plain copy
  1. once:function(){
  2. cc.log(@H_404_253@"延迟2秒后,只执行一次");
  3. },

3.schedule(callback,key)

@H_502_1@

同样以layer为例,在初始化或其他非解析方法添加

@H_502_1@

[javascript] view plain copy
  1. //不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
  2. bsp;this.schedule(this.notParameter,2,20,5);
@H_502_1@
接着新建一个notParameter的方法
[javascript] view plain copy
  1. notParameter:function(){
  2. cc.log(@H_404_253@"不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次");
  3. },

三、schedule的代码示例:

[javascript] view plain copy
  1. vartest=cc.Layer.extend({
  2. time:0,
  3. ctor:function(){
  4. this._super();
  5. this.time=0;
  6. this.scheduleUpdate();//开启每帧调用,对应update
  7. //不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次
  8. this.schedule(this.notParameter,5);
  9. //带参数的回调函数haveParameter,每1.5秒执行一次,累积执行10次
  10. this.schedule(functionnothing(){
  11. this.haveParameter(@H_404_253@"带参数")
  12. },1.5,10);
  13. //每5秒执行一次,无次数限制
  14. this.schedule(this.notRepeat,5);
  15. //延迟2秒后,只执行一次
  16. this.scheduleOnce(this.once,2);
  17. returntrue;
  18. },
  19. notRepeat:function(){
  20. cc.log(@H_404_253@"每5秒执行一次");
  21. },
  22. notParameter:function(){
  23. cc.log(@H_404_253@"不带参数的回调函数notParameter,延迟5秒后每2秒执行一次,累积执行20次");
  24. },
  25. haveParameter:function(s){
  26. cc.log(s+@H_404_253@"的回调函数haveParameter,每1.5秒执行一次,累积执行10次");
  27. },
  28. once:function(){
  29. cc.log(@H_404_253@"延迟2秒后,只执行一次");
  30. },
  31. update:function(dt){
  32. this.time+=dt;//dt为上一帧到当前帧的时长
  33. if(this.time>=3){
  34. cc.log(@H_404_253@"每3秒在调试框中输出一次");
  35. this.time=0;
  36. };
  37. },
  38. onExit:function(){
  39. this._super();
  40. this.unscheduleUpdate();//移除schedule
  41. this.unschedule(this.notRepeat);//移除没有次数限制的schedule
  42. this.unscheduleAllCallbacks();//移除所有schedule的回调函数
  43. }
  44. });

猜你在找的Cocos2d-x相关文章