Javascript 如何解决这个浏览器游戏错误
游戏中的漏洞是,如果用户点击足够快,他可以翻转超过2张牌。 我试着使这个布尔变量(var isProcessing)在代码中的某些地方设置为true/false, 但是我想我把它放错地方了,因为这个bug似乎没有被修复 我哪里错放了布尔值,哪里错了 守则:Javascript 如何解决这个浏览器游戏错误,javascript,Javascript,游戏中的漏洞是,如果用户点击足够快,他可以翻转超过2张牌。 我试着使这个布尔变量(var isProcessing)在代码中的某些地方设置为true/false, 但是我想我把它放错地方了,因为这个bug似乎没有被修复 我哪里错放了布尔值,哪里错了 守则: // Those are global variables, they stay alive and reflect the state of the game var elPreviousCard = null; var flippedCo
// Those are global variables, they stay alive and reflect the state of the game
var elPreviousCard = null;
var flippedCouplesCount = 0;
// This is a constant that we dont change during the game (we mark those with CAPITAL letters)
var TOTAL_COUPLES_COUNT = 3;
// Load an audio file
var audioWin = new Audio('sound/win.mp3');
var audioright = new Audio('sound/right.mp3');
var audiowrong = new Audio('sound/wrong.mp3');
var isProcessing = false;
// This function is called whenever the user click a card
function cardClicked(elCard) {
// If the user clicked an already flipped card - do nothing and return from the function
isProcessing = true;
if (elCard.classList.contains('flipped')) {
return;
}
// Flip it
elCard.classList.add('flipped');
// This is a first card, only keep it in the global variable
if (elPreviousCard === null) {
isProcessing = true;
elPreviousCard = elCard;
} else {
isProcessing = false;
// get the data-card attribute's value from both cards
var card1 = elPreviousCard.getAttribute('data-card');
var card2 = elCard.getAttribute('data-card');
// No match, schedule to flip them back in 1 second
if (card1 !== card2){
isProcessing = false;
setTimeout(function () {
elCard.classList.remove('flipped');
elPreviousCard.classList.remove('flipped');
elPreviousCard = null;
isProcessing = true;
}, 1000)
audiowrong.play();
} else {
// Yes! a match!
flippedCouplesCount++;
elPreviousCard = null;
audioright.play();
// All cards flipped!
if (TOTAL_COUPLES_COUNT === flippedCouplesCount) {
audioWin.play();
// and finally add a button to call resetCard()
document.getElementById("Play Again").innerHTML =
'<button onclick="resetCard();">Play Again</button>';
}
}
}
}
function resetCard() {// to erase the flipped classes
var cardclass = document.getElementsByClassName("card");
for (i = 0; i < cardclass.length; i++) {
cardclass[i].classList.remove("flipped");
document.getElementById("Play Again").innerHTML = "";
}
}
//这些是全局变量,它们保持活动状态并反映游戏状态
var elPreviousCard=null;
var FlippedCoupledScont=0;
//这是一个我们在游戏中不会改变的常数(我们用大写字母标记)
var总对数=3;
//加载音频文件
var audioWin=新音频('sound/win.mp3');
var audioright=新音频('sound/right.mp3');
var AudioError=新音频('sound/error.mp3');
var isProcessing=false;
//每当用户单击卡时,就会调用此函数
功能卡(elCard){
//如果用户单击了一张已经翻转的卡片-不执行任何操作并从函数返回
isProcessing=true;
if(elCard.classList.contains('fliped')){
返回;
}
//翻转它
elCard.classList.add('fliped');
//这是第一张卡,只保留在全局变量中
如果(elPreviousCard===null){
isProcessing=true;
elPreviousCard=elCard;
}否则{
isProcessing=false;
//从两张卡中获取数据卡属性的值
var card1=elPreviousCard.getAttribute('data-card');
var card2=elCard.getAttribute('data-card');
//不匹配,计划在1秒内将其翻转回去
如果(card1!==card2){
isProcessing=false;
setTimeout(函数(){
elCard.classList.remove('fliped');
elPreviousCard.classList.remove('fliped');
elPreviousCard=null;
isProcessing=true;
}, 1000)
错误。播放();
}否则{
//是的!一场比赛!
flippedCouplesCount++;
elPreviousCard=null;
对。播放();
//所有的牌都翻了!
if(总耦合数===翻转耦合){
audioWin.play();
//最后添加一个按钮来调用resetCard()
document.getElementById(“再次播放”).innerHTML=
“再玩一次”;
}
}
}
}
函数resetCard(){//以擦除翻转的类
var cardclass=document.getElementsByClassName(“卡”);
对于(i=0;i
bump..您实际上并没有在任何地方检查isProcessing
-仅仅设置变量本身不会做任何事情