rado9408 Posted December 11, 2018 Share Posted December 11, 2018 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 More sharing options...
Rose Game Solutions Posted December 13, 2018 Share Posted December 13, 2018 I have the same question: Are timers meant to be restarted? if not, what methods should I look at if I want a timer that runs the whole game but not as fast as the update()? Link to comment Share on other sites More sharing options...
wclarkson Posted December 13, 2018 Share Posted December 13, 2018 Set the loop property to true. If you want it to run X number of times, set up a counter and then remove the timer when the counter gets to X; this.time.addEvent({ delay: secs, callback: myfunction, callbackScope: this, loop: true }); https://phasergames.com/phaser-3-snippets/phaser-3-time-snippets/ Link to comment Share on other sites More sharing options...
rado9408 Posted December 13, 2018 Author Share Posted December 13, 2018 @wclarkson Probably it's not a solution because I want to start timer every time when I want to. But Loop will execute the timer in X times and nothing more Link to comment Share on other sites More sharing options...
wclarkson Posted December 13, 2018 Share Posted December 13, 2018 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' Link to comment Share on other sites More sharing options...
wclarkson Posted December 14, 2018 Share Posted December 14, 2018 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; } } DUDE and rado9408 1 1 Link to comment Share on other sites More sharing options...
rado9408 Posted December 14, 2018 Author Share Posted December 14, 2018 @wclarkson that is like my solution, thanks your answer was very helpfull Link to comment Share on other sites More sharing options...
Recommended Posts