Groups | Blog | Home
all groups > flash actionscript > december 2005 >

flash actionscript : multiple mouse listeners


pero_peric
12/10/2005 7:11:58 PM
does anyone know how to get a list of active mouse event listeners?

i'm working on one (huge) flash project that includes windows-like flash user
interface, and i've come to following problem:

consider main.swf that dynamicly loads other swf file (i'll call it child.swf)
in empty movieclip (i'll call it myMc). that child.swf has its mouselistener
object created in standard way:

var mouseListener : Object = new Object();
mouseListener.onMouseWheel = function(delta) {
... something ...
}
Mouse.addListener(mouseListener);


when child.swf is unloaded from main.swf by calling unloadMovie(), all objects
from child.swf are destroyed, but mouseListener object still exists!

when loading child.swf second time (at runtime), mouseListener object is
created again, and now there are 2 same mouse listener objects, and all my code
inside onMouseWheel function now runs twice! of course, when unloading and
loading movieclip again, i have 3, 4 and more same onMouseWheel functions
running!

i spent whole day trying to figure out where the error is, and in the end i
figured that mouseListener object is not destroyed when unloading movieclip
that created it - it seems like a bug to me! it is same in flash player 7 and 8.

the situation is even worse because i've found no way of listing all mouse
listeners that are active at some moment.

of course, i could first remove that listener by calling something like
myMc.removeListener(mouseListener), before unloading the child movie, but i
would like to have a 'generic' function that would remove all listeners created
in some movieclip - otherwise my main.swf must know exact locations of all
listeners created in each child movieclip (thus, my 'close' function that
unloads movieclip cannot be generic!)

the problem is 'generic' since i'm working on interface in which main.swf
creates 'window-like' objects that can be dragged arround, and each of created
window objects has different swf loaded in some empty movieclip in it (some of
child movieclips have even more then one mouse listener - i'm using it for
controling scrollable texfield objects, and some child movieclips have more
than one scrollable textfield movieclip). when users press X (close button) on
each window object, window is unloaded, but listeners created in that swf still
exist!

has anyone come to similar problem (and solution?)
pero_peric
12/10/2005 7:39:03 PM
there's more:

when you press 'list variables' on flash player inside flash ide, only one
mouseListener is displayed, even when there are two listeners running (i
suppose that's because both of running listeners have same name and location).

and what's even worse, when 2 same listeners are once created, you can destroy
one of them (last created one) with removeListener, but previously created
listeners cannot be removed with removeListener function!
pero_peric
12/10/2005 7:57:20 PM
i've found a partial solution to my problem:

i placed:

[CODE]this.onUnload=function()
{
Mouse.removeListener(myListener1);
Mouse.removeListener(myListener2);
...
}[/CODE]

inside root timeline in each child movieclip.

now every child movieclip takes care of removing it's own mouse listeners (on
its unload).

this is not the perfect solution, since i have to carefully list all the used
listeners in each movieclip, but i find better listing them in wach movieclip
than listing them all in main movie 'close' function.

i still wellcome better solutions :)

pero_peric
12/10/2005 9:12:11 PM
considering my 'solution':

it's better to write remove listener code on same place where each listener is
created:

var myListener:Object = new Object();
myListener.onMouseWheel=function(delta,targetMC)
{
...
}
Mouse.addListener(myListener);
this.onUnload=function()
{
Mouse.removeListener(myListener);
}

writing onUnload function right after creating listener should prevent me from
forgeting to write removelistener code.

hope that helps someone...
AddThis Social Bookmark Button