• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

arrayShuffle.as

Array.prototype.distinctShuffle = function () {

    trace("in distinct shuffle");
    result = [];
    for (posArray = [], i = 0; i < this.length; posArray[i] = i++) {}
    for (last = this.length - 1; last >= 0; last--)
    {
          selected = this[last];
          rand = random(posArray.length - 1);
          lastPos = posArray.getPos(last);
          if (lastPos == null)
          {
                result[posArray[rand]] = selected;
                posArray.splice(rand, 1);
          }
          else
          {
                posArray.splice(lastPos, 1);
                result[posArray[rand]] = selected;
                posArray.splice(rand, 1);
                posArray.push(last);
          }
    }
    return result;
}


Array.prototype.getPos = function (item) {
    for (i = 0; i < this.length; ++i) {
          if (this[i] == item) {
                return i;
          }
    }
    return null;
}


					  

What we've done is add a method to the Array class, which is something you can't do in AS2. It's a little strange using older code to do something the new code can't do. MX2004 feels a little like a transition product, using AS2 but not leaving AS1 completely behind yet.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint