Howdy. Welcome to our webdesign blog!

Thanks for dropping by! Please make yourself at home by leaving comments and subscribing to the RSS feed to stay updated.

  • Categories:

  • Blogroll

  • Archives:
  • HOW TO: Randomize/Shuffle an array in Flash (AS2)

    January 30, 2010

    I was creating a logo scroller in flash when a client of mine asked me if the logo’s could show up in a random order. This lead me to look to see if Actionscript 2.0 has a built in function to randomize an array. Well, I didn’t find a built in funciton, but i found a great blog entry describing how to build you own. The following code snippets come from:

    http://mrsteel.wordpress.com/2007/06/15/randomize-array-shuffle-an-array-in-flash/

    Prototype function and return function code is :

    1. Array.prototype.mixElements = function() {
    2.  var _length:Number = this.length, rn:Number, it:Number, el:Object;
    3.  for (it = 0; it<_length; it++) {
    4.  el = this[it];
    5.  this[it] = this[rn = random(_length)];
    6.  this[rn] = el;
    7.  }
    8. }
    1. function mixArray(array:Array):Array {
    2.  var _length:Number = array.length, mixed:Array = array.slice(), rn:Number, it:Number, el:Object;
    3.  for (it = 0; it<_length; it++) {
    4.  el = mixed[it];
    5.  mixed[it] = mixed[rn = random(_length)];
    6.  mixed[rn] = el;
    7.  }
    8. return mixed;
    9. }
    10.  

    Share and Enjoy:
    • Print this article!
    • Digg
    • StumbleUpon
    • del.icio.us
    • LinkedIn
    • Facebook
    • Twitter
    • MySpace
    • Reddit
    • Google Bookmarks
    • Live
    • Yahoo! Bookmarks
    • RSS
    • E-mail this story to a friend!

    No Comments »

    No comments yet.

    RSS feed for comments on this post. TrackBack URL

    Leave a comment