cocos2d-js 倒计时功能。

策划想做一个倒计时功能例如:05:00

首先我们先做写一个 倒计时的类。

/**
 * Created by zhao_et on 16/11/28.
 * 时间控制器
 */
var CountDownTimeControl = cc.Class.extend({

    m_updateTime: 1, // 每次的更新时间
    m_allTime: 10, // 整体执行时间
    m_frameCallBack: null, // 每帧回调
    m_overCallBack: null, // 整体执行完毕回调

    ctor: function (updateTime, allTime, frameCallBack, overCallBack) {
        this.init(updateTime, allTime, frameCallBack, overCallBack);
    },
    
    init: function (updateTime, allTime, frameCallBack, overCallBack) {
        this.m_updateTime = updateTime || 1;
        this.m_allTime = allTime || 10;
        this.m_frameCallBack = frameCallBack || null;
        this.m_overCallBack = overCallBack || null;
    },

    // 设置每次更新时间
    setUpdateTime: function (time) {
        this.m_updateTime = time;
    },

    // 设置总体执行时间
    setAllTime: function (time) {
        this.m_allTime = time;
    },

    // 设置每帧回调
    setFrameCallBack: function (callBack) {
        this.m_frameCallBack = callBack;
    },

    // 设置整体执行完毕回调
    setOverCallBack: function (callBack) {
        this.m_overCallBack = callBack;
    },
    
    //  开启倒计时
    startCountDownTime: function () {
        cc.director.getScheduler().schedule(this, this.updateCountDownTime, this.m_updateTime, cc.REPEAT_FOREVER, 0, false, "keyCountDownTime");
    },


    //  每帧更新
    updateCountDownTime: function () {
        this.m_allTime -= this.m_updateTime;
        if(this.m_frameCallBack) this.m_frameCallBack(this.m_allTime);

        if(this.m_allTime == 0 && this.m_overCallBack) {
            this.stopUpdateCountDownTime();
            this.m_overCallBack();
        }
    },

    // 停止倒计时
    stopUpdateCountDownTime: function () {
        cc.director.getScheduler().unschedule(this, this.updateCountDownTime);
    },

    //  清空
    cleanUp: function ( ) {
        this.stopUpdateCountDownTime();
        this.m_updateTime = null;
        this.m_allTime = null;
        this.m_frameCallBack = null;
        this.m_overCallBack = null;
    }
});

在有了倒计时之后我们 转换赋值。

// 战斗中倒计时功能
initBattleCoundDownLabel: function () {
    var _self = this;
    var battleTime = UserInfoManager.getInstance().getBattleTime() / 1000;

  
    var frameCallBack = function (allTime) {
        _self.setTimeStringBySecond(allTime);
        cc.log("alltime: " + allTime);
    };

    var overCallBack = function () {
        cc.log("overCallBack: ");
    };

    this.m_battleTimeCountDownTime.setUpdateTime(1); // 倒计时更新时间
    this.m_battleTimeCountDownTime.setAllTime(battleTime); // 倒计时总时间
    this.m_battleTimeCountDownTime.setFrameCallBack(frameCallBack); // 倒计时帧回调
    this.m_battleTimeCountDownTime.setOverCallBack(overCallBack); // 倒计时结束回调
    this.m_battleTimeCountDownTime.startCountDownTime();

    this.setTimeStringBySecond(battleTime);
},

// 通过描述获取时间
setTimeStringBySecond: function (second) {
    var minute = 0 | (second / 60);
    var second = parseInt(second % 60); // 防止出现小数
    minute = minute > 9 ? minute : "0" + minute;
    second = second > 9 ? second : "0" + second;

    var timeStr = minute + ":" + second;
    this["m_battleTime"].setString(timeStr);
},


你可能感兴趣的:(cocos2d)