开发者

Lost with dispatching an Event on CustomEvent Class

This is the objective I have been trying to accomplish but having no luck. I have a simple swf named "btn.swf". It has a button and when it is clicked, it dispatches an Event to it's parent 'Main App';

I then wrote a CustomEvent Class which 'btn.swf' extends to. Btn.swf creates an instance of CustomEvent and dispatches event.

In order for me to load swf content into 'Main App', I wrote a CustomLoader which extends Sprite class. CustomLoader loads the content into 'Main App'.

I am unable to catch the dispatched event in 'Main App'. I have spent day on it and now pretty much exhausted all that I could think of. I would highly appreciate any help with this please.

Let me recap my class Structure:

BtnClass extends CustomEvent class. Btn Class dispatches an event BtnClass is then published as btn.swf.

CustomLoader class is employed by the CustomMain class to load the btn.swf and catch the dispatched event by btn.swf.

Dispatched Event is being lost in translation somewhere. I may be approaching the whole thing wrong, perhaps.

Here are the classes:

CustomEvent class

package com.apps.comps.btn
{
    import flash.events.*;

    public class CustomEvent extends Event
    {
        public var data:*;

        public static const BUBBLE_UP:String = 'BubbleUp';

        public function CustomEvent(type:String, customData:Object, bubbles:Boolean=false, cancelable:Boolean=false)
        {
            super(type, bubbles, cancelable);
            this.data = customData;
        }

        override public function clone():Event
        {
            return new CustomEvent(type, data, bubbles, cancelable);
        }

        override public function toString():String
        {
            return formatToString('CustomEvent', 'type', 'data', 'bubbles', 'cancelable');
        }
    }
}

BtnMain class

package com.apps.comps.btn
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.errors.*;
    import fl.controls.*;

    public class BtnMain extends Sprite
    {
        public var evt:CustomEvent;
        public var obj:Object = {name:'JOHN', game:'RUNNING'};
        public var data:*;

        public function BtnMain()
        {
            trace("BTN MAIN INIT");
            createBtn();
        }

        p开发者_JS百科ublic function createBtn():void
        {
            var btn:Button = new Button();
            btn.label = 'CLICK ME';
            btn.x = btn.y = 200;
            btn.addEventListener(MouseEvent.CLICK, btnHandler);
            addChild(btn);
        }

        public function btnHandler(e:MouseEvent):void
        {
            evt = new CustomEvent(CustomEvent.BUBBLE_UP, obj, false, false);
            this.dispatchEvent(evt);
            trace('...');
        }
    }
}

CustomLoader class

package com.apps.comps
{
    import flash.display.*;
    import flash.net.*;
    import flash.events.*;
    import com.apps.comps.*;

    public class CustomLoader extends Sprite
    {
        // URLLoader already has a data property, so I used extraData
        private var _path:String;
        public var _ldr:Loader;
        public var eData:*;

        public function CustomLoader($path:String)
        {
            this._path = $path;
            loadContent(this._path);
        }       

        private function loadContent(e:String):void
        {
            this._ldr = new Loader();
            this._ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            this._ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
            this._ldr.load(new URLRequest(e));
        }

        private function progressHandler(e:ProgressEvent):void
        {
            var percent:uint = Math.round((e.bytesLoaded / e.bytesTotal) * 100);
            trace(percent);
        }

        private function onCompleteHandler(e:Event)
        {
            e.target.dispatchEvent('BubbleUp');
            this._ldr.contentLoaderInfo.addEventListener(CustomEvent.BUBBLE_UP, vHandler);
        }       

        public function vHandler(e:CustomEvent):void
        {
            trace(e);
        }

        override public function dispatchEvent(event: Event) : Boolean
        {
            var customEvent: CustomEvent = new CustomEvent(event.type, extraData, event.bubbles, event.cancelable);
            return super.dispatchEvent(customEvent);
        }

    }
}

CustomMain class

package com.apps.comps
{
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.errors.*;
    import com.apps.comps.*;

    public class CustomMain extends Sprite
    {
        var custLdr:CustomEvent;
        var obj:Object = {name:'SUPERMAN', game:'FLYING'};

        public function CustomMain()
        {
            //custLdr = new CustomLoader('movies/btn.swf');
            //addChild(custLdr);
            this.dispatchEvent(CustomEvent(obj));           
            this.addEventListener(CustomEvent.BUBBLE_UP, vHandler);

        }

        function vHandler(e:Event):void
        {
            trace('TARZAN');
        }

    }
}

Thanks you.


It looks like you're not setting your event to bubble in BtnMain.as, so CustomLoader won't receive it. Try changing this line in btnHandler to set "bubbles" to true:

evt = new CustomEvent(CustomEvent.BUBBLE_UP, obj, true, false);


You are almost there, you just have to listen to the object that is dispatching. So in your main class, refer to your BtnMain, and addEventListener to it.

package com.apps.comps
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.errors.*;
import com.apps.comps.*;

public class CustomMain extends Sprite
{
    var custLdr:CustomEvent;
    var obj:Object = {name:'SUPERMAN', game:'FLYING'};

    public function CustomMain()
    {
        var btnMain:BtnMain = new BtnMain();
        addChild(btnMain);

        /* This is the line you need */
        btnMain.addEventListener(CustomEvent.BUBBLE_UP, vHandler);

    }

    function vHandler(e:Event):void
    {
        trace('TARZAN');
    }

}
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜