作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试编码Wack-A-Mole,但是有一些问题。我目前有两个return语句,但是当我将stopper3和stopper4设置为true并在Trigger中重新运行该函数时。这是行不通的。另外,我编写了diwn两个函数来重复检查stopper1和stopper2是否为true,然后再次运行,但我不知道如何循环使用它们。我不希望有一天,setTimeout无法正常工作,并且我不了解回调。
我要问的是,如何循环2个Cycle函数,以及为什么在函数的停止器再次设置为true之后,return语句不会取消返回。
<!DOCTYPE html>
<html>
<head>
<title>Whack-A-Mole</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<center>
<p>Try to hit the Wack-a-moles!</p>
<button id="start">Click here to start!</button>
<button id="restart">Restart</button>
<button id="pause">Pause</button>
<button id="resume">Resume</button>
<p id= highscore>Highscore: 0 points!</p>
<p id="time"></p>
<div id="game">
<br>
<img src="moleman.png" alt="Please refresh" id="b1">
<img src="moleman2.png" alt="Please refresh" id="bb1">
<img src="moleman.png" alt="Please refresh" id="b2">
<img src="moleman2.png" alt="Please refresh" id="bb2">
<img src="moleman.png" alt="Please refresh" id="b3">
<img src="moleman2.png" alt="Please refresh" id="bb3">
<br>
<img src="moleman.png" alt="Please refresh" id="b4">
<img src="moleman2.png" alt="Please refresh" id="bb4">
<img src="moleman.png" alt="Please refresh" id="b5">
<img src="moleman2.png" alt="Please refresh" id="bb5">
<img src="moleman.png" alt="Please refresh" id="b6">
<img src="moleman2.png" alt="Please refresh" id="bb6">
<br>
<img src="moleman.png" alt="Please refresh" id="b7">
<img src="moleman2.png" alt="Please refresh" id="bb7">
<img src="moleman.png" alt="Please refresh" id="b8">
<img src="moleman2.png" alt="Please refresh" id="bb8">
<img src="moleman.png" alt="Please refresh" id="b9">
<img src="moleman2.png" alt="Please refresh" id="bb9">
</div>
<p>Your score is:</p>
<p id="T">0 points!</p>
<script type= "text/javascript">
document.getElementById("b1").addEventListener("click", Score);
document.getElementById("b2").addEventListener("click", Score);
document.getElementById("b3").addEventListener("click", Score);
document.getElementById("b4").addEventListener("click", Score);
document.getElementById("b5").addEventListener("click", Score);
document.getElementById("b6").addEventListener("click", Score);
document.getElementById("b7").addEventListener("click", Score);
document.getElementById("b8").addEventListener("click", Score);
document.getElementById("b9").addEventListener("click", Score);
document.getElementById("start").addEventListener("click", Start);
document.getElementById("pause").addEventListener("click", Pause);
document.getElementById("resume").addEventListener("click", Resume);
document.getElementById("restart").addEventListener("click", Restart);
var score = 0;
function Score() {
score++;
if(score == 1){
document.getElementById("T").innerHTML = score + ' point!';
} else {
document.getElementById("T").innerHTML = score + ' points!';
}
document.getElementById('b1').style.display = "none";
document.getElementById('bb1').style.display = "inline";
document.getElementById('b2').style.display = "none";
document.getElementById('bb2').style.display = "inline";
document.getElementById('b3').style.display = "none";
document.getElementById('bb3').style.display = "inline";
document.getElementById('b4').style.display = "none";
document.getElementById('bb4').style.display = "inline";
document.getElementById('b5').style.display = "none";
document.getElementById('bb5').style.display = "inline";
document.getElementById('b6').style.display = "none";
document.getElementById('bb6').style.display = "inline";
document.getElementById('b7').style.display = "none";
document.getElementById('bb7').style.display = "inline";
document.getElementById('b8').style.display = "none";
document.getElementById('bb8').style.display = "inline";
document.getElementById('b9').style.display = "none";
document.getElementById('bb9').style.display = "inline";
Highscore();
}
function Score0() {
score = 0;
document.getElementById("T").innerHTML = '0 points!';
}
var highscore = 0;
function Highscore() {
if(score > highscore) {
highscore = score;
}
if(highscore == 1){
document.getElementById("highscore").innerHTML = 'Highscore: ' + highscore + ' point!';
} else {
document.getElementById("highscore").innerHTML = 'Highscore: ' + highscore + ' points!';
}
}
var stopper1;
var stopper3;
var stopper4;
function Trigger() {
document.getElementById('b1').style.display = "inline";
document.getElementById('bb1').style.display = "none";
document.getElementById('b2').style.display = "inline";
document.getElementById('bb2').style.display = "none";
document.getElementById('b3').style.display = "inline";
document.getElementById('bb3').style.display = "none";
document.getElementById('b4').style.display = "inline";
document.getElementById('bb4').style.display = "none";
document.getElementById('b5').style.display = "inline";
document.getElementById('bb5').style.display = "none";
document.getElementById('b6').style.display = "inline";
document.getElementById('bb6').style.display = "none";
document.getElementById('b7').style.display = "inline";
document.getElementById('bb7').style.display = "none";
document.getElementById('b8').style.display = "inline";
document.getElementById('bb8').style.display = "none";
document.getElementById('b9').style.display = "inline";
document.getElementById('bb9').style.display = "none";
if(stopper3 === false) {
return;
}
document.getElementById("b1").style.visibility = "hidden";
document.getElementById("b2").style.visibility = "hidden";
document.getElementById("b3").style.visibility = "hidden";
document.getElementById("b4").style.visibility = "hidden";
document.getElementById("b5").style.visibility = "hidden";
document.getElementById("b6").style.visibility = "hidden";
document.getElementById("b7").style.visibility = "hidden";
document.getElementById("b8").style.visibility = "hidden";
document.getElementById("b9").style.visibility = "hidden";
document.getElementById("bb1").style.visibility = "hidden";
document.getElementById("bb2").style.visibility = "hidden";
document.getElementById("bb3").style.visibility = "hidden";
document.getElementById("bb4").style.visibility = "hidden";
document.getElementById("bb5").style.visibility = "hidden";
document.getElementById("bb6").style.visibility = "hidden";
document.getElementById("bb7").style.visibility = "hidden";
document.getElementById("bb8").style.visibility = "hidden";
document.getElementById("bb9").style.visibility = "hidden";
if(stopper4 === false) {
return;
}
var x = Math.floor((Math.random() * 9) + 1);
if (x == 1) {
document.getElementById("b1").style.visibility = "visible";
document.getElementById("bb1").style.visibility = "visible";
} else if (x == 2){
document.getElementById("b2").style.visibility = "visible";
document.getElementById("bb2").style.visibility = "visible";
} else if (x == 3){
document.getElementById("b3").style.visibility = "visible";
document.getElementById("bb3").style.visibility = "visible";
} else if (x == 4){
document.getElementById("b4").style.visibility = "visible";
document.getElementById("bb4").style.visibility = "visible";
} else if (x == 5){
document.getElementById("b5").style.visibility = "visible";
document.getElementById("bb5").style.visibility = "visible";
} else if (x == 6){
document.getElementById("b6").style.visibility = "visible";
document.getElementById("bb6").style.visibility = "visible";
} else if (x == 7){
document.getElementById("b7").style.visibility = "visible";
document.getElementById("bb7").style.visibility = "visible";
} else if (x == 8){
document.getElementById("b8").style.visibility = "visible";
document.getElementById("bb8").style.visibility = "visible";
} else if (x == 9){
document.getElementById("b9").style.visibility = "visible";
document.getElementById("bb9").style.visibility = "visible";
}
function Cycle1() {
if (stopper1) {
setTimeout(Trigger,1000);
}
}
}
var stopper2;
var showtime = 61;
function Time() {
showtime--;
if(showtime == 1){
document.getElementById('time').innerHTML = showtime + " second left!";
} else {
document.getElementById('time').innerHTML = showtime + " seconds left!";
}
if (showtime < 0) {
document.getElementById('time').innerHTML = "Time's up!";
stopper1 = false;
stopper2 = false;
document.getElementById('pause').style.display = "none";
document.getElementById('resume').style.display = "none";
}
function Cycle2() {
if (stopper2) {
setTimeout(Time,1000);
}
}
}
function Time61() {
showtime = 61;
}
function Start() {
stopper1 = true;
stopper3 = true;
stopper4 = true;
Trigger();
stopper2 = true;
Time();
document.getElementById("start").style.display = "none";
document.getElementById("pause").style.display = "inline";
document.getElementById("restart").style.display = "inline";
document.getElementById("time").style.display = "inline";
}
function Pause() {
document.getElementById('resume').style.display = "inline";
document.getElementById('pause').style.display = "none";
stopper1 = false;
stopper2 = false;
stopper3 = false;
}
function Resume() {
document.getElementById('pause').style.display = "inline";
document.getElementById('resume').style.display = "none";
stopper1 = true;
stopper2 = true;
stopper3 = true;
}
function Restart() {
document.getElementById("start").style.display = "inline";
document.getElementById("pause").style.display = "none";
document.getElementById("resume").style.display = "none";
document.getElementById("restart").style.display = "none";
document.getElementById("time").style.display = "none";
Score0();
Time61();
stopper1 = false;
stopper2 = false;
stopper4 = false;
}
</script>
</center>
</body>
</html>
:html {
font-size:40px;
}
#time {
}
button#start {
margin-top:-100px;
font-size:24px;
width:300px;
height:100px;
}
button#pause {
display:none;
}
button#resume {
display: none;
}
button#restart {
display: none;
}
img {
width:100px;
height:65px;
}
#b1 {
visibility: hidden;
}
#b2 {
visibility: hidden;
}
#b3 {
visibility: hidden;
}
#b4 {
visibility: hidden;
}
#b5 {
visibility: hidden;
}
#b6 {
visibility: hidden;
}
#b7 {
margin-left:-12px;
visibility: hidden;
}
#b8 {
visibility: hidden;
}
#b9 {
visibility: hidden;
}
#bb1 {
display: none;
visibility: hidden;
}
#bb2 {
display: none;
visibility: hidden;
}
#bb3 {
display: none;
visibility: hidden;
}
#bb4 {
display: none;
visibility: hidden;
}
#bb5 {
display: none;
visibility: hidden;
}
#bb6 {
display: none;
visibility: hidden;
}
#bb7 {
margin-left:-12px;
display: none;
visibility: hidden;
}
#bb8 {
display: none;
visibility: hidden;
}
#bb9 {
display: none;
visibility: hidden;
}
#T {
margin-top:-50px;
text-decoration: underline;
}
最佳答案
回油塞
我不清楚返回值,但是有没有缘由从未将stopper4
设置为true
?您将stopper4
设置为false
两次或三次,但从未设置为true
。
循环功能
这是循环两个循环功能的方式:
(function Cycle1() {
if (stopper1) {
setTimeout(Trigger, 1000);
}
})();
(function(){
})();
关于javascript - Whack-A-Mole JavaScript返回和循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080038/
我是一名优秀的程序员,十分优秀!