views:

79

answers:

4

Hello,

I have a sprite and when it is added to the screen, I want it to be added on top of everything else on the stage.

What is the best way to ensure that a sprite when added, is on top of everything and not hidden behind other sprites?

Thanks,

~ kcg

A: 

Provided that the parent is the top MovieClip, this should work

addChildAt( topSprite , this.numChildren );

Of course, if other Sprites are added afterwards, this will not work anymore , you could create two containers which will act as parent layers to your Sprites and reserve the top layer for this specific Sprite

var bottomLayer:Sprite = new Sprite();
var topLayer:Sprite = new Sprite();

addChildAt( bottomLayer , 0 );
addChild( topLayer );

//somewhere else in your code
bottomLayer.addChild( anySprite );
topLayer.addChild( topSprite );

PatrickS
+1  A: 

Anytime you add a clip it is added on top by default.

So can use

addChild(Sprite);

or

addChildAt(Sprite,this.numChildren );

But if by any reason you are you are using flash/flex IDE & trying to keep the clip on the top of everything always & not really spawning them on the run, then I would suggest you to simply add the clip to the top most layer. But since you have tagged it as only actionscript the layer thing does not really exist here.

Also you might consider using swapchildren for already existing clips & swapping thier depths in the parent display object.

swapChildren(DisplayObject, DisplayObject);
loxxy
A: 

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;
    }
}
}
Cotton
A: 

Add it to the stage.

stage.addChild(sprite);

You're probably adding it to a child sprite that is below another:

Stage
    - Child1
        *** Adding here ***
    - Child2
        - Something obscuring your sprite

Adding it to the stage will ensure:

Stage
    - Child1
    - Child2
        - Something obscuring your sprite
    *** Adding here ***
Daniel Hai