How do I add a real-time clock to recreate a digital watch?

0 favourites
  • 6 posts
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • I'm wanting to replicate a vintage digital watch game, and include the watch part.

    To do this I'm guessing I'll want to get the system time and display it HH:MM with the colon flashing every second (also at five past nine I want it to display 9:05 (not 9:5)).

    I've looked at Browser.ExecJS("Date()") and have got a close result with mid(Browser.ExecJS("Date()"),16,5) but, of course, there's no flashing colon, and where zeroes should be, there isn't.

    I've also looked at the Rex plugin and while I found the Construct3 version, I couldn't find the moment.js plugin for formatting the output.

    What would be the best way to achieve this if, indeed, it's even possible?

    Thanks :)

  • You can break time into seconds, minutes and hours, use zeropad() expression to add leading zeroes to them, and add a blinking colon character.

    Set Seconds = int(unixtime/1000)%60 
    Set Minutes = int(unixtime/60000)%60
    Set Hours = int(unixtime/3600000)%24
    Set dividerCharacter = (Seconds%2 ? ":" : " ")
    Set timeString to zeropad(hours,2) & dividerCharacter & zeropad(Minutes,2)


    There is one issue with unixtime - it doesn't seem to recognize daylight saving time. So it's probably better to use Javascript, just google how to get time in the required format, there are tons of examples.

  • That sounds ideal - flashing colon and all!!

    Next question, though, putting it all together -- other than creating a text object and doing a Set Text to 'timeString', where do all the other bits go? Do they start as global variables of 0, and the Set commands On start of layout, then every second?

    So far my capx is just returning 23 -- so there's something I'm not putting in the right place.

    Thanks (still quite new at this) :)

  • I just worked out that 23 is 11pm (took me a while to work out, until I reran it and it was 0) - which is good, other than it not displaying the dividerCharacter or minutes; also (this may be a basic question) but how to make this equivalent to my timezone (GMT+13) ?

    Thanks again :)

  • Whew! Worked it out (nearly)!

    Sorry for hijacking my own thread, but if it helps someone in the future :)

    dividerCharacter and timeString are global variables (string)

    seconds, minutes and numbers are global variables (number)

    Set text to timeString every second - bingo!

    The only thing I'm missing now is how to set for my own timezone ??

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • And just for completion's sake - to set it for your own timezone, add or subtract hours from timeString.

    You can then make it AM/PM or stick with military time.

    Make a digital spritefont and, bingo, one digital watch :)

    Many thanks to dop2000 for setting me on the right path for this; much appreciated!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)