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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.