移动计数器显示结果比预期少一

时间:2019-08-16 15:42:41

标签: javascript html

我正在开发一个记忆游戏,我实现了一个移动计数器来计算完成游戏的尝试(移动)次数。但是我得到的结果比预期的少一。 (即,如果期望为9,我得到8)。帮帮我。

return this.aFirebase.verifyPhoneNumber(+1949-XXX-XXXX,120).then((res)=>{
  return res;
}).catch((error)=>{
  return error;
})

计算动作的功能:

var matches = 0;
var moves = 0;
var counter = document.querySelector(".moves");

该计数是在记忆游戏中第二次单击时进行的,并且被视为第一步。

function moveCounter(){
++moves;
//counter.innerHTML = moves;
}

游戏结束时,应该显示招式

for (i = 0; i < buttons.length; i++)
{
buttons[i].addEventListener('click', function (e) {
var turnable = e.target.dataset.turnable;


//first click
if (!wait && lastKnownButtonId == undefined && lastKnownButtonNumber == undefined && turnable == 'true') {
  e.target.dataset.turnable = 'false';

  e.target.innerHTML = getgImage(event.target.dataset.number);
  e.target.style.backgroundColor = 'yellow';

  lastKnownButtonId = e.target.id;
  lastKnownButtonNumber = e.target.dataset.number;
 }

//second click
else if (!wait && lastKnownButtonId != undefined && lastKnownButtonNumber != undefined && turnable == 'true' && e.target.id != lastKnownButtonId) {
  e.target.dataset.turnable = 'false';

  e.target.innerHTML = getgImage(event.target.dataset.number);




  //match
  if (e.target.dataset.number == lastKnownButtonNumber) {
    e.target.style.backgroundColor = '#00FF7F';
    document.getElementById(lastKnownButtonId).style.backgroundColor = '#00FF7F';

    lastKnownButtonId = undefined;
    lastKnownButtonNumber = undefined;

    matches++;


    if (matches == 8) {

    showWinScreen();

    document.getElementById("finalMove").innerHTML = moves;


    }

  }

  //no match
  else {
    document.getElementById(lastKnownButtonId).style.backgroundColor = 'red';
    e.target.style.backgroundColor = 'red';
    wait = true;

    setTimeout(() => {
      e.target.dataset.turnable = 'true';
      e.target.style.backgroundColor = 'white'
      e.target.innerHTML = getgImage(0);


      var tempLastClickedButton = document.getElementById(lastKnownButtonId);

      tempLastClickedButton.dataset.turnable = 'true';
      tempLastClickedButton.style.backgroundColor = 'white';
      tempLastClickedButton.innerHTML = getgImage(0);

      lastKnownButtonId = undefined;
      lastKnownButtonNumber = undefined;
      wait = false;
    }, 1000);


  }
  moveCounter();
}

});
}

假设“我期望moves的输出= 12,但我得到的输出= 11”

0 个答案:

没有答案