Shuffling Array in Flash
We often need to shuffle array in flash but flash is not having any in-built function to shuffle the array.
Here is code to shuffle the array in a simple manner.
Please convert smart quotes to normal quote before pasting the following code to flash
// Initinal Value of the Array
var aData:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
trace(‘Before Shuffling : aData = {‘+aData+’}');// Before Shuffling : aData = {1,2,3,4,5,6,7,8,9,10}
for (i=0; i<aData.length; i++) {
//This will fetch the value at random index and save it in nData
var nData:Array = aData.splice(randRange(0, aData.length-1), 1);
//This will store the nData value to end of the same array
aData.push(nData[0]);
}
trace(‘After Shuffling : aData = {‘+aData+’}');// After Shuffling : aData = {5,6,1,3,7,10,2,9,8,4}
function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random()*(max-min+1))+min;
return randomNum;
}
on April 21, 2009 on 10:42 am
Good One, Keep the posting up man,
Dont think who is reading and who is not. I strongly believe if the post is work for One Flash developer your efforts to
post is 100% Returned.
Keep It Up
on July 27, 2009 on 8:50 am
nice trick..i will use it on my game ..thanks