Jump to content

Phaser 3 : set timer event one more time after fireing


rado9408
 Share

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)

Link to comment
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;
    }

}

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...