ADDED、ADDED_TO_STAGE、REMOVED、REMOVED_FROM_STAGE这几个事件的区别

package {
    import flash.display.Sprite;
    import flash.events.*;

    public class EventExample2 extends Sprite {
        public function EventExample2():void {
            var parentSprite:Sprite = createSprite("parentSprite",200);
            var childSprite:Sprite = createSprite("childSprite",100);
            var childOfChildSprite:Sprite = createSprite("childOfChildSprite",50);
           
            trace(":: Adding to Stage ::");

            trace(":: Adding to Stage ::");

            trace(":: Adding to Stage ::");
            childSprite.addChild(childOfChildSprite);
               parentSprite.addChild(childSprite);
               this.addChild(parentSprite);
               this.removeChild(parentSprite);
            trace(parentSprite.width);
               //如果将上面这句注释的话,则输出是:
          /*     :: Creating Sprite ::
               :: Creating Sprite ::
               :: Creating Sprite ::
               :: Adding to Stage ::
               :: Adding to Stage ::
               :: Adding to Stage ::
               ADDED: childOfChildSprite within childOfChildSprite
               ADDED: childOfChildSprite within childSprite
               ADDED: childSprite within childSprite
               ADDED: childSprite within parentSprite
               ADDED: parentSprite within parentSprite*/
          //很明显,父容器没有添加到root的显示列表,不会在stage上显示,自然就不会触发ADDED_TO_STAGE事件了.
              
        }
        private function createSprite(name:String,size:uint):Sprite {
            trace(":: Creating Sprite ::");
            var newSprite:Sprite = new Sprite();
            newSprite.name = name;
            newSprite.graphics.beginFill(0xFFFFFF * Math.random(),1);
            newSprite.graphics.drawRect(0,0,size,size);
            newSprite.graphics.endFill();
            newSprite.addEventListener(Event.ADDED, spriteAdded);
            newSprite.addEventListener(Event.ADDED_TO_STAGE, spriteAddedToStage);
            newSprite.addEventListener(Event.REMOVED, spriteRemoved);
            newSprite.addEventListener(Event.REMOVED_FROM_STAGE, spriteRemovedFromStage);
            newSprite.addEventListener(MouseEvent.CLICK, remove);
            return newSprite;
        }
        private function remove(event:Event) {
            if(event.target == event.currentTarget) {
                trace(":: Removing Clicked Sprite ::");
                var target:Sprite = Sprite(event.target);
                target.parent.removeChild(target);
            }
        }
        private function spriteRemovedFromStage(event:Event):void {
            trace("REMOVED_FROM_STAGE: " + event.target.name + " : " + event.currentTarget.name);
        }
        private function spriteRemoved(event:Event):void {
            trace("REMOVED: " + event.target.name + " from " + event.currentTarget.name);
        }
        private function spriteAddedToStage(event:Event):void {
            trace("ADDED_TO_STAGE: " + event.target.name + " : " + event.currentTarget.name);
        }
        private function spriteAdded(event:Event):void {
            trace("ADDED: " + event.target.name + " within " + event.currentTarget.name);
        }
    }
}

ADDED是子对象被添加到自身时触发,例如A.addChild(B) 则B dispatch Event.ADDED事件

ADDED_TO_STAGE是对象本身被添加到显示列表时触发; 例如A.addChild(B) 只有当A可视的时候,B才会dispatch Event.ADDED事件

类似的:REMOVED当子对象从自身移除时触发,REMOVED_FROM_STAGE是自身从显示列表中移除时触发;

posted @ 2012-12-06 17:42  ForFreeDom  阅读(1661)  评论(0编辑  收藏  举报