Search Blog Posts

Shuffling an Array in Actionscript

Using arrays in flash is always a handy and efficient way to work with variables etc. But some times its handy to be able to shuffle the results in the array, for example when creating a quiz to allow a random set of questions.

Using the code below (actionscript 2.0 but should still work in 3.0) we create an array and trace the results.

The Code

Array.prototype.shuffle=function(){
for(i=0;i<this.length;i++){
var tempvar=this[i];
var ranVar=random(this.length);
this[i]=this[ranVar];
this[ranVar]=tempvar;
}
}
myArray=["flash","dreamweaver","illustrator","in-design","photoshop"];
myArray.shuffle();
trace(myArray);

This would return a trace with answers such as:
‘dreamweaver, flash, in-design, photoshop, illustrator’

Tags: , , ,

Post this story to Delicious Post this story to Digg Post this story to Technorati Post this story to SumbleUpon Post this story to Facebook Post this story to Reedit

Leave a Comment

  1. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© Copyright 2003-2008 Minimalistic Designs | Terms | Links | Sitemap
Website Design Liskeard - www.minimalistics.co.uk