# How to access gameObjects and variables from another function?

**URL:** <https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732>\
**Category:** Phaser 3\
**Created:** [April 2, 2020, 8:53am UTC](https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732 "2020-04-02T08:53:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GamerWael](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gamerwael/32/3064_2.png) [@GamerWael](https://phaser.discourse.group/u/GamerWael)\
**Post date:** [April 2, 2020, 8:53am UTC](https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732/1 "2020-04-02T08:53:51Z")

</div>

How do I access my player from another function?

In my create I have:

```
this.player = this.physics.add.sprite(width/2, height/2, 'player');
var isMoving = false;

this.input.on('pointerdown', movePlayer);

```

and in movePlayer:

```
function movePlayer(pointer)
{alert(this.isMoving);alert(this.player);
    if(!isMoving)
    {
        isMoving = true;
        if(pointer.downX > width/2)
            this.player.setVelocityX(100);alert();
        else if(pointer.downX < width/2)
            this.player.setVelocityX(-100);
    }
}

```

Both the alerts give me undefined and the player doesnt move. I know there’s something wrong with the way I’m accessing it but I don’t know what because in the update function

```
this.player.setVelocityX(100);

```

works fine.

---

<div class="post-metadata">

**Author:** ![GamerWael](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gamerwael/32/3064_2.png) [@GamerWael](https://phaser.discourse.group/u/GamerWael)\
**Post date:** [April 2, 2020, 9:35am UTC](https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732/2 "2020-04-02T09:35:47Z")

</div>

How do I know what is “this” in the create function?  
And also why do we write  
this.player = this.physics.add.sprite()  
and why not  
player = this.physics.add.sprite()

---

<div class="post-metadata">

**Author:** ![ptaku19](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ptaku19/32/2170_2.png) [@ptaku19](https://phaser.discourse.group/u/ptaku19)\
**Post date:** [April 2, 2020, 11:18am UTC](https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732/3 "2020-04-02T11:18:58Z")

</div>

You have to go back to the basics, variable scope, contexts

---

<div class="post-metadata">

**Author:** ![GamerWael](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/gamerwael/32/3064_2.png) [@GamerWael](https://phaser.discourse.group/u/GamerWael)\
**Post date:** [April 2, 2020, 12:03pm UTC](https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732/4 "2020-04-02T12:03:33Z")

</div>

Thanks I’ll have a look at that.

---

<div class="post-metadata">

**Author:** ![john](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@john](https://phaser.discourse.group/u/john)\
**Post date:** [April 2, 2020, 1:07pm UTC](https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732/5 "2020-04-02T13:07:33Z")

</div>

Good Morning, I am new to Phaser also. I also struggled with “this” . So I use this:

console.log(this);

and it helps me learn what my current scope is at any given point. Typically I find out that “this” is referring to my current scene.

---

<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:** [April 2, 2020, 1:50pm UTC](https://phaser.discourse.group/t/how-to-access-gameobjects-and-variables-from-another-function/5732/6 "2020-04-02T13:50:52Z")

</div>

[https://github.com/samme/phaser3-faq/wiki#how-do-i-use-this-in-a-scene](https://github.com/samme/phaser3-faq/wiki#how-do-i-use-this-in-a-scene)
