cocos JS 定时器

cocos2d-js的定时器的创建跟使用:

情况一:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. var TestLayer = cc.Layer.extend({  
  2.     sprite:null,  
  3.     ctor:function () {  
  4.         this.scheduleUpdate();  
  5.     },  
  6.     update: function () {  
  7.         //每一帧都会调用update这个函数  
  8.     }  
  9. });  

情况二:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. var TestLayer = cc.Layer.extend({  
  2.     sprite:null,  
  3.     ctor:function () {  
  4.         this.schedule(this.updateData,0.1);  
  5.     },  
  6.     updateData: function () {  
  7.         //会根据this.schedule第二个参数的时间来调用updataData函数  
  8.     }  
  9. });  


cocos2d-js定时器的销毁unschedule,unscheduleAllCallbacks

一种是针对个别的计时器销毁:unschedule通过调用的函数名销毁

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. var TestLayer = cc.Layer.extend({  
  2.     sprite:null,  
  3.     ctor:function () {  
  4.         this.schedule(this.updateData,0.1);  
  5.         this.removeSchedule()  
  6.     },  
  7.     updateData: function () {  
  8.         //会根据this.schedule第二个参数的时间来调用updataData函数  
  9.         this.unscheduleAllCallbacks()  
  10.     },  
  11.     /** 
  12.      * 删除计时器 
  13.      */  
  14.     removeSchedule: function () {  
  15.         this.unschedule(this.updateData);//通过函数名update删除  
  16.     }  
  17. });  

unschedule,unscheduleAllCallbacks是无论有几个定时器全部都删除了:

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. var TestLayer = cc.Layer.extend({  
    2.     sprite:null,  
    3.     ctor:function () {  
    4.         this.schedule(this.updateData,0.1);  
    5.         this.removeSchedule()  
    6.     },  
    7.     updateData: function () {  
    8.         //会根据this.schedule第二个参数的时间来调用updataData函数  
    9.         this.unscheduleAllCallbacks()  
    10.     },  
    11.     /** 
    12.      * 删除计时器 
    13.      */  
    14.     removeSchedule: function () {  
    15.         this.unscheduleAllCallbacks();//全部删除  
    16.     }  
    17. });  
原文地址:https://www.cnblogs.com/luorende/p/6723191.html