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â
what do i put for âinsert_variable_hereâ?
whatever you want. I would put âtimerâ
like this?
yeah that will work
it didnât
Can you show the error message that showed up in the console?
Oh you need to change
timer += 1;
to
var timer +=1;
i think
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++;
like this? not sure about âtimer++;â
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++;
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;
}
}
@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
try using the setText method (I think thatâs what itâs called)
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.