Yichuan Wang
Yichuan Wang

Reputation: 753

Strange behavior of Event listener / Event flow

In the following code, if I click on the 'button', all three function will be called. But in all other cases, only stage event are fired. Why the 'sprite' event didn't got fired?

public class EventFlowTest extends Sprite
{
    private var button:Sprite;

    public function EventFlowTest()
    {
        addEventListener(Event.ADDED_TO_STAGE, init);

    }
    private function init(e:Event = null):void
    {
        stage.addEventListener(MouseEvent.MOUSE_DOWN,stageMouseDown,false);


        graphics.beginFill(0x11);
        graphics.drawCircle(100,100,100);
        addEventListener(MouseEvent.MOUSE_DOWN,spriteMouseDown,false);          


        button=new Sprite();
        addChild(button);

        button.graphics.beginFill(0xF1);
        button.graphics.drawCircle(100,100,10);
        button.addEventListener(MouseEvent.MOUSE_DOWN,buttonMouseDown,false);
    }

    private function spriteMouseDown(e:MouseEvent):void
    {
        trace("sprite");
    }

    private function stageMouseDown(e:MouseEvent):void
    {
        trace("stage");
    }

    private function buttonMouseDown(e:MouseEvent):void
    {
        trace("button");
    }

}

Upvotes: 0

Views: 71

Answers (2)

OXMO456
OXMO456

Reputation: 3548

that's strange... this behavior occur when your test class is the document class. If you embed your test in a document class, every thing is running as expected. I have no explaination for that behavior.

package {
    import flash.display.Sprite;

    public class Main extends Sprite {
        public function Main() {
            var test : EventFlowTest = new EventFlowTest();
            addChild(test);
        }
    }
}

Upvotes: 0

Related Questions