9 Replies Latest reply on Jan 27, 2010 6:51 AM by brandonL88

    Help with AS3 EventListeners in AS2

    brandonL88

      I'm trying to emulate the ActionScript 3 versions of addEventListener and removeEventListener in ActionScript 2. I'm having the hardest time getting the functions to work properly, so I figured I would ask here and maybe someone would know how these are written in ActionScript 3.

        • 1. Re: Help with AS3 EventListeners in AS2
          Ned Murphy Adobe Community Professional & MVP

          I'm not clear on what you're after.  Can you give an example... something along the lines of... I have [this] and want to know how to do it using [that]...

          • 2. Re: Help with AS3 EventListeners in AS2
            brandonL88 Level 1

            Not to come off as being rude, but exactly as it states above. I'm trying to make an ActionScript 2 version of the addEventListener and removeEventListener functions.

            An example: I have an ActionScript 2 file where I want to have event listeners on a certain object or the stage etc. and I would like to be able to remove them as well.

             

            someObject.addEventListener(onRelease, hello);

             

            function hello():Void{

                 trace("hello world");

                 someObject.removeEventListener(onRelease, hello);
            }

            • 3. Re: Help with AS3 EventListeners in AS2
              Ned Murphy Adobe Community Professional & MVP

              It might be clear in your head what you're thinking, but in writing it out there's room for misinterpretation, so examples help. In your original post the first sentence says you want to do something in AS2 like you would in AS3.  And your second sentence says you hope someone knows how to do something in AS3.

               

              In any case, if your goal is to add and remove the functionality of a button, you do not need to emulate how AS3 does it.  That would be wasteful trying to concoct something that resembles it.  You can simply use...

               

              btn.onRelease = function(){
                   trace("hello world");

                   delete btn.onRelease;
              }

              1 person found this helpful
              • 4. Re: Help with AS3 EventListeners in AS2
                Rothrock Level 5

                Yeah your first post is completely confusing. You cannot do anything from AS3 in AS2 they are different languages. If you want to do addEventListeners in AS2 you have to do them like AS2 does them.

                 

                It also makes the real meaning of your question harder to figure out because addEventListener works almost exactly the same in AS2 as it does in AS3, but with a few very small changes. Void goes to void. The event to listen for is a Static typed variable in AS3, but a "magic" string in AS2. And the handler function would probably need to receive an event object. In AS3 that is a special type of object like MouseEvent or Event, but in AS2 the handler usually recieves just a plain old object. So your code would look like this:

                 

                someObject.addEventListener("onRelease", hello);

                 

                function hello(event:Object):void{

                     trace("hello world");

                     someObject.removeEventListener("onRelease", hello);
                }

                 

                However someObject would need to be an instance of a class that can dispatch events and specifically an onRelease event.

                 

                In AS2, as Ned says you can handle certain events from a movieclip as he has shown. Of course in some AS2 classes you can use addEventListener as I have described and in yet others there is just addListener. This is the confusion of AS2 and why AS3 is so nicely unfied.

                 

                If you are trying to write your own Class in AS2 then it is pretty easy to add event dispatching functionality to it.

                • 5. Re: Help with AS3 EventListeners in AS2
                  brandonL88 Level 1

                  Sorry, for any confusion I may have caused and the example I used was not a good one. To try and clear some things up I will try to explain a little better what my dilema is. I have a file I made in ActionScript 3, but I need to convert it down to ActionScipt 2. Luckily, almost everything was using basic functions that would work in either ActionScript 2 or 3 except for a few changes. The one that was slighlty throwing me for a loop was the addEventListener/removeEventListener. From some of the help files I looked at I figured out you could make a listener like so:

                   

                  var someListener:Object = new Object();

                   

                  fucntion someFunction(){

                       trace("something");

                  }

                   

                  someListener.onRelease = someFunction;

                   

                  someObj.addListener(someListener);

                   

                   

                  This made me think I would need a new listener object for every type of event. I was thinking this because two of the same events would cancel out the other. Example:(Not the full code, but a snippet to go with the snippet above)

                   

                  someListener.onRelease = someFunction;

                  someListener.onRelease = newFunction;

                   

                  someObj_2.addListener(someListener);

                   

                  Obviously, the second onRelease would overwrite the first one and the first someObj would also point to the newFunction. To get around this you would have to have one listening object for each someObj if I wanted them to have the same event, but run different functions.

                   

                  Though, after looking at the code you provided, I think I was overthinking this entire thing due slightly to my lack of knowledge in ActionScript 2 and only finding examples as shown above.

                   

                  If I am understanding correctly, I can just use the code like this:

                   

                  someObj.onRelease = someFunction;

                  someObj_2.onRelease = newFunction;

                   

                  and to remove the onRelease listener for one of the objects I would just do this:

                   

                  delete someObj_2.onRelease;

                   

                  I am trying to clarify this so I don't get the wrong idea. I also want to make sure this is removing the event listener for the object as in ActionScript 3 you would want to remove your event listeners you created to avoid having all of these event listeners still running when they are no longer needed. Though I'm not sure if it works the same way in ActionScript 2, so cut me some slack if I'm wrong as I'm still trying to understand the older version.

                  • 6. Re: Help with AS3 EventListeners in AS2
                    Rothrock Level 5

                    Ah-ha. It seems your real question has nothing to do with event listeners in AS2. Your real question seems to be, "How do I convert my existing AS3 code to AS2, especially the part that handles mouse events?"

                     

                    As I said before event handling in AS2 is kind all over the map—with at least three different ways of doing it depending upon what class you are using. So if you keep calling the mouse events that MovieClips can have "event listeners" you will only confuse everybody, since there are no "listeners" (at least with that name involved).

                     

                    And yes your code for event handlers is correct.

                     

                    someObject1.onRelease=handleRelease1

                    similarObject1.onRelease=handleRelease1

                    someObject2.onRelease=handleRelease2

                     

                    And you can either reassign the handler functions:

                     

                    someObject1.onRelease=newFunction1

                     

                    or just delete them like you've shown. Although generally in AS2 you don't have to delete them. Also in your code you are referring to the instances as "some object." Since there aren't sprites, loaders, etc. I think the only classes that have the mouse events are Button and MovieClip. Unless you are making your own classes and then you could have the actually dispatch events and treat them just like AS3, except for the few caveats I listed above.

                    • 7. Re: Help with AS3 EventListeners in AS2
                      brandonL88 Level 1

                      Would you by any chance be able to explain these 3 methods a little more in depth for me? I'm having a hard time finding clear info, as you stated, the stuff is all over the place and I find it not well documented.

                      • 8. Re: Help with AS3 EventListeners in AS2
                        Rothrock Level 5

                        Not sure what there is to explain specifially.

                         

                        One way is the "on..." way, which you are seeing:

                         

                        someTextField.onChanged

                        someMovieClip.onPress

                        someMovieClip.onMouseMove

                         

                        The other you already know (with the few exceptions I've mentioned above it is the same as AS3), the event listener:

                         

                        someMediaPlayer.addEventListener("cuePoint",handleCuePoint);

                         

                        And the last is very similar to the event listener, it is just the listener:

                         

                        Mouse.addListener(someObject)

                        Key.addListener(someObject);

                        someMovieClipLoader.addListener(someObject);

                         

                        The main difference between addEventListener and addListener is that the first allows you to specify which events you want to listen for. addListener just means listen for all the events, but of course if you don't have a method/function with the proper name then it won't be heard. Also with addEventListener you can choose which function (or even multiple functions, which could be good or bad, depending...) will hear a certain event.

                         

                        You might notice that onMouseMove is in the first group and that there is also a Mouse in the last group. That is because by default all movieclips are registered as listeners of the Mouse class, but you can use the addListener to have some object that already isn't a listener of the mouse object become one.

                         

                        As for the documentation, the AS2 documentation is actually pretty good. The trick is you have to get to the AS2 Language Reference where all the classes are presented with all their properties, methods, and events. In AS3 when we talk about events we are really talking about the instances of the Event objects themselves. In AS2, we are more talking about the names of the handlers like onChanged, rather than the event object itself.

                        1 person found this helpful
                        • 9. Re: Help with AS3 EventListeners in AS2
                          brandonL88 Level 1

                          Sorry for the late reply, I've been kind of busy. It seems I'm somewhat getting the hang of it.