# Why does this only work in update and create?

**URL:** <https://phaser.discourse.group/t/why-does-this-only-work-in-update-and-create/15224>\
**Category:** Phaser 3\
**Created:** [March 15, 2025, 6:59am UTC](https://phaser.discourse.group/t/why-does-this-only-work-in-update-and-create/15224 "2025-03-15T06:59:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohio](https://avatars.discourse-cdn.com/v4/letter/o/54ee81/32.png) [@ohio](https://phaser.discourse.group/u/ohio)\
**Post date:** [March 15, 2025, 6:59am UTC](https://phaser.discourse.group/t/why-does-this-only-work-in-update-and-create/15224/1 "2025-03-15T06:59:37Z")

</div>

Phaser uses this a lot (this.add.image, this.physics.add.sprite, etc.). However, you can only use these things attached to this inside the create or update function. This is very annoying for me because I cannot use this.add.image or this.physics inside a function outside of the update or create function  
ex:

```javascript
//this will not work
function addImage()
{
    this.add.image(0, 0, ' ');
}
function create()
{
    addImage(); //output: 'this' is not defined in addImage()
}

```

instead I have to do this:

```javascript
//this will work
function addImage(place)
{
    place.add.image(0, 0, ' ');
}
function create()
{
    addImage(this); // adds an empty image in coordinates (0, 0)
}

```

this is very annoying and inefficient with bigger projects and it would be much easier if this.physics, this.add.image, and all of those were global variables instead of variables stuck in the create and update functions. Is there a workaround to this or something you can do so users do not need to have a choppy solution to using this outside of create or update?

---

<div class="post-metadata">

**Author:** ![ohio](https://avatars.discourse-cdn.com/v4/letter/o/54ee81/32.png) [@ohio](https://phaser.discourse.group/u/ohio)\
**Post date:** [March 15, 2025, 7:00am UTC](https://phaser.discourse.group/t/why-does-this-only-work-in-update-and-create/15224/2 "2025-03-15T07:00:47Z")

</div>

Is this because this is a local variable inside the create() and update() or am I wrong?

---

<div class="post-metadata">

**Author:** ![scottwestover](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/scottwestover/32/7442_2.png) [@scottwestover](https://phaser.discourse.group/u/scottwestover)\
**Post date:** [March 16, 2025, 1:41am UTC](https://phaser.discourse.group/t/why-does-this-only-work-in-update-and-create/15224/3 "2025-03-16T01:41:50Z")

</div>

For these examples, when we use `this` in the `create` and `update` functions, the context of the code is referring to the Phaser Scene context that is associated with those two functions. This context is what makes all of those methods available to use (the `this.add.` and `this.physics`).

By default, when you provide these two functions to your Phaser Game configuration, Phaser is automatically creating a Phaser Scene instance, and binding the scene context to that method. If you want to use `this` with the same context, you will need to bind the context of those functions to the same context, or pass the context as an argument (as you did in your example).

Another option would be to create a class that extends the Phaser Scene class, and add the related logic to that class.

For more information on `this` and how the context works, I would recommend checking out [this - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this).

For an example of how you can bind the same context to another function:

```javascript
function addImage() {
    this.earth = this.add.image(400, 350, "earth");
}

function preload() { 
    this.load.image("earth", "https://cdn.phaser.io/sandbox/square-earth.png");
}

function create() {
    this.addImage = addImage.bind(this);
    this.addImage();
}

```

---

<div class="post-metadata">

**Author:** ![ohio](https://avatars.discourse-cdn.com/v4/letter/o/54ee81/32.png) [@ohio](https://phaser.discourse.group/u/ohio)\
**Post date:** [March 16, 2025, 5:53am UTC](https://phaser.discourse.group/t/why-does-this-only-work-in-update-and-create/15224/4 "2025-03-16T05:53:56Z")

</div>

thanks so much. This really helps a lot. Now I don’t have to constantly use `this` as a parameter.

---

<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 21, 2025, 8:42pm UTC](https://phaser.discourse.group/t/why-does-this-only-work-in-update-and-create/15224/5 "2025-03-21T20:42:26Z")

</div>

You can also use `extend` in a scene object config:

> <https://github.com/phaserjs/examples/blob/master/public/src/scenes/extend%20scene%20from%20object.js>
