这是我正在制作的纸牌游戏。密码笔位于LINK

问题在于该代码仅应删除运行状况为0的卡,而是删除所有运行状况级别的卡。在Battle函数中,您将看到我所指的if语句。我正在检查数组中的数字是否为0,但是其中一张卡的健康状况为1,但是它也被删除了。要查看卡,请在调用设置功能时注释掉。



var turn = true;

var enemyCards = document.getElementById('enemy-cards');

var friendlyCards = document.getElementById('friendly-cards');

var friendlyHealth = document.getElementById('friendly-health-value');

var enemyHealth = document.getElementById('enemy-health-value');

var enemyCardArray = [[2, 3], [2, 4]];

var friendlyCardArray = [[3, 3], [3,2]];

function initialSetup() {
  for (var i=0; i < enemyCardArray.length; i++) {
    var card = enemyCardArray[i]
    var damage = card[0];
    var health = card[1];
    enemyCards.innerHTML += "<div class='card'><h1 class='damage'>"+damage+"</h1><h1 class='health'>"+health+"</h1></div>"
  }

  for (var i=0; i < friendlyCardArray.length; i++) {
    var card = friendlyCardArray[i]
    var damage = card[0];
    var health = card[1];
    friendlyCards.innerHTML += "<div class='card'><h1 class='damage'>"+damage+"</h1><h1 class='health'>"+health+"</h1></div>"
  }
}

function setup() {
       enemyCards.innerHTML = "";//<--Set it to "" here, not in for loop
       friendlyCards.innerHTML = "";//<--Set it to "" here, not in for loop
       for (var i = 0; i < enemyCardArray.length; i++) {
            var card = enemyCardArray[i]
            var damage = card[0];
            var health = card[1];
            //enemyCards.innerHTML = "";
            enemyCards.innerHTML += "<div class='card'><h1 class='damage'>" + damage + "</h1><h1 class='health'>" + health + "</h1></div>";
                }

       for (var i = 0; i < friendlyCardArray.length; i++) {
              var card = friendlyCardArray[i]
              var damage = card[0];
              var health = card[1];
              //friendlyCards.innerHTML = "";
              friendlyCards.innerHTML += "<div class='card'><h1 class='damage'>" + damage + "</h1><h1 class='health'>" + health + "</h1></div>";
                }
            }

function battle() {
  if (turn === true){
    for (var i = 0; i<friendlyCardArray.length; i++) {
      if (friendlyCardArray.length == enemyCardArray.length){
        enemyCardArray[i][1] -= friendlyCardArray[i][0];
        if (enemyCardArray[i][1] < 1){
          enemyCardArray.splice(i);
        }else{

        }
      }else{

      }
    }
  }else if (turn === false){

  }
}
initialSetup();
battle();
setup();

body {
  margin: 0;
  font-family: sans-serif;
  position: relative;
}

#enemy-cards{
  background-color: #873a00;
  width: 100%;
  height: 270px;
}

#friendly-cards{
  background-color: #873a00;
  width: 100%;
  height: 270px;
  position: fixed;
  bottom: 0;
}

.card {
  width: 150px;
  height: 250px;
  background-color: #afafaf;
  margin-top: 10px;
  margin-left: 10px;
  margin-bottom: 10px;
  position: relative;
  float:left;
}

.damage {
  text-align: left;
  position: absolute;
  bottom:0;
  left: 20px;
}

.health {
  text-align: right;
  position: absolute;
  bottom:0;
  right: 20px;
}

#friendly-health{
  float:left;
  width: 50%;
  background-color: lightgreen;
  height: 200px;
}
#friendly-health-value{
  position: fixed;
  left: 25%;
  vertical-align: middle;
  color: white;
}

#enemy-health-value{
  position: fixed;
  left: 75%;
  vertical-align: middle;
  color: white;
}

#enemy-health{
  float:left;
  width: 50%;
  background-color: #f73f27;
  height: 200px;
}

<div id="game">
  <div id="enemy-cards">
  </div>
  <div id="health">
    <div id="friendly-health">
      <h1 id="friendly-health-value">20</h1>
    </div>
    <div id="enemy-health">
      <h1 id="enemy-health-value">20</h1>
    </div>
  </div>
  <div id="friendly-cards">
  </div>
</div>

最佳答案

enemyCardArray.splice(i);应该为enemyCardArray.splice(i,1);,以仅删除被检查的卡。

请参见splice documentation及其参数。如果仅传递start参数,它将删除该元素以及此后的所有元素。

关于javascript - HTML,CSS,JS if语句认为1 == 0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43837805/

10-12 12:47