Hard time trying to reference the callback function itself

Hi!

Here we go again…

I think I still didn’t figure the Phaser scope out correctly. I am having a hard time to refer to the callback function itself (for instances to auto-remove the event itself). This is the demo code I have with a timed event:

for (let w=1; w<=4; w++) {
    var mintime = 500;
    var maxtime = 1000;
    var mytime = Math.random()*(maxtime - mintime) + mintime;
    var eventname = "event"+w;
    this[eventname] = this.time.addEvent ({
        delay: mytime,
        callback: () => {
            console.log(eventname)
        },
        callbackScope: this // I also tried this[eventname] and not setting scope at all
    });
}

The code creates four instances of a time event named from (event1…event4). They are correctly created as if I console.log this it will show the scene with all the four events. However when I try to console.log it from within the callback I get four event4 and not event1…event4 as would be expected.

Any idea?

callback: (e) => {
    console.log(e)
},
args: [eventname]

The way you’re trying it, ‘eventname’ just has the last value set, and it is has nothing to do with the callback.

1 Like

Woha! It is exactly what I was after! Thanks Milton! :slight_smile: