我正在尝试用javascript制作忍者水果风格的游戏,但问题正在发生。我有这个if语句,它将变量“ fruit”与“ fruits”数组的索引进行比较。问题是当我“消除”一个水果时,另一个if语句确实起作用。

游戏需要这样运作:

1启动游戏后,将出现一个随机的水果名称供您单击。

2单击水果图像,该图像消失,在此单击中,将生成另一个随机水果。

3然后您完成游戏,这很重要。

因此很难解释,但其逻辑与忍者水果游戏相同。而且我不知道是否需要使用shift函数来消除数组中的结果。



var fruits = ['Banana', 'Apple', 'Pineapple'];
var fruit = fruits[Math.floor(Math.random() * fruits.length)];
document.getElementById("frut").innerHTML = fruit;
if (fruit == fruits[0]) {
  bana.onclick = function() {
    var fruit = fruits[Math.floor(Math.random() * fruits.length)];

    document.getElementById("frut").innerHTML = fruit;

    bana.style.display = "none";
  }
}
if (fruit == fruits[1]) {
  app.onclick = function() {
    var fruit = fruits[Math.floor(Math.random() * fruits.length)];
    document.getElementById("frut").innerHTML = fruit;
    app.style.display = "none";
  }
}
if (fruit == fruits[2]) {

  pin.onclick = function() {
    var fruit = fruits[Math.floor(Math.random() * fruits.length)];
    document.getElementById("frut").innerHTML = fruit;
    pin.style.display = "none";
  }
}
function movFruit() {

  document.getElementById("info").style.display = "table";
  document.getElementById("fruitAnimation").style.display = "table";
  document.getElementById("insructions").style.display = "none";

  var elem = document.getElementById("fruitAnimation");
  var pos = 0;
  var id = setInterval(frame, 10);

  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++;
      elem.style.top = pos + 'px';
    }
  }
}

#fruitAnimation {
  position: relative;
  display: none;
  margin: 0 auto;
}

.fr {
  float: left;
  padding: 80px;
}

#info {
  display: none;
  margin: 0 auto;
}

#insructions {
  display: table;
  margin: 0 auto;
  margin-top: 200px;
  border: 1px solid black;
  padding: 10px;
}

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

  <title>JSfruit</title>
</head>





<body>
  <div id="info">
    <h1>Fruit: <span id="frut"></span></h1>
  </div>


  <button onclick="movFruit() " style="display: table; margin: 0 auto;"><h4>Start the game</h4></button>




  <div id="fruitAnimation">
    <div class="fr" id="bana">
      <img src="https://orig00.deviantart.net/5c87/f/2016/322/8/9/banana_pixel_art_by_fireprouf-daosk9z.png" width="60" height="60">
    </div>
    <div class="fr" id="app">
      <img src="https://art.ngfiles.com/images/404000/404664_thexxxreaper_pixel-apple.png?f1454891997" width="60" height="60">
    </div>
    <div class="fr" id="pin">
      <img src="https://i.pinimg.com/originals/c2/f9/e9/c2f9e9f8d332da97a836513de98f7b29.jpg" width="60" height="60">
    </div>
  </div>

  <span id="insructions">Click in the fruits and erase them!</span>


</body>

</html>

最佳答案

现在,您只在顶层的if语句中将处理程序附加到水果图像上-但是一旦这些语句运行并且主块完成,就不会再次运行它。

首先应将处理程序附加到所有水果图像,然后在处理程序中检查单击的水果是否有效。

如果要为元素分配文本,请分配给textContent,而不是innerHTMLtextContent更快,更安全且更可预测。



const fruits = ['Banana', 'Apple', 'Pineapple'];
const getRandomFruit = () => {
  const randomIndex = Math.floor(Math.random() * fruits.length);
  const fruit = fruits[randomIndex];
  document.getElementById("frut").textContent = fruit;
  fruits.splice(randomIndex, 1);
  return fruit;
};
let fruitToClickOn = getRandomFruit();


bana.onclick = function() {
  if (fruitToClickOn !== 'Banana') return;
  bana.style.display = "none";
  fruitToClickOn = getRandomFruit();
}
app.onclick = function() {
  if (fruitToClickOn !== 'Apple') return;
  app.style.display = "none";
  fruitToClickOn = getRandomFruit();
}
pin.onclick = function() {
  if (fruitToClickOn !== 'Pineapple') return;
  pin.style.display = "none";
  fruitToClickOn = getRandomFruit();
}


function movFruit() {

  document.getElementById("info").style.display = "table";
  document.getElementById("fruitAnimation").style.display = "table";
  document.getElementById("insructions").style.display = "none";

  var elem = document.getElementById("fruitAnimation");
  var pos = 0;
  var id = setInterval(frame, 10);

  function frame() {
    if (pos == 350) {
      clearInterval(id);
    } else {
      pos++;
      elem.style.top = pos + 'px';
    }
  }
}

#fruitAnimation {
  position: relative;
  display: none;
  margin: 0 auto;
}

.fr {
  float: left;
  padding: 80px;
}

#info {
  display: none;
  margin: 0 auto;
}

#insructions {
  display: table;
  margin: 0 auto;
  margin-top: 200px;
  border: 1px solid black;
  padding: 10px;
}

<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">

  <title>JSfruit</title>
</head>





<body>
  <div id="info">
    <h1>Fruit: <span id="frut"></span></h1>
  </div>


  <button onclick="movFruit() " style="display: table; margin: 0 auto;"><h4>Start the game</h4></button>




  <div id="fruitAnimation">
    <div class="fr" id="bana">
      <img src="https://orig00.deviantart.net/5c87/f/2016/322/8/9/banana_pixel_art_by_fireprouf-daosk9z.png" width="60" height="60">
    </div>
    <div class="fr" id="app">
      <img src="https://art.ngfiles.com/images/404000/404664_thexxxreaper_pixel-apple.png?f1454891997" width="60" height="60">
    </div>
    <div class="fr" id="pin">
      <img src="https://i.pinimg.com/originals/c2/f9/e9/c2f9e9f8d332da97a836513de98f7b29.jpg" width="60" height="60">
    </div>
  </div>

  <span id="insructions">Click in the fruits and erase them!</span>


</body>

</html>

关于javascript - JavaScript中的水果忍者类型游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50033321/

10-12 12:39