我正在製作一個Whack-A-Mole遊戲,目前我一直在困擾着這個問題:我的遊戲一直運行在生命之後< 0,我只是想知道是否有人想看看我的代碼(特別是無爲布爾),並能告訴我什麼,我做錯了,我只是學習:)這裏是我的代碼:爲什麼我的應用程序繼續運行?
#moleWorld {
height: 330px;
width: 1500px;
margin: 0 auto;
border: 1px solid black;
}
.field {
display: inline-block;
width: 21%;
margin: 27px;
height: 21%;
border: 1px solid black;
background: red;
}
.mole {
display: inline-block;
width: 21%;
margin: 27px;
height: 21%;
border: 1px solid black;
background-color: green;
}
#generalInformation {
height: 40px;
width: 330px;
margin: 0 auto;
background: lightblue;
}
#level-display,
#lifes-display {
margin-left: 30px;
}
#beginEasyClick,
#beginNormalClick,
#beginHardClick {
margin: 40px 45%;
width: 70px;
height: 30px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="../jquery-3.1.1.js">
"use strict";
var currentScore = 2;
var niveau = 0;
var currentLives = 3;
var inAction = false;
var moleworld = "#moleWorld";
var beginEasyClick = document.getElementById("beginEasyClick");
var beginNormalClick = document.getElementById("beginNormalClick");
var beginHardClick = document.getElementById("beginHardClick");
var displayScore = document.getElementById("_displayScore");
var $field = $(moleworld).find(class = "field")
var getrandomInt(function(min, max) {
return Math.floor(Math.random() * (max - min - 1)) + min
})
function randomField() {
return Math.floor(Math.random() * 8)
}
</script>
<script>
var moleworld = "#moleWorld";
var $moleworld = $(moleworld);
currentScore = 0;
currentLives = 5;
inAction = false;
"use strict";
$().ready(function() {
$(beginEasyClick).click(function() {
if (!inAction) {
inAction = true
setInterval(function() {
spawnMole();
}, 1400);
showScore();
isThisTheMole();
}
});
function spawnMole() {
var oldScore = currentScore;
var allFields = new Array();
allFields = document.getElementsByClassName("field")
var target = $(allFields[Math.floor(Math.random() * allFields.length)])
target.addClass("mole");
setTimeout(function() {
target.removeClass("mole")
if (oldScore === currentScore) {
currentLives--;
checkLives();
}
showScore();
}, 1000)
}
function showScore() {
document.getElementById("_displayScore").innerHTML = "<span> Score : " + currentScore + " Lives : " + currentLives + "</span>"
}
$(beginNormalClick).click(function() {
// $("#car").css("background-color" , "green");
inAction = false;
});
function isThisTheMole() {
$("div>div").click(function() {
var clickedField = $(this);
if (clickedField.hasClass("mole")) {
currentScore++;
clickedField.removeClass("mole");
} else {
currentLives--;
}
showScore();
checkLives();
})
}
function checkLives() {
if (currentLives === 0) {
alert("")
inAction = false;
}
}
});
</script>
<p id="car" class="kes">blablacar</p>
<p class="kes">carblabla </p>
<div id="StartMenu"></div>
<button id="beginEasyClick"> Easy </button>
<button id="beginNormalClick"> Normal </button>
<button id="beginHardClick"> Hard </button>
<div id="generalInformation">
<p id="_displayScore"> </p>
</div>
<div id="moleWorld">
<div class="field"> </div>
<div class="field"> </div>
<div class="field"> </div>
<div class="field"> </div>
<div class="field"> </div>
<div class="field"> </div>
<div class="field"> </div>
<div class="field"> </div>
非常感謝您的閱讀!
非常感謝 – THEoneANDonly