2011-05-23 40 views
1

嗨大家 我需要更改动作脚本中输出的随机数字。 你会看到我得到5个不同位置的5个随机数字,但它限制了每个位置的数量。我可以在动作脚本中更改我的随机数字

_loc_2 = Math.floor(Math.random() * 4) + 1; 
Cone.text = "" + _loc_2; 
_loc_3 = Math.floor(Math.random() * 4) + 5; 
Ctwo.text = "" + _loc_3; 
_loc_4 = Math.floor(Math.random() * 2) + 9; 
Cthree.text = "" + _loc_4; 
_loc_5 = Math.floor(Math.random() * 2) + 11; 
Cfour.text = "" + _loc_5; 
_loc_6 = Math.floor(Math.random() * 3) + 13; 
Cfive.text = "" + _loc_6; 

我需要将其更改此随机1到15,但不必产生重复号码,

_loc_2 = Math.floor(Math.random() * 15) + 1; 
Cone.text = "" + _loc_2; 
_loc_3 = Math.floor(Math.random() * 15) + 1; 
Ctwo.text = "" + _loc_3; 
_loc_4 = Math.floor(Math.random() * 15) + 1; 
Cthree.text = "" + _loc_4; 
_loc_5 = Math.floor(Math.random() * 15) + 1; 
Cfour.text = "" + _loc_5; 
_loc_6 = Math.floor(Math.random() * 15) + 1; 
Cfive.text = "" + _loc_6; 

可这个动作脚本来完成?

在此先感谢 戴夫

回答

1

所以,你想,没有重复生成随机顺序的编号1-15?

可能有不少方法可以做到这一点,但这里有一个我的头顶部:

var numberSource:Array = []; 

function initArray(maxValue:int):void { 
for (var i:int = 0; i < maxValue; i++) { 
    numberSource[i] = i + 1; 
} 
} 

function getNumber():int { 
var index:int = int(Math.random * (numberSource.length - 1)); 
return numberSource.splice(index, 1); 
} 

所以你会打电话initArray(15),那么你会打电话getNumber()每次它会弹出你的一个值并返回它。你永远不应该得到一个副本,你最终应该得到每一个数字。我没有测试过,所以有可能在那里出现一些故障,但这应该至少给你一个开始的好地方! :)

+0

好吧我会尝试,看起来很有希望 – DaveOh 2011-05-23 20:27:48

+0

并感谢您的快速回复 – DaveOh 2011-05-23 20:28:45

相关问题