переменная меняется на true до завершения setInterval - PullRequest
0 голосов
/ 28 апреля 2018

showMoves - это функция, показывающая мигание для игры в симоны.

Когда мигающие огни закончились, я очищаю интервал, чтобы остановить его, а затем я устанавливаю game.playerTurn на true, чтобы я мог нажимать на цвета, но game.playerTurn меняется на true, как только showMoves активирован.

Я хочу, чтобы game.playerTurn оставался false до тех пор, пока функция showMoves не начнет мигать.

Вот функции, которые я использую game.playerTurn in -

game.playerTurn = false;

//for flashing lights
function showMoves() {
    let i = 0;

    const start = setInterval(function () {
        if (i >= game.computerMoves.length) {
            clearInterval(start);
            game.playerTurn = true;
            return;
        }

        const move = game.computerMoves[i];
        setLight(move, true);
        setTimeout(setLight.bind(null, move, false), 1000); //Using bind to preset arguments

        i++;
    }, 2000);
}

function setLight(color, isOn) {
    if (isOn) {
        sounds[color.id].play();
    }
    color.style.backgroundColor = isOn ? colors[0].get(color) : colors[1].get(color);
}


//compareMoves is fired everytime I click on a color
function compareMoves(e) {
    if (e === game.computerMoves[game.counter]) {
        game.counter++;
        //This is if all the moves were chosen correctly
        if (game.playerMoves.length === game.computerMoves.length && e === game.computerMoves[game.computerMoves.length - 1]) {
            simonHTML.displayScore.textContent = ++game.score;
            game.playerTurn = false;
            resetMoves();
            randomMoves(++game.turn);
            showMoves();
            game.counter = 0;
        }
    } else if (game.strict) {

        //if your move was wrong do this
    } else {
        game.playerMoves = [];
        game.counter = 0;
        game.playerTurn = false;
        showMoves();
        return false;
    }
}

Буду признателен за любую помощь в этом. Вот ссылка на игру и весь код https://codepen.io/icewizard/pen/JLBpNQ

1 Ответ

0 голосов
/ 28 апреля 2018

Где вы устанавливаете game.playerTurn обратно в false?

function showMoves() {
  game.playerTurn = false;
  let i = 0;

  const start = setInterval(function() {
  if (i >= game.computerMoves.length) {
      clearInterval(start);
      game.playerTurn = true;
      return;
  }

  const move = game.computerMoves[i];
  setLight(move, true);
  setTimeout(setLight.bind(null, move, false), 1000); //Using bind to preset arguments

  i++;
}, 2000);
}

Похоже, у меня работает пример кода, который вы предоставили

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...