ERU
ERU

Reputation: 13

easeljs click event not firing when using from typescript

The following code shows a red disk on the canvas but the event handler is never called when clicking the disk:

var circle = new createjs.Shape();
circle.graphics.beginFill("red").drawCircle(0, 0, 50);
circle.x = circle.y = 100;
this.stage.addChild(circle);
circle.addEventListener("click", (evt: createjs.MouseEvent) => this.handleMouseEvent);
...

private handleMouseEvent(evt: createjs.MouseEvent) {
    alert("handled");
}

Upvotes: 1

Views: 336

Answers (1)

Fredrik Ljung
Fredrik Ljung

Reputation: 1475

If you need to access this inside the event change to:

circle.addEventListener("click", (evt: createjs.MouseEvent) => this.handleMouseEvent(evt));

if you don't need this in the event you can also change to:

circle.addEventListener("click", this.handleMouseEvent);

The reason is that you are registering a lambda expression to handle the event, and the only thing that happens in the lambda expression is that this.handleMouseEvent is returned. You either need to register the actual function this.handleMouseEvent as event handler (the second option above) or call this.handleMouseEvent in the lambda (the first option above).

Upvotes: 3

Related Questions