Adding a count down timer in Phaser 3.6

const config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    scene: {
        preload: preload,
        create: create,
        update: update
    }
};

const game = new Phaser.Game(config);

let countdownText;
let countdownValue = 60; // Initial countdown time in seconds
let countdownTimer;

function preload() {
    // Load any assets you need here
}

function create() {
    countdownText = this.add.text(400, 300, '', { fontSize: '48px', fill: '#fff' });
    countdownTimer = this.time.addEvent({
        delay: 1000,
        repeat: countdownValue - 1,
        callback: updateCountdown,
        callbackScope: this,
    });
}

function update() {
    // Your game logic goes here
}

function updateCountdown() {
    countdownValue--;
    countdownText.setText(countdownValue.toString());

    if (countdownValue === 0) {
        // Time's up, you can add your logic here
        countdownText.setText("Time's up!");
        // Stop the countdown timer
        countdownTimer.remove();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *.

*
*
You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>