jump to navigation

Simple Example of Randomizing the order of an array in ActionScript August 30, 2010

Posted by Tournas Dimitrios in Actionscript.

Just got asked this by a  mate and after asking Mr Google and looking at the Array Documentation came up with this (Flex based) example, so thought I would share🙂

< ? x ml version="1.0" encoding="utf-8"?>
< m x: A pplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
private function myExample() : void {
//can be an array of anything you like, but using letters here
var myArray   : Array   = ( "a,b,c,d,e,f,g,h,j,k,l" ).split(",");

An alternative approach to assign values to the array
var myArray:Array=new Array("a","b","c","d","e","f","g","h","j","k","l");

//sort the array using my function
myArray.sort( myFunction );

debug.text = myArray.toString() + "\n" + debug.text;

//This function takes in 2 args, but we never use them...
private function myFunction( a : Object , b : Object ) : int {
return ( Math.round( Math.random() * 10 ) - 5);// return a random value above, below or on 0
</ m x: S cript>

< m x: B utton label="Make me Random" click="myExample();"/>
< m x: T extArea id="debug" height="100%" />
</m x: A pplication>


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s