MoveToObject-- Scope problem?

Getting error “Uncaught TypeEroror: cannot read property ‘moveToObject’ of undefined”
also tried ‘moveto’.

I want to move a character object to the mouse click’s x position.

I think it’s a scope problem but not sure how to fix.

>     class Game extends Phaser.Scene {
>           constructor(){
>             super("Game");
>         }
>         preload(){ //assume loading of everything is fine}
> 
>     create(){
>      let player = this.add.sprite(500, 400, 'char', 3);
>       this.input.on('pointerdown', function(pointer){
>                 this.physics.moveToObject(player, pointer.x, 200);           
>             });
> 
>     }

Thanks in advance for help!

That means this.physics is undefined.

You can always pass context to on():

this.input.on('pointerdown', function(pointer) {
  this.physics.moveToObject(player, pointer.x, 200);
}, this);

Thanks @samme. If I do add this.physics.add.existing(player),
and then add this to the end of the function as shown, the player character disappears altogether when I click!?

I think you need

let player = this.physics.add.sprite(500, 400, 'char', 3);

But check console.

Nope–nothign in console, the player just disappears.

Oh you need moveTo(). Arguments don’t match.

1 Like

YESS!!! thank you

1 Like

THe problem with this is, it’s just floating my object randomly around the screen, not towards the pointer.x position.