你可能未定義的錯誤,因爲根本不是什麼它曾經是。根現在是結構的程序化基礎,舞臺已經成爲顯示對象的持有者,因此應該被引用用於添加子項等。
舞臺是一個動態屬性,僅針對處於顯示列表。所以如果你以編程的方式創建這個類,如果你只是改變了stage的根目錄,這可能會返回一個null錯誤,因爲它還沒有被賦予一個階段引用,因爲它沒有被添加到列表中。嘗試使用Event.ADDED_TO_STAGE
偵聽器(如果它可能未被連接以確保該階段不會爲空)。
更重要的是,你正在計劃以這種方式將事物附加到舞臺上。一般來說這不是一個好主意,因爲舞臺本身不會直接引用孩子(雖然可以使用getChildByName或getChildAt),並且可能會導致後面丟失引用的問題。嘗試想着要麼將孩子目前的用戶接口或添加將分配某種價值的剪輯功能被傳遞,例如:在你的主時間軸
public static const PLAY_THIS:String = "the event to play the mc";
public function RPUserInterface(){
var np:nowplaying = new nowplaying();
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function getClip():nowplaying{
return np;
}
private function init(ev:Event):void{
//If you want it to play immediately, else use mouseEvent etc
dispatchEvent(new Event(PLAY_THIS));
}
:
var userInt:RPUserInterface = new RPUserInterface();
userInt.addEventListener(RPUserInterface.PLAY_THIS, addFromThis);
addChild(userInt);
private function addFromThis(ev:Event){
//add a global var ref here if needed.
var clip = (ev.target as RPUserInterface).getClip();
addChild(clip);
}
它可能看起來更加複雜和浪費空間,但它的良好實踐和更容易使這些類互動。其後更容易更改。