我正在做一个猪游戏。整个游戏和代码在这里:http ://cisp362.info/johng/CISP362_Pig/Pig.html
我的问题是电脑播放器玩得太快了,你看不到柱子颜色的变化,所有的滚动看起来都像是发生了立刻。我知道 setInterval 和 setTimeout 是我唯一可以用来减慢速度的方法,但我不知道如何重新排列我的代码以使其工作,因为它处于一个取决于变量结果的循环中thisRoll
。我尝试使用 setTimeout,但它具有不可预测的结果,因为其余代码继续运行并且不等待thisRoll
更改rollDie()
. 我尝试使用 setInterval 代替 do while 循环,但我不知道如何合并条件语句和/或之后如何运行清理代码。我只是想让它运行得慢一点。也许我只是盯着这个太久了。
function takeNPCTurn(){
do{
thisRoll = rollDie();
if(thisRoll===1){
document.getElementById("NPCRolls").innerHTML = "------------------------------------<br>You Rolled a 1 and lost your turn!<br>" + document.getElementById("NPCRolls").innerHTML
localStorage.whosTurnIsIt = "Player";
changeColumnColor('col1','lime');
changeColumnColor('col2','transparent');
runningTotal=0;
return;
}else{
runningTotal += thisRoll;
document.getElementById("NPCRolls").innerHTML = "You Rolled a " + thisRoll + ", totalling " + runningTotal+"<br>" + document.getElementById("NPCRolls").innerHTML;
}
}while(runningTotal < 20 && (parseInt(document.getElementById('NPCScore').textContent) + runningTotal) < 100);
//finish up NPC turn and switch back to player
document.getElementById('NPCScore').textContent = parseInt(document.getElementById('NPCScore').textContent) + runningTotal;
document.getElementById("NPCRolls").innerHTML = "------------------------------------<br>You held at " + runningTotal + ", bringing your score to " + document.getElementById('NPCScore').textContent + "<br>" + document.getElementById("NPCRolls").innerHTML;
runningTotal=0;
localStorage.whosTurnIsIt = "Player";
changeColumnColor('col1','lime');
changeColumnColor('col2','transparent');
if(parseInt(document.getElementById("NPCScore").textContent) >= 100){alert (losingMessage);}
}