# Call function from within create?

**URL:** <https://phaser.discourse.group/t/call-function-from-within-create/10538>\
**Category:** Phaser 3\
**Created:** [October 28, 2021, 9:32pm UTC](https://phaser.discourse.group/t/call-function-from-within-create/10538 "2021-10-28T21:32:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DonP](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/donp/32/5913_2.png) [@DonP](https://phaser.discourse.group/u/DonP)\
**Post date:** [October 28, 2021, 9:32pm UTC](https://phaser.discourse.group/t/call-function-from-within-create/10538/1 "2021-10-28T21:32:37Z")

</div>

Hi, I am wondering if it is possible to call functions from within the create function. I am trying to call a function that sets up second player controls, however when I call the function that handles the control set up from within the create function, i get errors, such as 'cannot read properties of undefined (reading keyboard) . But when I use the same code from the set up function directly within the create function, it works. So is it not possible to call functions from within create?

---

<div class="post-metadata">

**Author:** ![NickHatBoecker](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/nickhatboecker/32/5380_2.png) [@NickHatBoecker](https://phaser.discourse.group/u/NickHatBoecker)\
**Post date:** [October 28, 2021, 11:47pm UTC](https://phaser.discourse.group/t/call-function-from-within-create/10538/2 "2021-10-28T23:47:47Z")

</div>

Hey Don, please show some code. It might have something to do with the right binding of the scene object. Which is accessible via `this` in `create` function.

Two examples:

**A) Hand over scene**

```javascript
create () {
    const keys = [] // Just a variable do demonstrate function arguments
    setupPlayerControls(this, keys)
}

setupPlayerControls (scene, keys) {
    // Do something with `scene.input.keyboard` and `keys`
}

```

**B) Bind scene**

```javascript
create () {
    const keys = [] // Just a variable do demonstrate function arguments
    setupPlayerControls.bind(this, keys)
}

setupPlayerControls (keys) {
    // Do something with `this.input.keyboard` and `keys`
}

```

---

<div class="post-metadata">

**Author:** ![DonP](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/donp/32/5913_2.png) [@DonP](https://phaser.discourse.group/u/DonP)\
**Post date:** [October 29, 2021, 12:35am UTC](https://phaser.discourse.group/t/call-function-from-within-create/10538/3 "2021-10-29T00:35:57Z")

</div>

Hi, what I am trying to do is

```javascript
let aKey, dKey

```

```javascript
initPlayer2Controls(); // called in create()

```

```javascript

 function initPlayer2Controls()         
 {   
   aKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);    
   dKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
 }

```

---

<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:** [October 29, 2021, 12:41am UTC](https://phaser.discourse.group/t/call-function-from-within-create/10538/4 "2021-10-29T00:41:38Z")

</div>

```
initPlayer2Controls.call(this);
```

---

<div class="post-metadata">

**Author:** ![DonP](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/donp/32/5913_2.png) [@DonP](https://phaser.discourse.group/u/DonP)\
**Post date:** [October 29, 2021, 12:51pm UTC](https://phaser.discourse.group/t/call-function-from-within-create/10538/5 "2021-10-29T12:51:35Z")

</div>

Thank you, got it working now with this solution.
