如何一次掷一个骰子?

时间:2016-10-12 23:43:28

标签: javascript dice

所以这就是我到目前为止所拥有的。我是javascript的新手。如何在div中按顺序制作这些骰子?例如仅在显示第一个随机数之后才显示下一个随机数。

<html>
<style>
.numdiv{
height: 400px;
width: 200px;
border:solid 1px green; 
float: left
}
</style>

<head>
<script>
function rollDice(){
var number1 = document.getElementById("number1");
var number2 = document.getElementById("number2");
var number3 = document.getElementById("number3");
var status = document.getElementById("status");
var d1 = Math.floor(Math.random() * 6) + 1;
var d2 = Math.floor(Math.random() * 6) + 1;
var d3 = Math.floor(Math.random() * 6) + 1;
var diceTotal = d1 + d2 + d3;

    number1.innerHTML = d1;
    number2.innerHTML = d2;
    number3.innerHTML = d3;
    status.innerHTML = "You rolled "+diceTotal+".";
    if(diceTotal == 15){
        status.innerHTML += "You Win!!";
    }
}
</script>
</head>
<body>
<table>
<tr>
<td>
<div class="numdiv" id="number1">

0</div>
<div class="numdiv"  id="number2">
0</div>
<div class="numdiv"  id="number3">
0</div>
</td>
</tr>
<tr>
<td><button onclick="rollDice()"/>Generate Number</button></td></tr>
<tr>
<td><span id="status">0</span></td></tr>
</table>

</body>


</html>

3 个答案:

答案 0 :(得分:2)

你可以使用setTimeout() method来延迟执行一个函数,这样就可以一次滚动一个骰子:

function rollDice(){
  var status = document.getElementById("status");
  var nums = document.querySelectorAll(".numdiv");
  var diceTotal = 0;
  var current;
  
  for (current = 0; current < nums.length; current++) {
    nums[current].innerHTML = "?";
  }
  
  current = 0;
  status.innerHTML = "Rolling...";
  
  function rollNext() {
    currentRoll = Math.floor(Math.random() * 6) + 1;
    nums[current].innerHTML = currentRoll;
    diceTotal += currentRoll;
    current++;
    if (current < nums.length) {
      setTimeout(rollNext, 1000);
    } else {
      status.innerHTML = "You rolled " + diceTotal + ".";
      if(diceTotal == 15){
        status.innerHTML += "You Win!!";
      }    
    }
  }
  setTimeout(rollNext, 1000);
}
.numdiv { height: 30px; width: 30px; border:solid 1px green; float: left; margin: 10px }
<table>
<tr>
  <td>
    <div class="numdiv" id="number1"></div>
    <div class="numdiv"  id="number2"></div>
    <div class="numdiv"  id="number3"></div>
  </td>
</tr>
<tr>
  <td><button onclick="rollDice()"/>Generate Number</button></td></tr>
<tr>
  <td><span id="status">0</span></td>
</tr>
</table>

另请注意,我已删除了单独的number1number2number3变量,而是使用单个变量num来引用所有变量的列表.numDiv div元素。这样你就可以在不改变JS的情况下轻松改变想要使用的骰子数量(你只需要添加或删除.numDiv元素)。

编辑:当我发布上述内容时,您明确表示您希望用户按下每个模具的按钮,而不是自动滚动所有三个模具。所以我想,也许类似于以下内容,保持一个计数器和变量声明的函数:

  var statusSpan = document.getElementById("status");
  var nums = document.querySelectorAll(".numdiv");
  var diceTotal;
  var current = 0;

  function rollDice(){
    if (current === 0) { // reset display
      for (var i = 0; i < nums.length; i++)
        nums[i].innerHTML = "?";
      diceTotal = 0;
    }
    
    currentRoll = Math.floor(Math.random() * 6) + 1;
    nums[current].innerHTML = currentRoll;
    diceTotal += currentRoll;
    if (++current < nums.length) {
      statusSpan.innerHTML = "Current total: " + diceTotal + ".";
    } else {
      statusSpan.innerHTML = "You rolled " + diceTotal + ".";
      if(diceTotal == 15){
        statusSpan.innerHTML += "You Win!!";
      }
      current = 0;
    }
  }
.numdiv { height: 30px; width: 30px; border:solid 1px green; float: left; margin: 10px; text-align: center }
<table>
<tr>
  <td>
    <div class="numdiv" id="number1"></div>
    <div class="numdiv"  id="number2"></div>
    <div class="numdiv"  id="number3"></div>
  </td>
</tr>
<tr>
  <td><button onclick="rollDice()">Roll next die</button></td></tr>
<tr>
  <td><span id="status">Ready to roll?</span></td>
</tr>
</table>

答案 1 :(得分:1)

创建一个从rollDice函数外部开始的计数器变量,每次单击该变量,然后将计数器增加1。然后你需要用这个替换所有的数字变量:

var number = document.getElementById("number" + counter);

我想你知道从哪里开始。如果您需要更多指导,请告诉我们!

答案 2 :(得分:0)

以您现有的逻辑为基础,如果您希望每次用户点击时显示1,那么替代方法....

    var totalIter = 0;//global var which is set to increments on each click
    var diceTotal = 0;

    function rollDice() {
        totalIter = totalIter + 1; //increment by 1
        //Existing logic from your script
        var ele = document.getElementById("number" + totalIter);
        var d1 = Math.floor(Math.random() * 6) + 1;
        var status = document.getElementById("status");
        diceTotal = diceTotal + d1;
        ele.innerHTML = d1;
        status.innerHTML = "You rolled " + diceTotal + ".";
        if (diceTotal == 15) {
            status.innerHTML += "You Win!!";
        }
    }