How to make a stopwatch

i need to make a stop watch that counts upward and stops when somethings happens (like when and object touches another object) how can i do so?

put this into the update function: “insert_variable_here += 1;
if (this.hits(insert_clip_here) {
insert_variable_here = 0”

1 Like

what do i put for “insert_variable_here”?

whatever you want. I would put ‘timer’

1 Like

like this?

Screenshot 2021-04-28 12.34.13 PM

yeah that will work

1 Like

it didn’t
Screenshot 2021-04-28 12.39.39 PM

Can you show the error message that showed up in the console?

1 Like

Screenshot 2021-04-28 12.43.57 PM

Oh you need to change

timer += 1;

to

var timer +=1;

i think

1 Like

actually, no, that would be an assignment error. or whatever it’s called idk.

declare timer in the default tab (or somewhere else) like this: var timer = 0;

then increment timer when you need to: timer++;

2 Likes

like this? not sure about “timer++;”
Screenshot 2021-04-28 12.54.23 PM

timer++ is a shortcut for saying timer+=1

Idk if this is the default or update script, but you shouldn’t set the timer to zero (line 6) and add one to the timer (line 11) in the same script (otherwise, value won’t be greater than 1).

Instead, try this:
Default Script:

window.timer=0;

Update Script:

if(this.hits(project.spector)){
timer=0;
}
timer++;
2 Likes

first you need to declare/define the variable somewhere before changing it. usually somewhere like in the default script.

this.timer = 0;
this.timerRunning = true;

you’re attaching a variable “timer” to the this object, which is the clip. you’re also attaching a “timerRunning” variable to it to. if it is false then the timer should not count up, but if it is true then the timer should continue counting.

i am attaching the variables to the clip so the variable can be accessed from other scripts.

then in the update script, you should write this somewhere:

// only run the code inside the curly brackets
// if timerRunning is true
if (this.timerRunning) {
  this.timer++; // var++ is the same thing as var += 1

  if (this.hits(project.spector)) {
    // set timerRunning to false so it will not continue counting
    this.timerRunning = false;
  }
}
2 Likes

@Hamzah_Alani @pumpkinhead
both of the codes you gave me work fine, i just need a way to display the counter on-screen

Use the text tool to add text, then name the text object something (ex: “text”), and then just add this code to the update script:

text.setText(timer); // Use this.timer if you used pumpkinhead's code
2 Likes

try using the setText method (I think that’s what it’s called)

3 Likes

it was a little laggy at first but it works just fine! thanks :) you too @pumpkinhead

1 Like

I still don’t understand JavaScript lmao
I’m used to Python

i’m sure you’ll get used to it. knowing multiple languages is good.

2 Likes