package {
import flash.display.*;
import flash.events.*;
public class DepthSortSpace extends MovieClip {
public function DepthSortSpace() {
super();
this.addEventListener( Event.ADDED_TO_STAGE, this.addedToStage, false, 0, true );
}
private function addedToStage( e:Event ) {
this.stage.addEventListener( Event.ENTER_FRAME, this.enterFrame, false, 0, true );
}
private function sortDisplayList():void {
var len:uint = numChildren;
var i,j;
for( i=0; i < len-1; i++ )
for (j=i+1; j < len; j++)
if ( getChildAt(i).y > getChildAt(j).y ) this.swapChildrenAt( i, j );
}
private function enterFrame(e:Event) {
this.sortDisplayList();
}
}
}
To use this, just make a new symbol, and set its class to DepthSortSpace (or subclass DepthSortSpace as your movie clip if you're already adding custom code for it). Then, add any objects you want to depth sort as children of this movie clip. It will handle it automatically.
1 comment:
Dude, quicksort, radixsort, mergesort are vastly superior to bubblesort : bubblesort is n squared, and quicksort is average case nlogn. For very small n, bubblesort may be fast enough, but quicksort typically does much better: the lynchpin of quicksort is finding a good pivot: if your data is close to sorted, you may consider insertion sort.
Hope Halloween is fun... Sorry I can't be there to operate the magic mirror! I am actually getting my toe x-rayed in the doctors office, so I thought I'd check your blog.
Post a Comment