0
votes

Hai,

I want to override the dispatchEvent method while inheriting from flash.display.Sprite. Whenever an event gets dispatched now like ADDED_TO_STAGE and CLICK it will get through dispatchEvent in my theory. However there is no single trace he'll get through dispatchEvent.. so the events get dispatched internally from some where else?

    public class TestSprite extends Sprite
    {

        public function TestSprite() 
        {
            this.addEventListener(Event.ADDED_TO_STAGE, this.handleAddedToStage);
        }

        private function handleAddedToStage(event:Event):void
        {

        }

        override public function dispatchEvent(event:Event):Boolean
        {
            trace(event.type);

            return super.dispatchEvent(event);
        }

    }
            this._sprite = new TestSprite();
            this._sprite.graphics.beginFill(0x000000);
            this._sprite.graphics.drawRect(20, 20, 200, 200);
            this._sprite.graphics.endFill();

                        this.addChild( this._sprite );

There is no trace..

2

2 Answers

1
votes

The Sprite implements IEventDispatcher so that the coder - you - can dispatch custom events from display-list classes. As you suspected however, native Flash Player events are not dispatched through the dispatchEvent method itself, they are created and passed to event listeners internally. I imagine a primary reason for this is performance.

0
votes

sooner or later in the function you need to pass the event to the real sprite eventDispatcher or it won't work (hint: super.dispatchEvent(event) )

by the way this will work only on objects that subclasses your own class, the others will continue to subclass Sprite, hence use the Sprite method.