October 16th, 2007
Array sorting
In AS2 we had Array.sortOn() and it worked fine if you had an array full of Objects with nice property names to sort on. But what happens when you have an array of MovieClips and you need to sort the array based on the Y axis? You’d have to pop/shift/unshift/slice/splice … wait … what?
In comes AS3. Array.sortOn("y", Array.NUMERIC); will easily change the indexes of the array if the y property changes. This comes in handy for a container MovieClip (or Sprite) that has a list of MovieClips (or Sprites) and that container is scrolling up and down. While it is scrolling, if the clip reaches the top of the stage, for example, you would want it to re-appear at the bottom.
The check is easy enough and moving the clip is also easy and now the mess of rearranging the array is also easy!
We are doing this so the array’s indexes mimic the order of the list. In doing so we can easily check the 1st element in the array (and the last for the bottom to top swap) to see if it is above the stage and … BAM! … set it’s y position to the bottom edge of the last element in the array.
Nice … and easy.