选择随机元素的数组中的ActionScript 3组中、元素、ActionScript

2023-09-08 11:09:05 作者:随疯奔跑

我有影片剪辑的数组,我希望把他们在舞台上。所以他们必须是唯一的,随机选择的。

I have an array of movieclips and i want to put them on stage. so they have to be unique and randomly chosen.

我该怎么办呢?

感谢您的时间

推荐答案

您可以用得到一个随机数的Math.random()这将返回的0号和1。

You can get a random number using Math.random() This will return a number between 0 and 1.

因此​​,获取数组的一个随机元素,使用这样的:

So, for getting a random element of the array, use this:

function getRandomElementOf(array:Array):Object {
    var idx:int=Math.floor(Math.random() * array.length);
    return array[idx];
}