Reputation: 2058
I'm trying to create a particle that moves by mouse position. So, I have created a class that creates a particle. And I'm able to add an event listener by calling "handleMouseEvent()"
Please see this fiddle :
Expected result is both two particle that moves by mouse actions. But result is; only one particle is moving, the first ones event listener is overridden.
At the most bottom of the code you will see two create procedure. When I call two create procedure the second one overrides the first ones event listener.
Can you please advice me why this code doesn't work?
Upvotes: 0
Views: 142
Reputation: 11294
I modified your fiddle to use the scope
parameter of the on()
method, which is much cleaner. I think it works now (not exactly sure what you were going for), but it affects both particles.
particleClass.prototype.handleMouseEvent=function(){
stage.on("stagemousemove",function(){
console.log(this.currentX);
this.setXYByMousePosition(stage.mouseX,stage.mouseY);
}, this);
return this;
};
Here is the updated version: https://jsfiddle.net/lannymcnie/xLgwfj99/6/
Upvotes: 1