定时器类的封装

/**
 * 在Scene里添加  this.addChild(new TimeCount())
 * 使用美术字的倒计时   需要 fnt文件预加载
 * 美术字的名字注意修改
 * 使用的时候通过
 * cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_SET_TIME, 5) 设置倒计时时间
 * cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_START) 使得倒计时开启
 * cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_STOP)  使得倒计时暂停
 *
 * 在需要得到倒计时结束的类里去
 * cc.eventManager.addCustomListener(USER_TIME_COUNT_FINISH, function(event){
        cc.log('app 获取了定时器传回的结束')
    })
 */
var USER_TIME_COUNT_SET_TIME = "USER_TIME_COUNT_SET_TIME"   //设置倒计时时间
var USER_TIME_COUNT_START = "USER_TIME_COUNT_START"         //开始倒计时
var USER_TIME_COUNT_PAUSE = "USER_TIME_COUNT_PAUSE"           //停止倒计时
var USER_TIME_COUNT_FINISH = "USER_TIME_COUNT_FINISH"       //倒计时结束     由定时器抛出
var TimeCount = cc.Layer.extend({
    EventArr:[USER_TIME_COUNT_SET_TIME, USER_TIME_COUNT_START, USER_TIME_COUNT_PAUSE],
    Label_time:null,
    Label_Position:null,
    Count_Time:0,
    ScheduleRun:false,
    Start_DateTime:0,
    ctor:function(){
        this._super()
        this.init()
    },
    init:function(){
        this.Label_Position = cc.p(cc.winSize.width>>1,cc.winSize.height*0.95);       //调整倒计时坐标
        this.Label_time = new cc.LabelBMFont("TimeCount", res.bmFont);
       /* if(this.Label_time.width <= 0) cc.error('字体文件没有预加载 或 TimeCount模块没有设置正确的BmFont')*/
        this.Label_time.setPosition(this.Label_Position)
        this.addChild(this.Label_time)
        this.scheduleUpdate()

        for(var index in this.EventArr){
            var event = this.EventArr[index]
            cc.eventManager.addCustomListener(event, this.onGetCustom.bind(this))
        }
    },
    onGetCustom:function(event){
        var data = event.getUserData();
        switch (event.getEventName()){
            case USER_TIME_COUNT_SET_TIME:
                cc.log('设置了倒计时时间:',data)
                this.Count_Time = data
                this.Label_time.setString(this.Count_Time)
                break
            case USER_TIME_COUNT_START:
                cc.log('启动了倒计时:')
                this.Start_DateTime = Date.now()
                this.ScheduleRun = true
                break
            case USER_TIME_COUNT_PAUSE:
                cc.log('停止了倒计时')
                this.ScheduleRun = false
                break
        }
    },
    update:function(){
        if(this.ScheduleRun){
            if(Date.now() - this.Start_DateTime>=1000){
                this.Count_Time --;
                this.Label_time.setString(this.Count_Time)
                this.Start_DateTime += 1000
                if(0 == this.Count_Time){
                    this.timeStop();
                    cc.eventManager.dispatchCustomEvent(USER_TIME_COUNT_FINISH)
                }
            }
        }
    },
    timeStop:function(){
        cc.log('倒计时结束了:')
        this.ScheduleRun = false
    },
    onExit:function(){
        this.unscheduleUpdate()
        this.removeAllChildren(true)
        for(var index in this.EventArr){
            var event = this.EventArr[index]
            cc.eventManager.removeCustomListeners(event)
        }
    }
})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值