Pick random Element of an Array Actionscript 3

前端 未结 2 1770
猫巷女王i
猫巷女王i 2021-01-05 04:46

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

how can I do that?

thank you for your time

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-05 05:00

    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];
    }
    

提交回复
热议问题