Timer doesn't work

My first attempt at making a game. Stole… uh, I mean, borrowed code from a couple of games I found here and on YouTube, so I’m guessing they are interfering with each other somehow? Trying to count down from 30, but timer resets to 30 after each second. And now I see that since I’m new here, I can’t upload my file to get help.

So, here’s some of the code:
On Default:
gameOver = false;
timer = 0;
maxTime = 60;

On Update:

if (gameOver === false) {
timer+=1;

if(timer>maxTime) {
    timer = 0;
    maxTime -= 2;
    
    if(maxTime < 20) {
        maxTime = 20;
    }

On Default:
project.seconds = 1;
project.secondpassed = 30;

On Update:
project.seconds += 1;
if (project.seconds == 60) {
project.secondpassed -= 1;
project.seconds = 0;
}
secondtext.setText(project.secondpassed);

use project.seconds += 1/project.framerate if you want to use a similar method
Personally, I would use the Date object to calculate

1 Like

I haven’t tried your code, but looking at it, I can see why it wouldn’t work.

Here’s how I would do it (using the same method @Donut mentioned):

Default:

window.seconds = 33;
window.counter = seconds*project.framerate;

Update:

counter-=counter>0?1:0;
var time = Math.max(Math.ceil(counter/project.framerate),0);
text.setText(time+' seconds');
text.fillColor = counter===0?'red':'black'; // This line is optional

Here’s the same code, but with notes to help explain what each line does:

Default:

window.seconds = 33; // The total seconds
window.counter = seconds*project.framerate; // This variable will count how many frames the project runs in the total time

Update:

counter-=counter>0?1:0; // If the counter isn't zero, this line will take away 1 from the counter at the speed of the project's framerate
var time = Math.max(Math.ceil(counter/project.framerate),0); // Setting the time variable to the counter divided by the frame rate, the answer is rounded up
text.setText(time+' seconds'); // Setting the text to the time value
text.fillColor = counter===0?'red':'black'; // This line is optional, it changes the text's color to "red" when the time is zero

(Note: Replace “text” with the name of your text object if it’s not already named “text”)

Thank you so much for your help! Unfortunately, I got the exact same result. When I pasted the code you have given me, the timer starts at 33 goes to 32 and then immediately back to 33. Over and over. Somehow it’s resetting. Stitch Game10-6-2022_14-24-35.wick (321.8 KB)

I looked at the w3schools Date Objects, but I’m afraid I couldn’t figure it out.

Thanks anyway!

I see the problem, it seems that you have the code for the timer inside of the clip named “cone.”
The clip is constantly cloned, and every clone has the same code as that of the “cone” clip.

Inside of the “cone” clip’s default script, the timer’s seconds are set to 33, so every new clone of the cone object will also set the seconds to 33.

It should be easy to fix, all you need is to move the timer’s code to another clip that doesn’t constantly get cloned, or add an “if(!this.isClone)” condition to make sure the object isn’t a clone before making any adjustments to the timer inside of the “cone” clip.

Here’s the same project with simple fixes to the timer:
Stitch Game.wick (321.8 KB)

2 Likes

Oh my goodness! Yes, that makes total sense! Thank you so so much!

2 Likes