Adding elements on mouse evens

Hi,

What i’m trying to do is, on ‘pointermove’ event I would like to draw a line. However when I try this with the following command I get an error.

 function create() {
var posY =133;
 this.input.on('pointermove', function (pointer, currentlyOver) {
                    this.add.line(200, PosY, 0, 0, 140, 0, 0xff6699);});
}

Error : Uncaught TypeError: Cannot read property 'line' of undefined

This works fine if I try to draw the line directly in the ‘create’ function.
I assume it’s failing because ‘this’ is probably referring to something else when it’s inside the mouse event. How can I make this work ?

Thanks

Use an Arrow Function

create() {
  const PosY = 133
  this.input.on('pointermove', (pointer, currentlyOver) => {
    this.add.line(200, PosY, 0, 0, 140, 0, 0xff6699)
  })
}
1 Like

Perfect. Thanks