# MoveToObject-- Scope problem?

**URL:** <https://phaser.discourse.group/t/movetoobject-scope-problem/5559>\
**Category:** Phaser 3\
**Created:** [March 18, 2020, 2:16pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559 "2020-03-18T14:16:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [March 18, 2020, 2:16pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/1 "2020-03-18T14:16:06Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [March 18, 2020, 4:41pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/2 "2020-03-18T16:41:28Z")

</div>

> [@casey](#):
>
> cannot read property ‘moveToObject’ of undefined

That means `this.physics` is undefined.

You can always pass context to `on()`:

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

```

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [March 18, 2020, 4:46pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/3 "2020-03-18T16:46:56Z")

</div>

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!?

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [March 18, 2020, 4:48pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/4 "2020-03-18T16:48:33Z")

</div>

I think you need

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

```

But check console.

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [March 18, 2020, 4:49pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/5 "2020-03-18T16:49:14Z")

</div>

Nope–nothign in console, the player just disappears.

---

<div class="post-metadata">

**Author:** ![samme](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/samme/32/5275_2.png) [@samme](https://phaser.discourse.group/u/samme)\
**Post date:** [March 18, 2020, 4:54pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/6 "2020-03-18T16:54:52Z")

</div>

Oh you need [moveTo()](https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Arcade.ArcadePhysics.html#moveTo). Arguments don’t match.

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [March 18, 2020, 4:55pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/7 "2020-03-18T16:55:45Z")

</div>

YESS!!! thank you

---

<div class="post-metadata">

**Author:** ![casey](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/casey/32/2503_2.png) [@casey](https://phaser.discourse.group/u/casey)\
**Post date:** [March 18, 2020, 10:49pm UTC](https://phaser.discourse.group/t/movetoobject-scope-problem/5559/8 "2020-03-18T22:49:53Z")

</div>

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