我们一般不会注意,在调用addChild()的时候将触发ADDED_TO_STAGE事件;
例如:
addChild(mc);
当mc完全加载到舞台上时就会触发:ADDED_TO_STAGE 事件;
例如:
package {
import flash.display.Sprite;
import flash.events.Event;
public class ats_example extends Sprite {
public function ats_example() {
var child:a_child = new a_child();
addChild(child);
}
}
}
a_child.as 是这样的:
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
然而我们要把a_child.as改一下:
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]
完全正确了。