mercredi 15 novembre 2017

How to limit an else statement? - Javascript game

I'm looking for an explanation on how I would properly implement a 'button' (square) that you would stand on, which would change the background color. I've successfully implemented the if statement, which changes the background, but when I add an else statement to change it back to normal when you are not on the button, it runs repeatedly without stopping.

The same goes for another obstacle I have that makes you move slower when standing on it. When you're not on the obstacle, it's constantly changing the variable rather than changing it back once.

  if (obstacle === "ice") {
    playerXSpeed = 0.5;
    console.log('slow speed');
  }
  else {
    playerXSpeed = 7;
    console.log('norm speed');
  }
  var changeBackground = document.getElementsByClassName('background');
  if (obstacle === "blind") {
      changeBackground[0].style.background = 'white';
      console.log('purple');
    }
  else {
      changeBackground[0].style.background = 'blue';
      console.log('blue');
  }

The 'blind' obstacle is supposed to change the background only when you are standing on it, and when you are off it's supposed to revert to normal. Same for the ice.

So how would you make this more like a 'switch on' - 'switch off' scenario?

Aucun commentaire:

Enregistrer un commentaire