# Phaser 3 ES6 disableBody and keyboard input

**URL:** <https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723>\
**Category:** Phaser 3\
**Created:** [June 20, 2020, 10:59am UTC](https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723 "2020-06-20T10:59:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![YayYay](https://avatars.discourse-cdn.com/v4/letter/y/f475e1/32.png) [@YayYay](https://phaser.discourse.group/u/YayYay)\
**Post date:** [June 20, 2020, 10:59am UTC](https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723/1 "2020-06-20T10:59:43Z")

</div>

Hello, I am new to both Javascript and Phaser.  
I’m learning to build something with ES6 format without NodeJS

Here’s how my code looks like:

```javascript
var space;

class gameScene extends Phaser.Scene 
{
    constructor (config) 
    {
        super({key:"game"});
    }

    create ()  
    {
     bushes = this.physics.add.staticGroup();
    bushes.create(50, 425, "bush");  
     bushes.create(200, 425, "bush");
      bushes.create(900, 425, "bush");
      bushes.create(500, 425, "bush");

         berry = this.physics.add.staticGroup();
         berry.create(50, 425, "berry");  
         berry.create(200, 425, "berry");
         berry.create(900, 425, "berry");
         berry.create(500, 425, "berry");

          //code for player goes here
        this.physics.add.overlap(player, berry, this.collectBerry());
}
update()
{
         space = this.input.keyboard.addKey("SPACE");

}

collectBerry (player, berry)
    {
        berry.disableBody(true, true);
        if(space.isDown)
        {
        infoTxt.setText(" berry acquired.");
        }
    }
```

Basically the idea is when the player walks up to the berry and press space the berry will disappear and player gets some berry.  
I get 2 errors when commenting out parts of collectBerry()

```javascript
Cannot read property 'isDown' of undefined
```

and

```javascript
Cannot read property 'disableBody' of undefined
```

I have tried adding this. but it still doesn’t work.  
Does anybody have idea of what I did wrong?

---

<div class="post-metadata">

**Author:** ![retroVX](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/retrovx/32/1491_2.png) [@retroVX](https://phaser.discourse.group/u/retroVX)\
**Post date:** [June 20, 2020, 1:59pm UTC](https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723/2 "2020-06-20T13:59:23Z")

</div>

Hi,

Change this

> [@YayYay](#):
>
> `this.physics.add.overlap(player, berry, this.collectBerry());`

to

`this.physics.add.overlap(player, berry, this.collectBerry);`

Hopefully this fixes it 🙂

---

<div class="post-metadata">

**Author:** ![MrAmericanMike](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@MrAmericanMike](https://phaser.discourse.group/u/MrAmericanMike)\
**Post date:** [June 20, 2020, 2:08pm UTC](https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723/3 "2020-06-20T14:08:45Z")

</div>

When a function takes a function as a parameter, you pass it by the name, not using the () as that is telling calling the function to execute, and what you really want is for the function to be called when an overlap happens, and not right when you add it to the function overlap.  
Hope this helps also clarify a bit as to why that happens.

> **[JavaScript Callback Functions | DashingD3js.com](https://www.dashingd3js.com/lessons/javascript-callback-functions)**
>
> This video covers JavaScript Callback Functions. Specifically, this video covers: a) Passing JavaScript Functions as Variables Revisited and b) JavaScript Callback Functions.

As a side note, for ES6 code I believe you want to start using “const” and “let” and not “var” anymore.

---

<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:** [June 20, 2020, 2:45pm UTC](https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723/4 "2020-06-20T14:45:25Z")

</div>

> [@YayYay](#):
>
> `space = this.input.keyboard.addKey("SPACE");`

This should be in `create()`, not `update()`.

---

<div class="post-metadata">

**Author:** ![YayYay](https://avatars.discourse-cdn.com/v4/letter/y/f475e1/32.png) [@YayYay](https://phaser.discourse.group/u/YayYay)\
**Post date:** [June 20, 2020, 3:39pm UTC](https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723/6 "2020-06-20T15:39:24Z")

</div>

Thank you!  
Do you have any idea how do I fix

```javascript
 Cannot read property 'isDown' of undefined 
```

Apparently I have the same issue with infoTxt.setText

---

<div class="post-metadata">

**Author:** ![YayYay](https://avatars.discourse-cdn.com/v4/letter/y/f475e1/32.png) [@YayYay](https://phaser.discourse.group/u/YayYay)\
**Post date:** [June 20, 2020, 3:40pm UTC](https://phaser.discourse.group/t/phaser-3-es6-disablebody-and-keyboard-input/6723/7 "2020-06-20T15:40:09Z")

</div>

I actually tried it both way before posting. Neither of them worked with same error.
