views:

228

answers:

2

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..

A: 

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.

kajyr
Edited my question to make it more clear :P
Fristi
+1  A: 

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.

alecmce
Hmm too bad you can't catch those events :), i guess i have to figure out another way :)
Fristi
If you're able to explain the context of what you're tryin to do - perhaps in another question - I'm sure someone will be able to help! :)
alecmce
I want more control on the events. My plan is to subclass the common classes like Sprite, MovieClip, EventDispatcher, etc. I want to override the methods of IEventDispatcher to put the events in a manager/eventdispatcher? which keep track of all the events. Whenever a movieclip or sprite gets removed , i can just call one method (dispose) to remove all the event listeners and clean up the memory.This can easily achieved by overriding the IEventDispatcher methods ofcourse, but i want to reduce the amount of code by not repeating myself. That's why i want to catch those events :P
Fristi
Ah, you're trying to implement a removeAllListeners() style method? Hmm... unfortunately you're going to find it impossible, sorry. An alternative method might be to consider migrating from native events to Signals though? (search for AS3Signals) - which includes a removeAll method. If you adopt this across your entire application, you gain much better control over your events, and can probably implement what you've described. Good luck!
alecmce
That signal library looks nice! Philospy fits in my standards :)
Fristi