Registering duplicate mouse event HTML5 target

173 views
Skip to first unread message

glantucan con golo lojo

unread,
Jul 4, 2013, 2:51:06 PM7/4/13
to haxe...@googlegroups.com
I'm just wondering if this is a bug or it's the way it works

If I register an event handlers with a particular display object:
display.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
//...
private function onMouseOver(e:MouseEvent):Void {
   display
.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
   display
.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);

   
//...
}
private function onMouseOut(e:MouseEvent):Void {
   display
.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
 
 display.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);

   
//...
}


If for some reason another actor has registered again the MOUSE_OVER handler on the display object, when tne on;ouseOut function executes the onMouseOver handler shouldn't be added again to the listeners list to be called when the event happens.

At least that's how it works in actionscript and when you select the flash target. But when compiling to html5 the  handler is added to that list as many times as the 
display.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
sentence is executed.

Is this is a bug? Or is it just the way html5/javascript works after compilation from haxe nme code?

Thanks !!

glantucan con golo lojo

unread,
Jul 4, 2013, 3:04:29 PM7/4/13
to haxe...@googlegroups.com

Actually I think this is weird, cause if I do something like:
_display.removeEventListener(MouseEvent.MOUSE_OUT, mouseIsOut);
trace
("MOUSE_OUT " + _display.hasEventListener(MouseEvent.MOUSE_OUT));


It traces out false for the flash target and true for the html5 target

Juraj Kirchheim

unread,
Jul 4, 2013, 3:38:30 PM7/4/13
to haxe...@googlegroups.com
If you're using haxe 2, then this is caused by how the platforms handle method closures. For haxe/js, each access to `someObject.method` creates a new closure, whereas for flash it is the very same function. A workaround is to store the closure in a separate variable and register/unregister that.

AFAIK, in Haxe 3, closures are created only once on all platforms.

Regards,
Juraj

glantucan con golo lojo

unread,
Jul 5, 2013, 2:28:38 AM7/5/13
to haxe...@googlegroups.com
Thanks, I think I understand what you mean.
:)

glantucan con golo lojo

unread,
Jul 5, 2013, 2:53:28 AM7/5/13
to haxe...@googlegroups.com
Mmmm
@back2dos: I'm trying this code just for testing what you said:


private var _onMouseDown:MouseEvent->Void;
 
 
function init()
 
{
 
 //var myInteractive:IMouseInteractive = new MouseInteractive();
 display = new Sprite();
 
 display.graphics.beginFill(0x778855);
 display.graphics.drawCircle(0, 0, 30);
 display.graphics.endFill();
 display.x = 200;
 display.y = 300;
 addChild(display);
 display.name = "circle";
 display.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
 _onMouseDown = onMouseDown;
 display.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
 display.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
 display.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
 display.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
 }
 
 
 
private function onMouseDown(e:MouseEvent):Void
 
{
 display.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
 display.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
 display.scaleX = display.scaleY = 1.2;
 trace("  MOuseDown:              " + e.target.name);
 }
 
 
private function onMouseUp(e:MouseEvent):Void
 
{
 display
.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
 display
.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
 display
.scaleX = display.scaleY = 1.1;
 trace
("  MOuseUp:      " + e.target.name);

 
}
 
 
 
private function onMouseOver(e:MouseEvent):Void
 
{
 display.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
 display.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
 display.scaleX = display.scaleY = 1.1;
 trace("MouseOver:     " + e.target.name);
 }
 
private function onMouseOut(e:MouseEvent):Void
 
{
 display.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
 display.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
 display.scaleX = display.scaleY = 1;
 trace("MouseOut: " + e.target.name);
 }

I still get this on the chrome console:
MouseOver:     circle 
  MOuseDown:              circle 
  MOuseDown:              circle 
  MOuseDown:              circle 
  MOuseDown:              circle 
  MOuseUp:      circle 
  MOuseUp:      circle 
  MOuseUp:      circle 
  MOuseUp:      circle 
MouseOut: circle 

Was that what you meant with the workaround or I missunderstood?

glantucan con golo lojo

unread,
Jul 5, 2013, 12:58:43 PM7/5/13
to haxe...@googlegroups.com
It seems it is a bug, so I posted it on github (https://github.com/haxenme/NME/issues/88) and atlassian(https://haxenme.atlassian.net/browse/NME-302

In the meantime I am using flags to store the state of every listener in my class and avoid duplicate registrations.

Joshua Granick

unread,
Jul 5, 2013, 3:39:07 PM7/5/13
to haxe...@googlegroups.com
Could you please test with OpenFL, and see if this issue still exists?

glantucan con golo lojo

unread,
Jul 6, 2013, 4:04:26 AM7/6/13
to haxe...@googlegroups.com
I didn't test it myself but hasufel at github said that it does duplicate with nme 4.x (source) under OpenFL also for android and mac.

I would try it myself, but I'm a newbe on haxe and don't know if it's possible to install haxe 3 and OpenFL an then go back to haxe 2 and NME to finish my actual project. 
Also I'm relying on flashdevelop to configure my projects and the version of it with haxe 3 support is still in alpha. 
Reply all
Reply to author
Forward
0 new messages