rado9408

Phaser 3 : set timer event one more time after fireing

Recommended Posts

Hi guys want to create a timer via phaser 3 for my game and everything is working fine for first time when event  is called but I can't call start timer one more time when event is fired already.

Can you please explain, can I use timerEvent.reset() and how correctly use it or is there another solution for that? 

(Players timer should start when it is his turn)

Share this post


Link to post
Share on other sites

Then just create a one-shot timer everytime you'd like to use it. 

If it is a clock that you want to simulate, set up a function to fire every second, and use a boolean to return out of the function if your clock is 'off'

 

Share this post


Link to post
Share on other sites

Here is how I build a clock for games. I hope this is what you mean:

var myClock=new Clock({scene:this,secs:60});

 

class Clock extends Phaser.GameObjects.Container {
    constructor(config) {
        super(config.scene);
        this.scene = config.scene;
        if (!config.secs) {
            this.secs = 60;
        } else {
            this.secs--;
        }

        if (!config.style)
        {
            config.style={fontSize:game.config.width/10,color:'#27ae60',align:'center'};
        }

        this.text1=this.scene.add.text(0,0,"",config.style).setOrigin(0.5,0.5);
        this.add(this.text1);


        this.scene.add.existing(this);

        this.setText();
        this.setSize(this.text1.displayWidth,this.text1.displayHeight);
        ut.emitter.on(G.CLOCK_START,this.startClock,this);
        ut.emitter.on(G.CLOCK_STOP,this.stopClock,this);

    }
    startClock() {
        console.log("startClock");
        this.timer = this.scene.time.addEvent({
            delay: 1000,
            callback: this.tick,
            callbackScope: this,
            loop: true
        });
    }
    stopClock() {
        console.log("stopClock");
        this.timer.remove();
    }
    tick() {
        this.secs--;
        if (this.secs == 0) {
           // this.stopClock();
        }
        this.setText();
    }
    setText() {
        var mins = Math.floor(this.secs / 60);
        var secs = this.secs - (mins * 60);

        secs=this.leadingZeros(secs);
        mins=this.leadingZeros(mins);

        this.text1.setText(mins+":"+secs);
    }
    leadingZeros(num) {
        if (num < 10) {
            return "0" + num;
        }
        return num;
    }

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.