I am trying to make a timer for my game but whatever I try to make it loop doesn't work and it will only go through once when the scene starts. This isn't my first scene in the game because I need it to start counting only during play.
create: function(){
const self = this
self.startTime = new Date();
self.totalTime = 120;
self.timeElapsed = 0;
self.createTimer();
self.gameTimer = this.time.addEvent({
delay: 100,
callback: self.updateTimer(),
callbackScope: self,
loop: true
});
}
createTimer: function(){
var me = this;
me.timeLabel = me.add.text(600, 40, "00:00", {font: "40px Arial", fill: "#000000"});
me.timeLabel.align = 'center';
},
updateTimer: function(){
console.log("please")
var me = this;
var currentTime = new Date();
//console.log(currentTime)
var timeDifference = me.startTime.getTime() - currentTime.getTime();
console.log(timeDifference)
//Time elapsed in seconds
me.timeElapsed = Math.abs(timeDifference / 1000);
console.log(me.timeElapsed)
//Time remaining in seconds
//Convert seconds into minutes and seconds
var minutes = Math.floor(me.timeElapsed / 60);
var seconds = Math.floor(me.timeElapsed) - (60 * minutes);
//Display minutes, add a 0 to the start if less than 10
var result = (minutes < 10) ? "0" + minutes : minutes;
console.log(result)
//Display seconds, add a 0 to the start if less than 10
result += (seconds < 10) ? ":0" + seconds : ":" + seconds;
me.timeLabel.text = result;
}
I have tried using different methods but it either never repeats or it doesn't work.