ActionScript 3 - Sprite Always On Top

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 scene.

What is the best way to make sure that the sprite is on top of everything and is not hidden behind other sprites?

Thank,

~ kcg

+3
source share
4 answers

Anytime you add a clip, it is added at the top by default.

Therefore, you can use

addChild(Sprite);

or

addChildAt(Sprite,this.numChildren );

But if for some reason you use the flash / flex IDE and try to keep the clip on top of everything all the time, and don't really run them on the go, I would suggest you just add the clip to the top layer. But since you marked it as an actioncript only, the subject of the layer really does not exist.

swapchildren .

swapChildren(DisplayObject, DisplayObject);
+6

.

stage.addChild();

, , :

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

:

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

, - MovieClip,

addChildAt( topSprite , this.numChildren );

, , , , 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 );

0

:

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 is as follows:

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;
    }
}
}
0
source

Source: https://habr.com/ru/post/1769015/


All Articles