Search code examples
javascriptoopcreatejseaseljs

Easeljs (createjs) unable to bind two event listener


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 :

http://bit.ly/1UczZvF

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?


Solution

  • 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/