casey
March 18, 2020, 2:16pm
1
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!
samme
March 18, 2020, 4:41pm
2
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);
casey
March 18, 2020, 4:46pm
3
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!?
samme
March 18, 2020, 4:48pm
4
I think you need
let player = this.physics.add.sprite(500, 400, 'char', 3);
But check console.
casey
March 18, 2020, 4:49pm
5
Nope–nothign in console, the player just disappears.
samme
March 18, 2020, 4:54pm
6
Oh you need moveTo() . Arguments don’t match.
1 Like
casey
March 18, 2020, 10:49pm
8
THe problem with this is, it’s just floating my object randomly around the screen, not towards the pointer.x position.