我正在尝试制作一个西蒙游戏。它第一次通过,但如果我调用game.init()函数重新启动游戏,我的按钮的点击事件不再有效,逻辑中断(它继续说我没有按顺序中的右键)。
您可以在此处查看codepen上的(半)工作游戏:http://codepen.io/cjaypierson/pen/yewjyM?editors=0010
var game = {
comSeq: [],
perSeq: [],
init: function() {
game.comSeq = [];
game.perSeq = [];
game.blinkSpace();
game.comSelect();
setTimeout(function() {
game.comPlay();
}, 1000);
game.perSelect();
},
reset: function() {
game.comSeq = [];
game.perSeq = [];
$('.count').text('00');
},
turnCount: function() {
var l = game.comSeq.length;
var c = l < 10 ? '0' + l : l;
$('.count').text(c);
},
mistake: function() {
$('count').text('!!');
console.log("MISTAKE");
setTimeout(function() {
game.turnCount();
}, 1500);
},
blinkSpace: function() {
$('.quarter').on('mousedown mouseup', function() {
var id = $(this).attr('id');
$(this).toggleClass(id);
})
},
playSound: function(id) {
var clip = $('#' + id).find('audio');
clip[0].play();
},
comSelect: function() {
var options = ['tl', 'tr', 'br', 'bl'];
var selection = options[Math.floor(Math.random() * 4)];
game.comSeq.push(selection);
return selection;
},
comClick: function(space) {
game.playSound(space);
$('#' + space).toggleClass(space);
setTimeout(function() {
$('#' + space).toggleClass(space);
}, 750);
console.log('comSeq: ' + game.comSeq);
console.log('perSeq: ' + game.perSeq);
},
comPlay: function() {
game.perSeq = [];
game.turnCount();
var i = 0;
var play = function() {
if (i < game.comSeq.length) {
game.comClick(game.comSeq[i]);
i++;
setTimeout(play, 1000);
}
};
play();
},
perSelect: function() {
$('.quarter').on('click', function() {
var id = $(this).attr('id');
game.playSound(id);
game.perSeq.push(id);
if (game.perSeq[game.perSeq.length - 1] != game.comSeq[game.perSeq.length - 1]) {
game.mistake();
setTimeout(function() {
game.comPlay();
}, 1500);
} else {
if (game.comSeq.length == game.perSeq.length) {
if (game.perSeq.length >= 3) {
$('.count').text('Winner!');
} else {
game.comSelect();
setTimeout(function() {
game.comPlay();
}, 1500);
}
}
}
})
}
}
$('.start').on('click', function() {
game.init();
});