jeudi 10 décembre 2020

Why wont if statement execute?

Im a n00b trying to make my first game. I have two code blocks im trying to merge that works on each of their own but when I merge them part of the code wont execute. The code that wont execute starts after the "console.log(life);" statement and i dont get any errors. Thanks a bunch!

<script src="http://koda.nu/simple.js">
var positionBar = 10;
var positionWord = 600;
var positionWordHeight = getRandomIntInclusive(20, 180);
var word;
var score = 0;
var life = 3;

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min + 1)) + min; //The maximum is inclusive and the minimum is inclusive
}

function getRandom(min, max) {
  return Math.trunc(Math.random() * (max - min) + min);
}

var txtFile = new XMLHttpRequest();
txtFile.open("GET", "database.txt", true);
txtFile.onreadystatechange = function() {
  if (txtFile.readyState === 4) {
    // Makes sure the document is ready to parse.
    if (txtFile.status === 200) {
      // Makes sure it's found the file.
      allText = txtFile.responseText;
      lines = txtFile.responseText.split("\n"); // Will separate each line into an array

      wordNo = getRandom(1, lines.length);
      letters = lines[wordNo];
      console.log(letters);

      arrLetters = "";
      arrLetters = letters.split("");
      arrLetters.pop();

      console.log(arrLetters);

      word = arrLetters;
      console.log(life);
      if (life > 0) {
        function update() {
          clearScreen();

          if (positionBar < 600 && positionBar < positionWord) {
            positionBar += 0.1;
            positionWord -= 0.7;
            /* if (onkeydown == word[0]) {
                  text(150, 160, 20, "Points!!");
              } else if (onkeydown != word[0]) {
              }*/
          } else {
            text(50, 150, 40, "Lost one life!");
            life--;
          }
          rectangle(positionBar, 10, 10, 200, "black");
          text(positionWord, positionWordHeight, 20, word.join(""));
          // console.log(word.join(''));
          document.addEventListener("keydown", function(event) {
            const key = event.key;
            if (key == word[0]) {
              word.splice(0, 1);
              score += 1;
            }
          });

          text(20, 240, 20, "Score: " + score + " points");
        }
      } else if (life <= 0) {
        text(50, 150, 40, "Game Over!");
      }
    }
  }
};

txtFile.send(null);
</script>

I tried checking the conditions and they are correct.

Aucun commentaire:

Enregistrer un commentaire