开发者

as3 event handling

I've got strange problem with Event handling in AS3.

I've got two classes:

public class Configurator extends Sprite {

    public function Configurator () {
        thi开发者_如何学Gos.addEventListener(FramedPhoto.FRAMED_PHOTO_LOADED, this._test);
        var fp = new FramedPhoto();
    }

    protected function _test(event:Event) {
        trace('_test()');
    }
}

and the second one:

public class FramedPhoto extends Sprite {

    public static const FRAMED_PHOTO_LOADED = 'framedPhotoLoaded';

    public function FramedPhoto () {
        trace('FramedPhoto()');
        this.dispatchEvent(new Event(FramedPhoto.FRAMED_PHOTO_LOADED, true, false));
    }
}

Problem is that _test() is not being fired. Any ideas what could be done wrong here?

Test package with source files is available from http://blackbox.home.pl/test.zip


Events are dispatched in a hierarchical fashion, so just listening for an event wherever won't do the trick. What you need to do is to make your configurator listen to the object that dispatches the event or one above it in the display list. If you want to catch all events you'll need to listen to the topmost object of them all, the stage.


grapefrukt hit the nail on the head, but to be more specific its called the event flow. Simply put there are 3 phases to when a dispatchEvent() is called from a display object in a display list, these are the capture phase, target phase and bubble phase. In the capture phase the flash player travels down the display list from the stage to the display object that called the dispatchEvent().Then during the target phase the target display object dispatches the event. Then in the bubbles phase, the event bubbles up to the stage.

In your case, if you want your Configurator display object to hear the event dispatched from your FramedPhoto display object you have to add FramedPhoto to its display list.

I've taken the liberty of adding a document class to your project since you should avoid using the timeline anyway.

package
{
    import com.Configurator;
    import flash.display.MovieClip;

    public class Main extends MovieClip
    {
        public function Main():void
        {
            init();

        }// end function

        private function init():void
        {
            var configurator:Configurator = new Configurator();
            addChild(configurator);

        }// end function

    }// end class

}// end package

I imported your Configurator class(renamed the package from abc to com), declared, instantiated and added it to the display list.

package com
{
    import com.FramedPhoto;
    import flash.display.Sprite;
    import flash.events.Event;

    public class Configurator extends Sprite 
    {
        public function Configurator():void 
        {
            var framedPhoto:FramedPhoto = new FramedPhoto();
            addChild(framedPhoto);

            framedPhoto.addEventListener(FramedPhoto.FRAMED_PHOTO_LOADED, framedPhotoLoadedHandler);

        }// end function

        public function framedPhotoLoadedHandler(e:Event):void
        {
            trace("framePhotoLoadedHandler()");

        }// end function

    }// end function

}// end function

Next in your Configurator class, I imported your FramedPhoto class, declared and instantiated it. Then I added an event listener to it, which listens for the event that is dispatched from the framePhoto display object and calls framedPhotoLoadedHandler().

package com 
{ 
    import flash.display.Sprite;
    import flash.events.Event;

    public class FramedPhoto extends Sprite 
    {
        public static const FRAMED_PHOTO_LOADED = 'framedPhotoLoaded';

        public function FramedPhoto():void
        {
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

        }// end function

        private function addedToStageHandler(e:Event):void
        {
            dispatchEvent(new Event(FRAMED_PHOTO_LOADED, true));

        }// end function

    }// end function

}// end function

Lastly in the FramePhoto class I added an event listener to it, that listens for when the FramePhoto display object gets added to the stage. When it is added to the stage it will call addedToStageHandler() which will dispatch the event and will be caught in the parent Configurator display object.

I hope this helped :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜