In my Phaser 3 game, when the player loses I want it to display a game-over panel. I’ve added a rectangle (using graphics.fillRect
) and add a tween to make it scale and fade in. Then when it has appeared, the scores and results are calculated and should also appear with different animations in the rectangle.
The problem is that when the onComplete function is called, it is called in the context of the Tween and not the GameScene. This gives error because I want to use Scene function like add.bitmaptext.
I’ve looked here but couldn’t find anything related. Is it possible to set the context of the onComplete function? See my code below
checkGameOver: function() {
// code..
// show panel for gameover texts etc.
var tw = this.tweens.add(
{
targets: this._gameoverpnl,
scaleY: 1.0,
alpha: 1.0,
ease: 'Power3',
duration: 1000, // duration of animation; higher=slower
delay: 500, // wait 500 ms before starting
onComplete: this.showScoresResults // set context? how?
}
);
},
showScoresResults: function() {
// `this` is now the `Tween` not the `GameScene`, so add.bitmapText gives error
var txt = this.add.bitmapText(100, 320, "fontwhite", "", 40); // <-- Error here
// etc. code for results calcultation and animations..
},