views:

4945

answers:

3

hello,

I'm building an animation in flash and as3, I have a function in which I add a stage eventListener, stage.addEventListener(Event.ENTER_FRAME, setScrollPercent, false, 0, true);

Since this event is set inside a function of a function, "two functions deep," How can I reset all stage event listeners from outside of the functions without getting an error?

+1  A: 

What errors are you seeing? The nesting level shouldn't have anything to do with it, since the listeners are just registered by the parameters, so as long you call remove with the same three key parameters you used for add, you should be fine.

Is your real question how to get a reference to the listener object to the outside scope? If so, there are several possible solutions and the best way to do it depends on the structure of your code.

-- MarkusQ

MarkusQ
+2  A: 

Best practices with AS eventlisteners are:

  • Make it weak (as you've done, the last parameter of addEventListener)
  • Set the event listener to null after handling (strictly speaking not required if you have set it to be weak)

Flex does not give you destructors. It has its own Garbage Collector running behind the scenes managing memory. It cleans up a piece of memory once there are no references to it. Well, strong references. All objects by default have a strong reference. Event handlers, since they tend to be a performance bottleneck, have this special ability of declaring themselves weak -- a weak reference. This is of course possible only when you are attaching the event handlers using the addEventHandler() function. Weak references are not taken into account by the GC and hence, when all strong references are gone, they'll be automagically garbage collected, freeing you from the tension of having to do a =null manually. Which is what you'd otherwise do when you don't specify the parameter. By default, even handlers are created as strong references.

However, marking them weak has a side-effect. They can vanish into thin air without you ever knowing about it. Eventually, you will know, but in terms of a nasty bug. Is that what's causing your problems? Maybe, may be not. You'll have to experiment. Also, it'll help if you can provide us with some more detail like the exact error code, some source.

Hope this helps. Happy flexing :)

dirkgently
can you add an example? I'm new to AS3 and I really don't know what does weak mean.
Ole Media
A: 

The answer to your question, and I realize you had a problem with scope, because I just answered a question you had on scope, is that you are working with stage. Consider the stage global as it is the canvas in which all of your display objects are drawn.

The following will work anywhere!!!

stage.addEventListener(Event.ENTER_FRAME, setScrollPercent, false, 0, true);

Now, the error that your talking about, IM GUESSING, is that you merely set the above to removeEventListener when you were ready which will not work.

The removeEventListener function DOES NOT accept five parameters like its sibling addEventListener, it only allows three ignoring priority and weak-reference, as the following shows.

//The following removes YOUR stage Event.ENTER_FRAME listener from any scope.
stage.removeEventListener(Event.ENTER_FRAME, setScrollPercent, false);

hope this helps, remember that stage is the global root in a sense, and be careful of root, it actually works how it is supposed to now in the fact that calling root is now relative to the swf you call it from, not the stage, unless the stage is the root of the current scope.

Brian Hodge
hodgedev.com blog.hodgedev.com

Brian Hodge