World.seconds question

Link: Remix: - Game Lab - Code.org

What I expect to happen: The student wants the counter to count in seconds so we used world.seconds. What they want is when the game over screen happens, the timer at the top of the screen stops and gives them their overall time. (Is this possible?)
What actually happens: Right now the timer keeps counting up
What I’ve tried: We have tried to use conditional to only run the speed run function if the health is > 1 (this just got rid of the timer when the game over screen occurred). We tried to set variable equal to a given value but var time = world.seconds is a different type of function.

@rachel.krone,

This was a little trickier than I expected and I don’t usually just give code, but …

I created a lost variable at the top of the code and set it to false (game isn’t lost yet).

Then, check out the code below. When the game ends, the timec variable captures the world.seconds and lost is set to true. That code never runs again, so timec variable captures the number of seconds since the game began (one time).

Then, below that in the speedrun function, and as long as the game is running (ie. … lost == false), the time is continuously captured from World.seconds. Once the game is over, time is no longer updated in this function, so the timec variable now contains the time when the game ended and since it no longer updates anywhere, it becomes a static number.

I tried to find a way to hint at a solution, but it’s the end of the day (and the end of the school year) and my brain is fried. Please let me know if you can follow the logic and if not, I’m happy to try and explain.

Mike

@rachel.krone

Turns out, after looking one more time, I don’t even need the “timec = World.seconds” block inside the lose function. It’s redundant. You only need to say if lost == false, lost == true. The last time captured in the speedRun function will be used as the end time.

One less line of code = a tiny bit more efficient.

Mike

Thank you so much, this student is going to be so happy to be able to have the time posted (and I appreciate it as I would not have come up with this debugging help).

I understood your explanation (and thank you for providing it). I don’t think I have ever used a variable in terms of true/false so I learned something new!

I was really tired a few hours ago…lol. i don’t even think the conditional block is needed. Just the lost = true; statement

So, it was me who over complicated it…

Good luck!

Mike

I know I’m a bit late to the party with the answer being resolved and everything, however since I’m assuming you want the time to be accurate enough well into the millisecond range may i suggest Date.now()? this will provide you with the current time in ms, which means is all you’d have to account for is the start time and subtract it from the current time when you die or win and then do the proper formatting but I’d only recommend it as a challenge as it’ll take a bit more setup than just World.seconds