I wrote a screen fade effect that starts the screen red and fades to black. When the code is called, it works correctly - only once; afterwards the effect fails and I don't know why.
export class FadeEffects {
constructor(game) {
this.game = game;
this.width = 640;
this.height = 480;
this.fadeFinished = false;
this.fade = 255;
}//constructor()
//this fade effect starts red transparent and fades to black
fadeRed() {
//now make the rectangle fade to black
let fadeOut = setInterval(() => {
console.log('in interval');
this.fade -= 1;
//when finished
if (this.fade === 0) {
this.fade = 255;
this.fadeFinished = true;
clearInterval(fadeOut);
}//fade check
}, 25);//fadeIn interval
return this.fadeFinished;
}//fadeRed()
draw(context) {
context.fillStyle = "rgba(" + this.fade + ", 0, 0, 0.5";
context.fillRect(0, 0, this.width, this.height);
}//draw()
}//class FadeEffects
Then in the calling function the object of FadeEffects is instantiated and toRemove is set to TRUE and fadeFinished is set to false. Then this code is called:
if(!this.fadeFinished && toRemove === 'TRUE'){
this.fadeFinished = this.fadeEffect.fadeRed();
this.fadeEffect.draw(context);
}
Then when ready, fadeFinished is set to false again. But for some reason the effect only works the first time.