Here is a simple function for your purpose:
private function set onTop (displayer:DisplayObject):void
{
displayer.parent.addEventListener(Event.ADDED, function (event:Event):void
{
if (event.target.parent == event.currentTarget)
DisplayObjectContainer(event.currentTarget).addChild(displayer);
}, false, 0, true);
}
And the test sample looks like this:
package
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.GlowFilter;
import flash.text.TextField;
public class Main extends Sprite
{
public function Main ()
{
this.addChild(this.createRandom());
this.addChild(this.createRandom());
this.onTop = this.addChild(this.createTop());
this.addChild(this.createRandom());
this.addChild(this.createRandom());
this.addChild(this.createRandom());
this.addChild(this.createRandom());
}
private function set onTop (displayer:DisplayObject):void
{
displayer.parent.addEventListener(Event.ADDED, function (event:Event):void
{
if (event.target.parent == event.currentTarget)
DisplayObjectContainer(event.currentTarget).addChild(displayer);
}, false, 0, true);
}
private function createTop ():DisplayObject
{
var text:TextField = new TextField();
text.text = "I'm always on top!";
text.filters = [new GlowFilter(0xFFFFFF)];
return text;
}
private var index:int = 0;
private function createRandom ():DisplayObject
{
var sp:Sprite = new Sprite();
sp.x = 10 * this.index;
sp.y = 2 * this.index;
sp.graphics.beginFill([0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF][this.index++ % 4], 0.8);
sp.graphics.drawRect(0, 0, 100, 100);
sp.graphics.endFill();
return sp;
}
}
}