so i am creating a scratching game where the user is supposed to scratch the cover the clear the area to reveal what is under the cover. the first part is done but i am unable to restart the game when he wants to play again. how can i bring back the cover when the user click the play again button?
i am trying to do this:
create() {
super.create();
this.createTexture();
this.scratchOff();
this.Buybuttons();
this.createInfoContainer();
this.scratchAll();
this.playAgainFct();
this.board = this.make.image({key: BOARD,add: false});
this.add.image(...this.screenCenter, BOARD).setScale(0.75);
this.ticketValues = this.add.text(10, 50);
}
createTexture() {
this.coverImage = this.textures.get(SKY_IMAGE_BLACK).getSourceImage();
this.coverHelperCanvas = this.textures.createCanvas("coverTexture",this.coverImage.width,this.coverImage.height);
this.coverHelperCanvas.draw(0, 0, this.coverImage);
this.coverHelperCanvas.context.globalCompositeOperation = "destination-out";
this.canvasCollection = this.coverHelperCanvas;
}
scratchOff() {
this.precent = this.add.text(10, 10, "");
let cover = this.add.image(this.screenCenter[0] - 171, this.screenCenter[1], "coverTexture").setInteractive().setScale(0.76).setDepth(1);
this.add.image(this.screenCenter[0] - 171, this.screenCenter[1], SKY_IMAGE).setScale(0.76).setDepth(0);
if (cover.on("pointerdown", this.startDrawing, this))
if (cover.on("pointerup", this.stopDrawing, this))
if (cover.on("pointermove", this.clearCover, this))
this.checkTimer = this.time.addEvent({
delay: 250,
loop: true,
callback: this.checkPercent,
callbackScope: this,
});
}
startDrawing() {
this.isPointerDown = true;
}
stopDrawing() {
this.isPointerDown = false;
}
clearCover(e, x, y) {
if (this.isPointerDown) {
let radius = 50;
this.coverHelperCanvas.context.beginPath();
this.coverHelperCanvas.context.arc(x, y, radius, 0, Math.PI * 2, false);
this.coverHelperCanvas.context.fill();
this.coverHelperCanvas.update();
this.onScratchParticles();
}
}
checkPercent() {
let full = this.coverImage.width * this.coverImage.height;
let { data } = this.coverHelperCanvas.context.getImageData(0,0,this.coverImage.width,this.coverImage.height);
let current = data.filter((v, i) => (i + 1) % 4 == 0 && v > 0).length;
let percentage = ((current / full) * 100).toFixed(2);
this.precent.setText(`Cover Percent: ${percentage}%`);
if (percentage <= 50) {
this.coverHelperCanvas.context.clearRect(0,0,this.coverImage.width,this.coverImage.height);
this.coverHelperCanvas.update();
this.destroyParticles();
this.checkTimer.remove();
}
}
playAgainFct() {
const menuPosition = [this.screenCenter[0] - 190, this.screenCenter[1] + 280];
const playAgain = new CustomButton(this, ...menuPosition, "button2", "button3", "Play Again");
this.add.existing(playAgain);
playAgain.setInteractive();
playAgain.on(Phaser.Input.Events.GAMEOBJECT_POINTER_DOWN, () => {
this.createTexture();
this.scratchOff();
});
}
i am using the playagainFct to call the createTexture and scratchOff function again but when i do that it give an error that says ('Cannot read properties of null (reading 'draw') at Scratch.createTexture'')