Hello guys,
I didnât reach to implement my Stopwatch in ZapWorks Studio.
In fact, the js functions: setInterval() and clearInterval() canât be find.
Please, help
Hello guys,
I didnât reach to implement my Stopwatch in ZapWorks Studio.
In fact, the js functions: setInterval() and clearInterval() canât be find.
Please, help
Hi Loick,
We donât use those functions as they are part of the window object which we donât expose in ZapWorks Studio.
We have however implemented our own versions, namely z.every( ) and z.everyOff( ).
Check out our other utility functions here - https://docs.zap.works/studio/scripting/utility-functions/
Thanks,
Mark
Dear Mark
Is there any examples for how to do this? I would like to do a timer countdown.
Thank you.
Milenne
I have a simple 10 sec timer I made for my Jackâs Revenge game.
Is that what your looking for?
Steve
For my game, I ended up creating an over-arching script in the root node that had a simple function which incremented a variable I called timer. I then used Z.every to call that function to create a system clock.
The trick is feeding it out to other scripts and for that you need to export that variable using
export let
when you declare. Iâve had mixed success. Itâs a work in progress.
Yes, I have managed to do it. I have got an example script from Jvuiloon.
https://forum.zap.works/t/count-down/2020/4
THank you. I have another question which I will put in a new thread.
Hey everyone
I have one more question if anyone could please help if you can of course.
I have the timer code below.
What I want to do it to add an if timer is equal 0, then switch certain things off and stop the game. I have tried but it goes off right at the beginning of the game�
That is the code as the original
//Set Timer
var timer = 90;
parent.on(âpointerdownâ, (e) => {
Z.every(1000,myTimer);
});
function myTimer(){
symbol.nodes.debug.text("timer: "+timer);
if(timer--<=0)Z.everyOff(myTimer);
}
Thank you
Milenne
Didi you try adding a call to a function:
function myTimer(){
if(timer--<=0){
Z.everyOff(myTimer);
stopGame();
}
}
function stopGame(){
// do what you have to do...
}
Thanks jvouillon
Yes, that is what I have there. When it gets to zero the timer stops. I have then added lines to stop the game.
Something like:
if(timerâ<=0){
Z.everyOff(myTimer);
symbol.controllers.Question1CNT.elements.n1_QuestionOff.activate();
}
But then for some reason it stops the game at the beginning rather than the end. It stops when the timer is 90 and not zeroâŚ
Just to be sure it is not a syntax error, could you check your condition:
what I read in yours, is:
if(timerâ<=0)
instead of:
if(timer--<=0)
It is if(timerâ<=0)
//Set Timer
var timer = 90;
parent.on(âpointerdownâ, (e) => {
Z.every(1000,myTimer);
});
function myTimer(){
symbol.nodes.debug.text("timer: "+timer);
if(timer--<=0)Z.everyOff(myTimer);
// stopGame();
}
When I put the stopGame, it stops at the beginningâŚ
Then I added an extra one:
if(timer==0)
stopGame();
it works but then when press the play again the timer doesnât go back to 90âŚ
You need the curly brackets to have more than one instruction with the if statement:
if(timer--<=0)Z.everyOff(myTimer);
stopGame(); // out of the if statement
if(timer--<=0){
Z.everyOff(myTimer);
stopGame(); // in the if statement
}
I see. Yes, the game ends
Now when I press the button play again the timer doesnât go back to 90âŚ;-(
I have a button play again which it goes back to instruction screen so you enter the same PLay button as before but the times goes to -1 or -2. oh dear!!!
Just reset the timer value to 90, in your btn playAgainâŚ
ok. I will try that. THank you. Basic coding knowledge showing
The play again button just links to the starter screen which then when I press play there is the var=90⌠so why doesnât that reset when I press play ?
It seems that your instruction:
var timer = 90;
is outside the parent.on(âpointerdownâ⌠) function
To make things simple, the instructions in a script are executed just one time, basically when the app is loaded.
If you want instructions to be executed several times, you have to put them in a function and call the functionâŚ
So, for example, one solution could be:
var timer; // the timer variable is created one time,,,
parent.on(âpointerdownâ, (e) => {
timer = 90; // the timer variable is set to 90 each time the button is pressed...
Z.every(1000,myTimer);
});
Ha! Thank you jvouillon.
It worked
Have a great day.
Milenne