J.C. Berry wrote:

> Thanks, Kerry and allandt. I will take your recommendations, allandt. Kerry,
> when you said that I would end up with only the last DynaBtn, that is true.
> But I wanted to ask about the solution I found using that DynaBtn handler
> with the parameter. It returns the function and so it works. My question is,
> is the better solution to create a separate class? More interesting for me
> is, can it work without creating a separate class? Any elucidation would be
> appreciated.

In your case, you never refer to the DynaBtn except to add the
listeners, so your code works.

There are two issues, though. If there are no references to an object,
that object is available for garbage collection. I think the fact that
you have the listeners attached gives each instance an implicit
reference--not entirely sure how it works under the hood, but that's
the only way I can explain why your buttons remain active without a
reference.

The other is a trap that you haven't run into, but will. When you add
the event listener, the event listener now belongs to the object. When
you want to remove that listener, you won't be able to do so from your
class (with a call like DynaBtn.removeEventListener(...).

You're setting yourself up for a couple of problems. One is a memory
leak--you can never get rid of the DynaBtn objects, and their
listeners will keep listening forever. That ties up resources.

Also, your listeners are fairly benign--they're just listening for
mouse events. But what if you had a listener that listened for a timer
event? It would periodically execute--perhaps several times a
second--and you would have no way of stopping it except killing the
timer.

The answer is pretty clear. Export the movie clips as a DynaBtnClass,
and create a DynaBtnClass class which adds and removes handlers from
itself. When you instantiate the buttons, put the references into an
array--DynaBtn would become an array in your DynaMap class.

Another thing I noticed--you have some variables that you haven't
declared the type for. For example, tbaInstance and DynaBtn. I would
strongly recommend that you put your compiler into strict mode and
declare a type for all variables.

It's a little extra work up front, and you'll have some choice words
when the compiler complains about untyped variables. But, believe me,
you would much rather the compiler catch your errors. The other option
is to spend hours trying to track down a mystery bug, and finally
finding that you set your variable to "42" (a string) instead of 42 (a
Number).

Cordially,

Kerry Thompson
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to