I'm trying to add a fade to black event to my game. It triggers after a certain dialogue is triggered, and causes the screen to fade to black and then switch to another scene. I started by creating a big, black sprite that covers the entire screen, the set its alpha to 0. Here is the code that makes the fade work:
this.line1.setText('Scally: Its been a long day Peef. Ready to hit the hay?');
this.line2.setText('Press A for: Yeah I am ready to sleep. or Press D for: Naw, I am gonna stay up a bit more.');
this.time.addEvent({
delay: 2500,
callback: () => this.blackFade.setAlpha(0.2),
callback: () => console.log("1"),
callbackScope: this,
});
this.time.addEvent({
delay: 4500,
callback: () => this.blackFade.setAlpha(0.4),
callback: () => console.log("2"),
callbackScope: this,
});
this.time.addEvent({
delay: 6500,
callback: () => this.blackFade.setAlpha(0.6),
callback: () => console.log("3"),
callbackScope: this,
});
this.time.addEvent({
delay: 8500,
callback: () => this.blackFade.setAlpha(0.8),
callback: () => console.log("4"),
callbackScope: this,
});
this.time.addEvent({
delay: 10500,
callback: () => this.blackFade.setAlpha(1),
callback: () => console.log("5"),
callbackScope: this,
});
this.time.addEvent({
delay: 12500,
callback: () => this.scene.switch('bedRoomDay2'),
callbackScope: this,
});
As I have this code in the update method, I'm having an issue where the timer events are triggered multiple times, causing the screen to only darken a little with occasional darker flashes before switching scenes, never becoming completely black. Is there a way to ensure that the timer events only trigger once, preferably without moving the code out of the update method?
If it helps, I'm using Phaser 3 in VSCode employing arcade physics.
Well you would need a helper variable to prevent multiple calls, and instead of the
timer, I would recommend using one simplytweencall, since the tween would update thealphaproperty in a smooth fashion.Both mentioned concepts are demonstrated in the following example.
Small fade-out Demo:
Update:
There are several way to let the black screen linger, but the easy one is simply to increase the
durationand/or thealphavalue of the tween. thealphavalue will theSmall fade-out & linger Demo:
Setting
alphato2this means, that half of the tweendurationabout ~3000ms the screen will be black. if you need less us can adjust thealphaproperty.