JavaScriptでスロットゲームを作っているのですが
絵柄が同じになったときに”おめでとう”、違ったときに”残念”と
表示させたいのですがうまくいきません。
いろいろ調べても解決しなかったのでここで質問させていただきました。
どうか回答のほどよろしくお願いいたします。
</head> <body> <h1>スロットマシーンで遊ぼう!</h1> <div> <button id="start" onclick="start1();start2();">スタート!</button> </div> <table> <thead> <tr><th>スロット1</th><th>スロット2</th></tr> </thead> <tbody> <tr> <td><img src="images/usagi.png" id="picture1"></td> <td><img src="images/usagi.png" id="picture2"></td> </tr> </tbody> </table> <div id="stop_box"> <button id="stop1" onclick="slotstop1()">ストップ1</button> <button id="stop2" onclick="slotstop2()">ストップ2</button> </div> <div id="result"></div> <button id="reset" onclick="restart()">もう一度する!</button> </body> </html><script> var firstOne; var secondOne; function start1() { var firstOne = Math.floor(Math.random() * 4); var image; if (firstOne == 0) { image = "raion.png"; } else if (firstOne == 1) { image = "tora.png"; } else if (firstOne == 2) { image = "zou.png"; } else if (firstOne == 3) { image = "ushi.png"; } else { image = "usagi.png"; } stop1 = setTimeout("start1()",1000); document.getElementById("picture1").src="images/" + image; } function start2() { var secondOne = Math.floor(Math.random() * 4); var image; if (secondOne == 0) { image = "raion.png"; } else if (secondOne == 1) { image = "tora.png"; } else if (secondOne == 2) { image = "zou.png"; } else if (secondOne == 3) { image = "ushi.png"; } else { image = "usagi.png"; } stop2 = setTimeout("start2()",1000); document.getElementById("picture2").src="images/" + image; } function slotstop1() { clearTimeout(stop1); } function slotstop2() { clearTimeout(stop2); } if (firstOne == secondOne) { document.getElementById("result").innerHTML = "<p>おめでとう</p>"; } else { document.getElementById("result").innerHTML = "<p>残念</p>"; } function restart() { location.reload(); } </script>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/11 00:51