/** * 游戏主体 * @param {function} endCallback 游戏结束的回调函数 * @param {number} max 最多运动次数 * * @property {object} lib 资源对象 * @property {object} element 主体元素对象 * @property {number} prevTime 上次完成的时间ms * @property {number} max 最多运动(play)次数 * @property {number} times 当前运动次数 play依次+1 * @property {function} endCallback 结束后 调用的回调函数 */ function Bird(endCallback, max) { this.lib = library; this.element = null; this.prevTime = 0; this.max = max; this.times = 0; this.endCallback = endCallback; } /** * 静态方法 封装new init 返回实例 * @param {function} endCallback 游戏结束的回调函数 * @param {number} max 最多运动次数 */ Bird.of = function (endCallback, max = config.times) { const instance = new Bird(endCallback, max); instance.init(); return instance; }; // 初始方法 Bird.prototype.init = function () { const target = new this.lib.Bird(); this.element = target; stage.addChild(target); }; // play // 限制了两次动作间隔时间不能少于2s // 游戏状态在进行中才能触发 // play次数 >= 最多完成次数 调用结束的callback Bird.prototype.play = function () { if (Date.now() - this.prevTime <= 2000 || state !== 1) return; this.element.play(); this.times += 1; this.prevTime = Date.now(); if (this.times >= this.max) { this.times = this.max; this.endCallback(); } console.log('this.times: ', this.times); };