Scope issue with callback function - using delayedCall outside of create()

Hi there,

I’m new to Phaser 3 and javascripts in general, and merely achieve what I want by tweeking examples :slight_smile:

I have the following issue:

Within my create function, I call another function when an image is clicked. This function needs 2 parameters.

Within that called function, I want to imlement a delayedCall and call a third function, but I can’t seem to retrieve the scene context in the second fonction.

Here’s what the code looks like:

function create ()
{
var b1 = this.add.image(812, 174, ‘B1’).setInteractive();
var C = this.sound.add(‘C’);
b1.on(‘pointerdown’, function() {playsound(C, “1”, this);}.bind(this)); // I tried various combinations to pass the context without success…
}

function playsound (sound, note, scope)
{

this.time.delayedCall(3000, youWin, []); // I get the error “Cannot read property ‘delayedCall’ of undefined”

}

function youWin ()
{

}

Help will be greatly appreciated !

I think your bind needs to be like (function()… ).bind, or use an arrow function.