melonJS icon indicating copy to clipboard operation
melonJS copied to clipboard

me.game.remove does not remove ObjectEntity's mouse events from canvas

Open greghouston opened this issue 14 years ago • 4 comments

me.game.remove (even with force set to true) does not appear to remove the mouse events of the ObjectEntity from the canvas. So even though the ObjectEntity is gone, clicking where it was still triggers its mouse events.

greghouston avatar Sep 23 '12 10:09 greghouston

actually it was a deliberate choice, as it's quite not possible to know what has been passed to the registerMouseEvent function (Could be the object itself, the hitbox, but as well any other rect managed by the object itself).

the "clean way" to do that is to call the releaseMouseEvent function from the Object onDestroyEvent function (which will be called on object removal), see here : http://www.melonjs.org/docs/symbols/me.SpriteObject.html#onDestroyEvent

ghost avatar Sep 23 '12 11:09 ghost

It seems really unintuitive and doesn't function like other HTML elements. If you remove a DIV that has an event on it, that DIV and all its children and their events are removed as well.

greghouston avatar Sep 23 '12 11:09 greghouston

To Olivier's point, registerMouseEvent() takes a rectangle object parameter, and not necessarily the same object given to me.game.add, it would be too difficult to handle every edge case of automatically removing the mouse handler associated with an object when that object is removed.

Take an example, I can create an object where its constructor creates a rectangle around itself with a 100px border: this.largeRect = me.Rect(me.Vector2d(x - 100, y - 100), this.width + 200, this.height + 200); And then passes this.largeRect to registerMouseEvent(). When this object is removed, the game manager does not know that its largeRect property has a mouse handler associated. So it cannot automatically call releaseMouseEvent() on it.

If only game objects themselves could have mouse events registered, you would be right; it would be a lot like removing the event handler for a DIV element when the DIV is removed from the document. But that just isn't the case.

parasyte avatar Sep 23 '12 21:09 parasyte

Closing this. I don't see anything we can do about it right now.

parasyte avatar Sep 28 '12 19:09 parasyte