Removal of child with drag and drop

25 views
Skip to first unread message

jack...@oregonstate.edu

unread,
Oct 28, 2016, 1:04:04 PM10/28/16
to melonJS - A lightweight HTML5 game engine
Hi All,

I having some trouble removing an entity correctly. I can remove what I want, but it's generating subsequent errors that make me believe that I'm doing something wrong.

I'm using this code from the examples (at least as far as pointer/mouse movements go):
http://melonjs.github.io/melonJS/examples/shapes/

The idea being that when I move this object to the place I intend, It spawns an entirely different object that is stationary, and the object I was originally moving should disappear or be destroyed rather, which I do with a call to a function that is in the shape's "parent" (if your looking at the example above it would be the equivalent game.ShapeObject ) class. Much like the footprint of a building in starcraft or similar.

The function is just this:
destroy: function(){

me.game.world.removeChild(this);
}

Which is called in the onRelease function of the same class.

However, doing so yields the error
"Uncaught TypeError: Cannot read property 'getBounds' of undefined"
And it retriggers repeatedly the same error if I mouse over the area where the shape was.

I cannot for the life of me get this error to stop. It's obvious that it's trying to get the bounds of something that is no longer there, but It shouldn't be trying to get those bounds in the first place because that entity should be all gone, or so I'd thought.

Any help is as always so very much appreciated!

aaron.g...@gmail.com

unread,
Oct 28, 2016, 1:53:48 PM10/28/16
to melonJS - A lightweight HTML5 game engine, jack...@oregonstate.edu
In your destroy function, add above the removeChild:

me.input.releasePointerEvent('pointerdown', this);

Though change pointerdown to which ever event you need to remove :).

I believe what's happening is the event is still registered for that place on the screen, but the entity is gone.

Robert Jackson

unread,
Oct 28, 2016, 4:46:58 PM10/28/16
to mel...@googlegroups.com
Thanks, that seemed almost like the right strategy, but it definitely got me futher! The pointerdown is the mouse click though, correct?  I was  getting the error just by scrolling over it not clicking, so I'm thinking that the pointer moment had to b specifically targeted. I found the releasePointerEvent, but couldn't use it successfully until I also registered the same in the onActivate function. So instead of:
 
this.handler = me.event.subscribe(me.event.POINTERMOVE, this.pointerMove.bind(this));

I now use
me.input.registerPointerEvent('pointermove', this, this.pointerMove.bind(this));
So far so good! I'm a little unsure of the implication though, is it now no longer bound to the global pointer move event, but uhh... locally ? 


--
You received this message because you are subscribed to a topic in the Google Groups "melonJS - A lightweight HTML5 game engine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/melonjs/Yi0g07urRTk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to melonjs+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

aaron.g...@gmail.com

unread,
Oct 28, 2016, 6:35:36 PM10/28/16
to melonJS - A lightweight HTML5 game engine, jack...@oregonstate.edu
Yeah so we have a mouse move event bound to the canvas already. Provided there are any registered listeners, we will call them so long as the provided bounds (second param to registerPointerEvent) collides with the mouse coordinates. This applies to down, up, scroll events as well. Can see the list of events here: http://melonjs.github.io/melonJS/docs/me.input.html#registerPointerEvent
> To unsubscribe from this group and all its topics, send an email to melonjs+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages