2010-10-11 47 views
3

我有一個精靈,當它被添加到屏幕上時,我希望它被添加到舞臺上其他任何東西的頂部。actionscript 3 - Sprite永遠在最上方

確保添加精靈時,最好的方法是什麼,而不是隱藏在其他精靈之後?

感謝,

〜KCG

回答

6

無論何時添加剪輯,默認情況下都會添加剪輯。

所以可以使用

addChild(Sprite); 

addChildAt(Sprite,this.numChildren); 

但如果以任何理由,你是使用Flash /柔性IDE &試圖讓一切的頂部剪輯總是&不在運行中真正產生它們,那麼我建議你只需將剪輯添加到最頂層。但是因爲你已經將它標記爲只有actionscript層的東西在這裏並不存在。

此外,您可能會考慮使用swapchildren處理已存在的剪輯&交換父顯示對象中的深度。

swapChildren(DisplayObject, DisplayObject); 
0

提供父是頂級的MovieClip,這應該工作

 
addChildAt(topSprite , this.numChildren); 

當然,如果其他精靈是後來加入的,這將不再工作,你可以創建兩個容器,它們將作爲Sprite的父層,併爲這個特定的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); 
} 

與試看起來是這樣的:

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; 
    } 
} 
} 
2

它添加到舞臺上。

stage.addChild(sprite);

你可能將它添加到一個孩子的精靈是低於另:

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

它添加到舞臺將確保:

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

討論的是兄弟姐妹。將它加入舞臺在這種情況下可能是一個非常糟糕的做法...... – jacmkno 2014-02-24 00:47:19