# Scope issue (again)

**URL:** <https://phaser.discourse.group/t/scope-issue-again/9633>\
**Category:** Phaser 3\
**Created:** [May 20, 2021, 2:14pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633 "2021-05-20T14:14:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![YanKleber](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yankleber/32/5152_2.png) [@YanKleber](https://phaser.discourse.group/u/YanKleber)\
**Post date:** [May 20, 2021, 2:14pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/1 "2021-05-20T14:14:24Z")

</div>

I created a custom method into a scene class but am not being able to access it. This is my code:

```
class MyScene extends Phaser.Scene {

    constructor () {
        super({key: "myScene"});
    }
  
    preload() {
        this.load.image('scnMyscene','assets/images/myscene.png');
        this.load.image('btnSound','assets/buttons/btnSound.png');
    }

    create() {
        var scnMyscene = this.add.image(512, 384, "scnMyscene")
        var btnSound = this.add.image(752, 135, "btnSound").setInteractive();

        btnSound.on('pointerup', function (event) {
           this.DoSound(); // won't work
           DoSound(); // won't work either
        });
    }

    DoSound() {
        console.log('do')
    }
}

export default MyScene;

```

It’s puzzling me because as far as I know in this context _this_ represents the scene itself and so it should work. What’s wrong here?

Also, how can I **CLOSE** this scene?

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 20, 2021, 2:46pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/2 "2021-05-20T14:46:22Z")

</div>

Use more logging 🙂

‘this’ will refer to btnSound. If you want to refer to scene, use (event) =\> { … }  
DoSound without any prefix means ‘window.DoSound’. That doesn’t exist.

## Destroy scene[¶](https://rexrainbow.github.io/phaser3-rex-notes/docs/site/scenemanager/#destroy-scene)

```javascript
scene.scene.remove(key);

```

---

<div class="post-metadata">

**Author:** ![YanKleber](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yankleber/32/5152_2.png) [@YanKleber](https://phaser.discourse.group/u/YanKleber)\
**Post date:** [May 20, 2021, 3:00pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/3 "2021-05-20T15:00:33Z")

</div>

Milton, unfortunately two things failed for me…

First I tried:

event.DoSound();  
**Uncaught TypeError: event.DoSound is not a function**

event.scene.DoSound();  
**Uncaught TypeError: Cannot read property ‘test’ of undefined**

Also it failed too:

```
btnClose.on('pointerup', function (event) {
   scene.scene.remove('myScene');
}); 

```

Any idea?

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 20, 2021, 3:04pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/4 "2021-05-20T15:04:01Z")

</div>

I meant, use an arrow function 🙂

change

```javascript
btnSound.on('pointerup', function (event) {

```

to

```javascript
btnSound.on('pointerup', (event) => {

```

---

<div class="post-metadata">

**Author:** ![YanKleber](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yankleber/32/5152_2.png) [@YanKleber](https://phaser.discourse.group/u/YanKleber)\
**Post date:** [May 20, 2021, 3:25pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/5 "2021-05-20T15:25:02Z")

</div>

**Milton** , I got it. But in fact it’s driving me crazy! LoL!

When I use the arrow function _this_ will refer to the scene from within the function event, correct? And how I reach the scene from my custom method? See:

```
create() {
   var btnSound = this.add.image(752, 135, "btnSound").setInteractive();
   btnSound.on('pointerup', (event) => {
      this.DoSound();
   });
}

DoSound() {
   console.log ('DoSound'); // it works!
   btnSound.setVisible(false); // Cannot read property 'setVisible' of undefined
}
```

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 20, 2021, 3:36pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/6 "2021-05-20T15:36:42Z")

</div>

You have declared btnSound inside create(). Either make it a class member (this.btnSound), or a global (outside the class).

---

<div class="post-metadata">

**Author:** ![YanKleber](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yankleber/32/5152_2.png) [@YanKleber](https://phaser.discourse.group/u/YanKleber)\
**Post date:** [May 20, 2021, 4:02pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/7 "2021-05-20T16:02:40Z")

</div>

I see!

A final question… in this case how could I target the object itself without explicitly declare its whole “path”?

For instances I see that it works:

```
create() {
   this.btnSound = this.add.image(752, 135, "btnSound").setInteractive();
   this.btnSound.on('pointerup', (event) => {
      this.btnSound.setVisible(false);
   });
}

```

There is a way of target itself inside the function? Or forcing declare the objects as part of the scene I cannot target to self?

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [May 20, 2021, 4:15pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/8 "2021-05-20T16:15:55Z")

</div>

I’m not sure I understand the question.  
If you use an arrow function, the ‘target’ (this) is not set, and therefore remains the class (scene).  
If you use functon() {}, ‘this’ is set to the target, but then you’ll need a reference to the scene. See the [solution](https://phaser.discourse.group/t/dynamically-creating-buttons-but-wont-retrieve-data-from-it/9622/9) to your previous post.

---

<div class="post-metadata">

**Author:** ![YanKleber](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/yankleber/32/5152_2.png) [@YanKleber](https://phaser.discourse.group/u/YanKleber)\
**Post date:** [May 20, 2021, 5:17pm UTC](https://phaser.discourse.group/t/scope-issue-again/9633/9 "2021-05-20T17:17:46Z")

</div>

I think I got it. I always had a certain problem to understand scope, specially with classes. Thanks for all your huge patience and help!

People like you makes all the difference in a day in the life of a coder.

😊

---

<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:** [May 21, 2021, 2:05am UTC](https://phaser.discourse.group/t/scope-issue-again/9633/10 "2021-05-21T02:05:22Z")

</div>

There’s no implicit access to properties of `this`, if that’s what you mean.

`this.btnSound` and `btnSound` would be completely different values.
