Skip to main content
New Participant
January 22, 2010
Question

Help with AS3 EventListeners in AS2

  • January 22, 2010
  • 1 reply
  • 1809 views

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.

This topic has been closed for replies.

1 reply

Ned Murphy
Brainiac
January 22, 2010

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]...

New Participant
January 22, 2010

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);
}

Inspiring
January 23, 2010

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.


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.