语法错误是什么,为什么非主动玩家获得积分计数

时间:2018-06-15 03:37:20

标签: javascript html dom

我正在构建一个简单的骰子游戏。我在计算未计分的玩家的分数时遇到问题。当它切换玩家时,仍然会计算未激活的玩家。我已经看了很多,但我找不到它。我甚至休息了一会儿,向前移动了一点然后又回来了,仍然找不到它。这是我的代码。

var scores, roundScore, activePlayer;

scores = [0, 0];
roundScore = 0;
activePlayer = 1;

document.querySelector('.dice').style.display = 'none';
document.getElementById('score-0').textContent = '0';
document.getElementById('score-1').textContent = '0';
document.getElementById('current-0').textContent = '0';
document.getElementById('current-1').textContent = '0';

document.querySelector('.btn-roll').addEventListener('click', function 
() {

//ran. number
var dice = Math.floor(Math.random() * 6) + 1;

//    displaying result
var diceDom = document.querySelector('.dice');
diceDom.style.display = 'block';
diceDom.src = 'Assets/dice-' + dice + '.png';

//    update round score IF the rolled # was not a 1
if (dice !== 1) {
    roundScore += dice;
    document.querySelector('#current-' + activePlayer).textContent = 
roundScore;
} else {
    //go to next player
    nextPlayer();
}

});


document.querySelector('.btn-hold').addEventListener('click', function() 
{
// add current score to global
scores[activePlayer] += roundScore;
//update UI
document.querySelector('#score-' + activePlayer).textContent = 
scores[activePlayer];
//check if player won the game


//next player
nextPlayer();
});

function nextPlayer() {
//go to next player
    activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
    roundScore = 0;

    document.getElementById('current-0').textContent = '0';
    document.getElementById('current-1').textContent = '0';

    document.querySelector('.player-0- 
    panel').classList.toggle('active');
    document.querySelector('.player-1- 
  panel').classList.toggle('active');
    //hide dice when player rolls 1
    document.querySelector('.dice').style.display = 'none';
 }

0 个答案:

没有答案