首先,讓我解釋一下我的代碼。我創造了一個遊戲,當你「捅」這個怪物時,它會產生一個1到100之間的隨機數。如果這個隨機數等於5,那麼你贏了,如果不是,它說你已經死了,一個計時器會持續2秒,然後刷新頁面,讓您再次「戳」。它只用了一個句子的回覆,但爲了加強它,我想添加一個可能的死亡句子數組,這樣當你點擊圖片而你鬆了,那麼其中一個句子被隨機挑選出來,那就是迴應。作爲迴應從JS數組中挑選的隨機句子
我的JS代碼:
var myTimer;
//Timer that reloads the page
function myTimerF() {
var x = document.getElementById("Button");
x.innerHTML = "<img src='poke.png' >";
clearInterval(myTimer);
}
//generates a random number then checks if you won or not
function randomNumber() {
var res = Math.floor((Math.random() * 100) + 1);
var x = document.getElementById("Button")
if (res == 5)
x.innerHTML = "Winner! YOU ACTUALLY WON! IT'S A MIRICALE!";
else
function getRandomSentence() {
var index = Math.floor(Math.random() * (maxSentences - 1));
return sentences[index];
}
myTimer = setInterval(function() {
myTimerF()
}, 2000);
}
//Random death sentences
var sentences = [
'You just got eaten! Try again in 2 seconds.',
'You are currently being digested. Try again in 2 seconds.',
'You have been incinerated, you may poke the monster in 2 seconds again.',
'Your head has been removed from your body, try again in 2 seconds when we find it.',
'You have been neautrilized. Try again in 2 seconds.',
'You ran away out of fear. Try again in 2 seconds.',
'Your legs are currently in the belly of the monster, try again in 2 seconds.'
],
maxSentences = sentences.length;
我的HTML代碼:
<p id="Button" onclick="randomNumber()">
<img src="poke.png" >
</p>
我的問題是隨機排列不工作。當你點擊圖像按鈕時,沒有任何反應。
'「隨機排列不工作」「 - 以什麼方式? –
這不起作用,當你點擊圖片時,它不起作用。沒有任何反應 – Timble
您的'if ... else'語句看起來有點奇怪(在正確格式化您的代碼後它變得更加明顯)。 –