康复游戏-番茄炒蛋
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

56 lines
1.5 KiB

/**
* 游戏主体
* @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 Main(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 最多运动次数
*/
Main.of = function (endCallback, max = config.times) {
const instance = new Main(endCallback, max);
instance.init();
return instance;
};
// 初始方法
Main.prototype.init = function () {
const target = new this.lib.Main();
this.element = target;
stage.addChild(target);
};
// play
// 限制了两次动作间隔时间不能少于2s
// 游戏状态在进行中才能触发
// play次数 >= 最多完成次数 调用结束的callback
Main.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);
};