我們都知道Event.ADDED_TO_STAGE在使用 addChild()函數,将顯示對象添加到舞台時觸發:
addChild(my_mc); 觸發該事件
var my_obj:a_class= new a_class();
addChild(my_obj)
上面的代碼中 是先觸發a_class裡的函數 然後因為addChild() 而 觸發Event.ADDED_TO_STAGE事件
我們看兩組不同的例子 來驗證Event.ADDED_TO_STAGE
主文檔類:
|
package {
import flash.display.Sprite;
import flash.events.Event;
public class a_child extends Sprite {
public function a_child() {
trace("this is the stage: "+stage);
trace("this is my parent: "+this.parent);
}
}
}
會發現輸出結果為: this is the stage: null
this is my parent: null
當我使用Event.ADDED_TO_STAGE事件來修改一下a_child類:
package {
import flash.display.Sprite;
import flash.events.Event;
public class a_child extends Sprite {
public function a_child() {
addEventListener(Event.ADDED_TO_STAGE, init);
}
function init(e:Event):void {
trace("this is the stage: "+stage);
trace("this is my parent: "+this.parent);
}
}
}
會發現輸出的結果是:
this is the stage: [object Stage]
this is my parent: [object ats_example] //上述表明雖然執行了a_Child裡的構造函數 但是由于不存在addChild()函數的觸發 是以init函數并沒有觸發 而是在文檔類中将其添加進舞台,而傳回去a_child構造函數類的 init()函數 ************************************************** 上面的方法給我提供另一種途徑去延緩某個類 相關方法的執行