Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Help with making a timer with sprites
- Abrima1
-
Scratcher
3 posts
Help with making a timer with sprites
Hello everyone, I'm trying to make a countdown timer that will start from 25 till 0 (out of sprites). I've already made costumes of numbers 0-9 in one sprite but i dont know how to make them appear based on the timer's countdown number. Thank you.
Last edited by Abrima1 (May 24, 2020 17:07:33)
- codentag
-
Scratcher
100+ posts
Help with making a timer with sprites
You'll need a script like this:
when I receive [start countdown v]
switch costume to [9 v] // this is costume name, you can change this to 25 when you made all the costumes
set [index v] to (costume #)
repeat until <(costume #) = [0]> // this is costume number, not name; also change to 25
change [index v] by (-1)
switch costume to (index)
end
- Abrima1
-
Scratcher
3 posts
Help with making a timer with sprites
You'll need a script like this:when I receive [start countdown v]
switch costume to [9 v] // this is costume name, you can change this to 25 when you made all the costumes
set [index v] to (costume #)
repeat until <(costume #) = [0]> // this is costume number, not name; also change to 25
change [index v] by (-1)
switch costume to (index)
end
Hi, thank you for your help! I'm so sorry I forgot to mention something important, the countdown is supposed to be in minutes not seconds. Also, regarding your solution, your code starts with 9 when the first digit should start with 2 while the second digit should start with 5 and count down till both reach 0.
I believe your solution would only work for one digit, I think I have to create a clone and set that clone to different digit, so I'm not sure how to do that to be honest.
- deck26
-
Scratcher
1000+ posts
Help with making a timer with sprites
Have a variable for the countdown - so you initialise it to 25. There is no need for the scripts displaying the number to have any fixed value preloaded. You then have a loop that changes the value once a minute and broadcasts to the sprites to update their costumes.
The sprite that shows the tens digit uses floor of variable/10 to get the value for its current digit. If that is zero you may prefer to hide the sprtie. the units sprite gets the value of its digit from variable mod 10.
If you want to use clones rather than digits you could use the sprite for one digit and a clone for the other. Use a local cloneID variable and you can get one object to behave differently to the other with a simple if.
The sprite that shows the tens digit uses floor of variable/10 to get the value for its current digit. If that is zero you may prefer to hide the sprtie. the units sprite gets the value of its digit from variable mod 10.
If you want to use clones rather than digits you could use the sprite for one digit and a clone for the other. Use a local cloneID variable and you can get one object to behave differently to the other with a simple if.
when I receive [showtime v]
if <(cloneID) = [0]> then
code for units
else
code for tens
end
- codentag
-
Scratcher
100+ posts
Help with making a timer with sprites
Hi, thank you for your help! I'm so sorry I forgot to mention something important, the countdown is supposed to be in minutes not seconds. Also, regarding your solution, your code starts with 9 when the first digit should start with 2 while the second digit should start with 5 and count down till both reach 0.
I believe your solution would only work for one digit, I think I have to create a clone and set that clone to different digit, so I'm not sure how to do that to be honest.
Whoops, I was basing it on seconds. Looks like I also forgot the wait block entirely. Apologies for my extreme negligence. I need more sleep
Deck26's answer describes how to do this well.
- 384207AD
-
Scratcher
75 posts
Help with making a timer with sprites
when green flag clicked
set [timer] to [0]
wait (60) secs
change [timer] by (1)
- Abrima1
-
Scratcher
3 posts
Help with making a timer with sprites
Thank you guys so, so much for your replies. I wasn't able to follow much of the instructions you provided because I'm still learning and I'm awfully terrible at Scratch (And embarrassingly math as well), even though I tried my best. However, I did come up with my own way of solving this problem by combining some of the tips you guys helped me with! So again, thanks a lot!
I'm almost done with the project but its still not working as it should, not sure if I should make a new thread regarding this so please let me know if that's the case.
Anyway, the timer needs to change the backdrop to break-time when all the timer variables = 0 and then stop the timer temporarily, wait for the break of 5 minutes (Which is also a timer) to finish then reset the timer to 25:00 mins. Since this is a pomodoro timer, it should do this cycle of reset/breaks 4 times, and then after that the break duration becomes 15 mins instead of 5 and that's it. I'm planning to add a pause/resume button as well but I think it would mess up everything as the code blocks don't seem like it would work with a button like that.
Here's the link to my project: https://scratch.mit.edu/projects/398289787/editor/
The timer is supposed to start at 25 mins but to make things quick for now, I've set it to 20 seconds.
Any help or feedback with regards to the project is immensely appreciated! Thank you!!
I'm almost done with the project but its still not working as it should, not sure if I should make a new thread regarding this so please let me know if that's the case.
Anyway, the timer needs to change the backdrop to break-time when all the timer variables = 0 and then stop the timer temporarily, wait for the break of 5 minutes (Which is also a timer) to finish then reset the timer to 25:00 mins. Since this is a pomodoro timer, it should do this cycle of reset/breaks 4 times, and then after that the break duration becomes 15 mins instead of 5 and that's it. I'm planning to add a pause/resume button as well but I think it would mess up everything as the code blocks don't seem like it would work with a button like that.
Here's the link to my project: https://scratch.mit.edu/projects/398289787/editor/
The timer is supposed to start at 25 mins but to make things quick for now, I've set it to 20 seconds.
Any help or feedback with regards to the project is immensely appreciated! Thank you!!
Last edited by Abrima1 (May 26, 2020 13:44:48)
- Oumuamua
-
Scratcher
1000+ posts
Help with making a timer with sprites
Hi, you go to https://scratch.mit.edu/projects/399220424/ and see if it helps some way.
Regards.
Regards.
- Discussion Forums
- » Help with Scripts
-
» Help with making a timer with sprites