简体   繁体   English

无法使JavaScript程序正常工作

[英]can't get javascript program to work

I'm learning javascript and I'm working on a small dice game. 我正在学习JavaScript,并且正在从事小型骰子游戏。 In the game you click the mouse and the dice "rolls" and you get a new side of the dice. 在游戏中,单击鼠标,骰子“滚动”,您将获得骰子的新面。 I just change the face of the die to match the number rolled. 我只是改变骰子的面孔以匹配滚动数。 I can't seem to get it to work. 我似乎无法正常工作。 When I click the mouse does the random number once but it's always the else. 当我单击鼠标时,随机数会执行一次,但总是其他。 It equals 6 or greater and I can't get it to roll again. 它等于6或更大,我无法再次滚动。 When you click the mouse it's supposed to create a random number between 0 5 and then the if statement that corresponds is supposed to show that side of the dice. 当您单击鼠标时,应该创建一个介于0 5之间的随机数,然后对应的if语句应该显示骰子的这一侧。 I've been racking my brain for over an hour and can't seem to figure why it's not working. 我已经动脑了一个多小时,似乎无法弄清为什么它不起作用。 Can someone look at my code and tell me what I am doing wrong. 有人可以看看我的代码并告诉我我在做什么错。 Please! 请!

var dice = 0;

function setup() {
  createCanvas(600, 400)
}

function draw() {
  background(0);
  if (dice == 0) {
    rectMode(CENTER);
    fill(255);
    rect(300, 200, 50, 50);
    rectMode(CENTER);
    fill(0);
    ellipse(300, 200, 5, 5)
  } else if (dice == 1) {
    rectMode(CENTER);
    fill(255);
    rect(300, 200, 50, 50);
    fill(150);
    ellipse(290, 190, 5, 5);
    fill(150);
    ellipse(310, 210, 5, 5);
  } else if (dice == 2) {
    rectMode(CENTER);
    fill(255);
    rect(300, 200, 50, 50);
    fill(150);
    ellipse(290, 190, 5, 5);
    fill(150);
    ellipse(310, 210, 5, 5);
    fill(150)
    ellipse(290, 210, 5, 5)
  } else if (dice == 3) {
    rectMode(CENTER);
    fill(255);
    rect(300, 200, 50, 50);
    fill(150);
    ellipse(290, 190, 5, 5);
    fill(150);
    ellipse(310, 210, 5, 5);
    fill(150)
    ellipse(290, 210, 5, 5);
    fill(150);
    ellipse(310, 190, 5, 5)
  } else if (dice == 4) {
    rectMode(CENTER);
    fill(255);
    rect(300, 200, 50, 50);
    fill(150);
    ellipse(290, 190, 5, 5);
    fill(150);
    ellipse(310, 210, 5, 5);
    fill(150)
    ellipse(290, 210, 5, 5);
    fill(150);
    ellipse(310, 190, 5, 5);
    fill(150);
    ellipse(300, 200, 5, 5)
  } else {
    rectMode(CENTER);
    fill(255);
    rect(300, 200, 50, 50);
    fill(150);
    ellipse(290, 190, 5, 5);
    fill(150);
    ellipse(310, 210, 5, 5);
    fill(150)
    ellipse(290, 210, 5, 5);
    fill(150);
    ellipse(310, 190, 5, 5);
    fill(150);
    ellipse(300, 210, 5, 5);
    fill(150);
    ellipse(300, 190, 5, 5)
  }
}

function mousePressed() {
  dice = random(5);
}

use this instead of dice = random(5) 用它代替dice = random(5)

dice = Math.floor(Math.random() * 6);

Math.random() returns a float number between 0 and 1 we multiple it by 6 then we have a number between 0 to 6 but it is float like 5.22554545 we use Math.floor to round it to the floor number, so it is an integer between 0 to 5 (and 5 itself). Math.random()返回一个介于0和1之间的浮点数,我们将其乘以6,然后得到一个介于0到6之间的数,但是它像5.22554545一样是浮点数,我们使用Math.floor将其四舍五入为下限数,因此它是一个0到5之间的整数(以及5本身)。 ===> 5.22324234 = 5 ===> 5.22324234 = 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM