カウントダウン(CocosCreator)

免責事項:この記事はブロガーオリジナル記事です、続くBY-SAのCC 4.0を著作権契約、複製、元のソースのリンクと、この文を添付してください。
このリンク: https://blog.csdn.net/shirln/article/details/91383669

推奨読書:

      ゲームでは、使用頻度の高い機能がカウントダウンされています。例えば:宝くじ活動から時間が;,現在アクティブなカウントダウンの残りの部分で使用される、など、次のように応じて私たちのサーバーから送信されたデータは、カウントダウンを達成するときについて何かを教えてくれます。

1.サーバは、(ミリ秒)の残留タイムスタンプを送信します

    countdown(remainTime,txt) {
        var self = this;
        let time = parseInt(remainTime/ 1000);
        var call1 = app.callFunc(function (adt) {
            time = time - 1;
            txt.string = self.formatTime(Math.max(0, time));
            if (time <= 0) {
                txt.node.stopAllActions();
            }
        }, txt.node);
        var delay = cc.delayTime(1);
        txt.node.runAction(cc.repeatForever(cc.sequence(call1, delay)));
    },
    /// 计算时间格式
    formatTime(t) {
        var h = Math.floor(t / 3600);
        var m = Math.floor(t % 3600 / 60);
        var s = Math.floor(t % 3600 % 60);
        return "{0}{1}{2}".format(h > 0 ? (this.addZero(h) + ":") : "00:",this.addZero(m) + ":") : "00:", s > 0 ? (this.addZero(s) + "") : "00");
    },

2.サーバーは、(ミリ秒単位)の開始、終了のタイムスタンプを送信します

    countdown(end,txt) {
        var self = this;
        txt.node.stopAllActions();
        var timestamp = Date.parse(new Date());//获取当前时间戳
        let time = parseInt((end - timestamp) / 1000);
        var call1 = app.callFunc(function (adt) {
            time = time - 1;
            txt.string = self.formatTime(Math.max(0, time));
            if (time <= 0) {
                txt.node.stopAllActions();
            }
        }, txt.node);
        var delay = cc.delayTime(1);
        txt.node.runAction(cc.repeatForever(cc.sequence(call1, delay)));
    },
    /// 计算时间格式
    formatTime(t) {
        var h = Math.floor(t / 3600);
        var m = Math.floor(t % 3600 / 60);
        var s = Math.floor(t % 3600 % 60);
        return "{0}{1}{2}".format(h > 0 ? (this.addZero(h) + ":") : "00:",this.addZero(m) + ":") : "00:", s > 0 ? (this.addZero(s) + "") : "00");
    },

3.まとめ

      カウントダウンの残りは秒単位で時間を変換するためFORMATTIME関数で、タイムスタンプを用いて計算されると共に上記2例の比較は、一般的な考え方00:00:00の形態であり、同じです。唯一の違いは、ケース(1)の残りのタイムスタンプが知られていることであり、両者の場合に必要:エンド=残留現在のタイムスタンプタイムスタンプタイムスタンプ。

おすすめ

転載: blog.csdn.net/shirln/article/details/91383669