1 var _container:Sprite = new Sprite(); 2 3 _container.addEventListener(Event.ADDED,onAdded); 4 _container.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage); 5 _container.addEventListener(Event.REMOVED,onRemoved); 6 _container.addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage); 7 8 function onAdded(e:Event):void { 9 trace("onAdded:",e); 10 } 11 12 function onAddedToStage(e:Event):void { 13 trace("onAddedToStage:",e); 14 } 15 16 function onRemoved(e:Event):void { 17 trace("onRemoved:",e); 18 } 19 20 function onRemovedFromStage(e:Event):void { 21 trace("onRemovedFromStage:",e); 22 } 23 24 addChild(_container); 25 //onAdded: [Event type="added" bubbles=true cancelable=false eventPhase=2] 26 //onAddedToStage: [Event type="addedToStage" bubbles=false cancelable=false eventPhase=2] 27 28 var _sub:Sprite = new Sprite(); 29 30 _container.addChild(_sub); 31 //onAdded: [Event type="added" bubbles=true cancelable=false eventPhase=3] 32 33 /*_container.removeChild(_sub); 34 //onRemoved: [Event type="removed" bubbles=true cancelable=false eventPhase=3]*/ 35 36 removeChild(_container); 37 //onRemoved: [Event type="removed" bubbles=true cancelable=false eventPhase=2] 38 //onRemovedFromStage: [Event type="removedFromStage" bubbles=false cancelable=false eventPhase=2] 39 40 _container.removeChild(_sub); 41 //onRemoved: [Event type="removed" bubbles=true cancelable=false eventPhase=3]
区别:
ADDED是子对象被添加到自身时触发,ADDED_TO_STAGE是对象本身被添加到显示列表时触发;类似的:REMOVED当子对象从自身移除时触发,REMOVED_FROM_STAGE是自身从显示列表中移除时触发;
ADDED/REMOVED事件会冒泡,而ADDED_TO_STAGE/REMOVED_FROM_STAGE不冒泡,也就是说每次addChild或removeChild自身时,下面的ADDED/REMOVED事件也会冒泡上来先被触发,然后才触发ADDED_TO_STAGE/REMOVED_FROM_STAGE事件.