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?
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
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
create () {
const keys = [] // Just a variable do demonstrate function arguments
setupPlayerControls.bind(this, keys)
}
setupPlayerControls (keys) {
// Do something with `this.input.keyboard` and `keys`
}
Hi, what I am trying to do is
let aKey, dKey
initPlayer2Controls(); // called in create()
function initPlayer2Controls()
{
aKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
dKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
}
initPlayer2Controls.call(this);
Thank you, got it working now with this solution.