[Tutorial] Stopwatch / Custom Countdown Timer
audun
Posts: 302 🔥🔥🔥
Hi,
Below is a simple script that dynamically updates a Screen Text with a countdown timer.
1) Text component to update
2) Time (in seconds)
//@input Component.Text myText //@input int time {"label":"Time", "min":0, "max":9999, "step":1} var startTime = 0; var countdownTime = 0; var countdownInterval = null; function startCountdown() { if (countdownInterval != null) { return; } startTime = getTime(); countdownTime = script.time; updateCountdown(); } function stopCountdown() { if (countdownInterval != null) { countdownInterval = null; } // do something here when it reaches 0 script.myText.text = "Wohooo!"; } function updateCountdown() { var remainingTime = countdownTime - (getTime() - startTime); if (remainingTime < 0) { stopCountdown(); } else { var minutes = Math.floor(remainingTime / 60); var seconds = remainingTime % 60; var countdownString = ("00" + minutes).slice(-2) + ":" + ("00" + seconds).slice(-2); script.myText.text = countdownString; countdownInterval = script.createEvent("DelayedCallbackEvent"); countdownInterval.bind(updateCountdown); countdownInterval.reset(1); } } function getTime() { return Math.floor(Date.now() / 1000); } var event = script.createEvent("TapEvent"); event.bind(startCountdown);
You can find a list of all the available events here.
Tagged:
2
Comments
-
This code did not achieve the effect of an instance. I copied the code and bound it to an orthogonal camera, adding text and time, but the text did not change.Which step may have caused a problem for me?
0