A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Re-R&D mx.events.EventDispatcher;

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    51

    Re-R&D mx.events.EventDispatcher;

    For some while I am researching on dispatching Events on AS2 which can bubble as it happens in AS3. This is not the first time in this world and I hope you all know how to do it in AS2. My issue is not on addEventListener but the removeEventListener which is not removing that particular event from that object. Cause the handler argument in the mx.events.EventDispatcher is throwing undefined where that should be [type Function]. At the time of registering on addEventListener it is registering the handler as [type Function], but it throw undefined on removeEventListener method.

    What I am doing is to make my code just as we write in AS3 style.

    My code is structured like this for Example:

    ActionScript Code:

    Actionscript Code:
    import com.events.MouseEvent;
    class com.AS2Main {
        private var timeline:MovieClip;
        public function AS2Main(target:MovieClip) {
            timeline = target;
            var btn = timeline.attachMovie("btn","btn", timeline.getNextHighestDepth());
            btn.label.text = "Submit";
            btn._x=1;
            btn._y=1;
            //btn.name="myButton";
            btn.addEventListener(MouseEvent.CLICK,submitHandler);
        }
        private function submitHandler(event:MouseEvent):Void {  
            trace(event.type);
            trace(event.target);
            trace(event.target.name);      
            event.target.removeEventListener(MouseEvent.CLICK,submitHandler);
        }
        public static function init(target:MovieClip) {
            new AS2Main(target);
        }
    }

    ActionScript Code:

    Actionscript Code:
    import mx.events.EventDispatcher;

    import com.events.Event;
    import com.events.MouseEvent;

    class com.SubmitButton extends MovieClip {
        public var addEventListener:Function;
        public var removeEventListener:Function;
        private var dispatchEvent:Function;
        public var name:String="instance";
        function SubmitButton() {
            trace("SibmitButton");
            stop();
            name+=String(this.getNextHighestDepth());
            EventDispatcher.initialize(this);      
        }
        private function onRelease():Void {
            this.gotoAndStop("Over");
            dispatchEvent(new Event(MouseEvent.CLICK));
        }
    }

    Usage:

    ActionScript Code:


    Actionscript Code:
    import com.AS2Main;
    AS2Main.init(this);

    I’m not including the other two classes which I used (Event Class and MouseEvent Class) cause not required for this issue. This code structure helps me to develop faster in AS2 structure. As I’m said for the above issue I am expecting the method removeEventListener will remove the addEventListener from that object and stop bubbling the dispatchEvent(new Event(MouseEvent.CLICK));, which is not going to true for me.

    So, what would be the work around to solve this?

    Thank You for giving Attention..


    Regards



    FFA

  2. #2
    Senior Member
    Join Date
    May 2010
    Posts
    178
    Its GOOD to see someone develop AS2 projects in this way.

    I recommend you to put this in AS3 or R&D section (if somewhere over here) as because AS2 coder will not being able to give you solutions unless they have a well enough knowledge in AS3 syntax too.

    Posting your Project will clear to everyone over here



    poltuda

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    51
    I will Post the project soon.

    Could it be possible to
    Actionscript Code:
    Object.registerClass("createEmptyMovieClip",withTheClassWhichCreatesThisMovieClip);
    ?


    Thank You for your Reply
    Last edited by FlexFlashApps; 02-13-2012 at 06:17 PM.
    FFA

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    51
    Well Coming back to this thread again with some clue which you might help me out.

    The thing is, the function is not recognizing as a function, inside of it, in a particular case:

    Let’s have a look into a working project with Timeline Code, where the Button Symbol in the Library is linked with a Class.

    Timeline Code:

    Actionscript Code:
    var btn;
    function ButtonAssetDoc(target) {
        trace("ButtonAssetDoc");
        btn = target.attachMovie("button", "myButton", target.getNextHighestDepth());
        btn.addEventListener("click",btnClicked);
    }
    function btnClicked(evt:Object) {
        trace(evt.target);
        trace(typeof(btnClicked));// trace as function.
        evt.target.removeEventListener("click",btnClicked);
    }
    ButtonAssetDoc(this);

    Class Linked with the Button:

    Actionscript Code:
    import mx.events.EventDispatcher;

    class com.ButtonAsset {
        public var addEventListener:Function;
        public var removeEventListener:Function;
        private var dispatchEvent:Function;

        public function ButtonAsset() {
            EventDispatcher.initialize(this);
        }
        public function onRelease() {
            dispatchEvent({target:this, type:"click"});
        }
    }

    This works Perfect as expected. You can try it yourself to understand the real problem. Now move to the situation where the actual problem is arising. Let’s create a Class which will represent as a Document Class in AS2 Structure. And instantiate that Class from a one line code in Timeline.

    Actionscript Code:
    class ButtonAssetDoc {
        private var btn;

        public function ButtonAssetDoc(target) {
            trace("ButtonAssetDoc");
            btn = target.attachMovie("button", "myButton", target.getNextHighestDepth());
            btn.addEventListener("click",btnClicked);
        }
        public function btnClicked(evt:Object) {
            trace(evt.target);
            trace(typeof(btnClicked));// trace as undefined
            evt.target.removeEventListener("click",btnClicked);
        }
    }

    Timeline Code:

    Actionscript Code:
    new ButtonAssetDoc(this);

    You can see that everything is same as the previous Timeline base project but the only difference is I shifted the code in a separate Class. Here trace is undefined.

    So, my asking of help to you all is the way to recognize this trace as a function.

    This will solve all of my problems in my project.

    Thank you
    FFA

  5. #5
    Member
    Join Date
    Jul 2011
    Posts
    51
    http://actionscript.org/forums/showp...61&postcount=7

    There might be more ways to do this but here is one of them:

    Actionscript Code:
    ActionScript Code:
    import mx.utils.Delegate;
    class ButtonAssetDoc {
        public var delegate;
        private var btn;
        public var target:MovieClip;
        public function ButtonAssetDoc(targ) {
            target = targ;
            trace("ButtonAssetDoc");
            btn = target.attachMovie("button", "myButton", target.getNextHighestDepth());
            delegate=Delegate.create(this, btnClicked)
            btn.addEventListener("click",delegate);
        }
        public function btnClicked(evt:Object) {
            trace(evt.target);
            trace(typeof (btnClicked));
            evt.target.removeEventListener("click",delegate);
        }
    }

    The Delegate class creates a function wrapper to let you run a function in the context
    of the original object, rather than in the context of the second object, when you pass a
    function from one object to another.

    marlopax
    This is really helped me and I want to share it in this forum too.

    If any one have some other way, please share with me here.
    Last edited by FlexFlashApps; 03-27-2012 at 05:58 AM.
    FFA

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center