创建精度系统

时间:2013-07-01 10:30:32

标签: javascript html5-canvas

有人可以解释我如何为游戏建立一个准确性系统,例如我希望有90%的机会击中,或者可能是60%。我想通过使用这种方法:

var radnomNum = Math.random()*10;

if(randomNum >= 2){
//it will hit , and the chance is about 80%
}else{
// it will miss , 20% chacne
}

但似乎没有创造这样的机会。

1 个答案:

答案 0 :(得分:4)

那应该有用。 Math.random将返回0到1之间的随机数,因此您不必将其乘以10.

您还在示例中将randomNum拼写为radnomNum,这可能是它无效的原因。

var randomNumber = Math.random();
if (randomNumber <= 0.8) {
    //80% chance
} else {
    //20% chance
}