Jump to content

Format timer to seconds:milliseconds (00:00)


Ninjadoodle
 Share

Recommended Posts

Hi panda people!

Just wondering whether somebody can help me with this. I've been looking around, but can't seem to find an easy way to format my timer.

Basically I would like to display seconds:milliseconds, in this format 00:00

I currently have this, and managed to pad the seconds when it goes into single digits, but I'm struggling a bit, to only display 2 digits on the milliseconds.

Thanks in advance for any tips :)

game.module(
    'game.game'
)
.body(function() {

game.createScene('Game', {
    
    init: function() {
 
        // TIMER
        this.countdownTxt = new game.Text('00:00');
        this.countdownTxt.anchor.set(this.countdownTxt.width / 2, 0);
        this.countdownTxt.addTo(this.fg);
        this.countdownTxt.position.set(640, -0.5*(game.system.height - 1920) + 64);
        
        this.countdownTimer = game.Timer.add(10000, function() {
            // DO STUFF
        });
    },
    
    pad: function(number, length) {
       
        var str = '' + number;
        while (str.length < length) {
            str = '0' + str;
        }
       
        return str;
    },
    
    update: function() {
        
        // TIMER
        var milliseconds = parseInt((this.countdownTimer.time()%1000));
        var seconds = parseInt((this.countdownTimer.time()/1000)%60);
        
        this.countdownTxt.setText(this.pad(seconds, 2) + ":" + milliseconds);
        this.countdownTxt.anchor.x = this.countdownTxt.width / 2;
        
        this.countdownTxt.position.set(640, -0.5*(game.system.height - 1920) + 64);
    }
});

});

 

Link to comment
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...
 Share

  • Recently Browsing   0 members

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