有人可以解释我如何为游戏建立一个准确性系统,例如我希望有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
}
但似乎没有创造这样的机会。
答案 0 :(得分:4)
那应该有用。 Math.random
将返回0到1之间的随机数,因此您不必将其乘以10.
您还在示例中将randomNum
拼写为radnomNum
,这可能是它无效的原因。
var randomNumber = Math.random();
if (randomNumber <= 0.8) {
//80% chance
} else {
//20% chance
}