// Count.of(startTime, defaultCount) 调用即可 /** * 倒计时类 * @param {number} startTime 开始倒计时的时间 * @param {number} defaultCount 倒计时时长 TODO: const startTime = +gameInfo.startTime; * @property {object} modal 面板对象 * @property {object} text 倒计时文本对象 * @property {number} default 倒计时值 * @property {number} timer 计时器id * @property {object} lib 库对象 */ function Count(startTime = Date.now(), defaultCount = config.count || 5) { this.default = defaultCount; this.startTime = startTime; this.modal = null; this.text = null; this.timer = null; this.lib = library; } /** * 静态方法 封装new 及 init方法 * 使用时直接调用此方法 * @param {number} defaultCount 倒计时时长 * @returns */ Count.of = function (defaultCount) { const instance = new Count(defaultCount); instance.init(); return instance; }; // 初始化方法 Count.prototype.init = function () { this.renderModal(); this.renderContent(); }; // 渲染面板 Count.prototype.renderModal = function () { const target = new this.lib.bgModalBegin(); target.x = this.lib.properties.width / 2; target.y = this.lib.properties.height / 2; this.modal = target; stage.addChild(target); // 显示开始游戏的倒计时面板 }; // 渲染倒计时文本内容 Count.prototype.renderContent = function () { const endCountTime = this.startTime + this.default * 1000; let leftCount = Math.round((endCountTime - Date.now()) / 1000); this.update(leftCount); // 开始开始游戏的 倒计时 this.timer = setInterval(() => { leftCount = Math.round((endCountTime - Date.now()) / 1000); stage.removeChild(this.text); this.update(leftCount); }, 1000); }; // 更新倒计时文本 Count.prototype.update = function (time) { if (time <= 0) { console.log(time); // 发送倒计时结束的消息 clearInterval(this.timer); stage.removeChild(this.text); stage.removeChild(this.modal); // sendEndCountRequest(); // 发送倒计时结束的消息 return; } this.renderText(time); }; // 渲染文本 Count.prototype.renderText = function (time) { const text = new createjs.Text(time, 'bold 100px Arial', '#87431c'); text.x = this.lib.properties.width / 2; text.y = this.lib.properties.height / 2 + 20; text.textAlign = 'center'; text.textBaseline = 'alphabetic'; this.text = text; stage.addChild(text); };