(several improvements)
(Fixed blocks.)
 
Line 8: Line 8:
 
<scratchblocks>
 
<scratchblocks>
 
when gf clicked
 
when gf clicked
delete (all v) of [Dates v]
+
delete all of [Dates v]
 
add [31] to [Dates v]
 
add [31] to [Dates v]
 
add [30] to [Dates v]
 
add [30] to [Dates v]
Line 22: Line 22:
 
when gf clicked
 
when gf clicked
 
if <(current [month v]) = (3)> then
 
if <(current [month v]) = (3)> then
replace item (1 v) of [Dates v] with ((item (1 v) of [Dates v]) - (current [date v]))
+
replace item (1) of [Dates v] with ((item (1) of [Dates v]) - (current [date v]))
 
else
 
else
replace item (1 v) of [Dates v] with (0)
+
replace item (1) of [Dates v] with (0)
 
if <(current [month v]) = (4)> then
 
if <(current [month v]) = (4)> then
replace item (2 v) of [Dates v] with ((item (2 v) of [Dates v]) - (current [date v]))
+
replace item (2) of [Dates v] with ((item (2) of [Dates v]) - (current [date v]))
 
else
 
else
replace item (2 v) of [Dates v] with (0)
+
replace item (2) of [Dates v] with (0)
 
if <(current [month v]) = (5)> then
 
if <(current [month v]) = (5)> then
replace item (3 v) of [Dates v] with ((item (3 v) of [Dates v]) - (current [date v]))
+
replace item (3) of [Dates v] with ((item (3) of [Dates v]) - (current [date v]))
 
end
 
end
 
end
 
end
 
end
 
end
set [Days left v] to ((item (1 v) of [Dates v]) + ((item (2 v) of [Dates v]) + (item (3 v) of [Dates v])))
+
set [Days left v] to ((item (1) of [Dates v]) + ((item (2) of [Dates v]) + (item (3) of [Dates v])))
 
</scratchblocks>
 
</scratchblocks>
  
 
[[Category:Scripting Tutorials|Countdown]]
 
[[Category:Scripting Tutorials|Countdown]]

Latest revision as of 06:43, 9 May 2021

A countdown is a timer that reports how much time is left until an event will occur. Projects can also have countdowns too. This tutorial shows how to make one. Within the scope of this tutorial, the timer is counting down to an event at a specific date measured in days. Of course, timers can also be built to be compatible to seconds/minutes with either the built in timer feature or the minute block depending on what the project using it requires.

List

First, a list is needed. A list is a feature which allows the program to stall multiple values at once. Create a list called Dates or anything else that is appropriate.

For each item in the list (except the last), it needs to be the last date of the months before the month of the event. The first item will have the last date of the current month and the last one will have the date of the event. For example, a countdown to the next Scratch Day and it is currently March.

when gf clicked
delete all of [Dates v]
add [31] to [Dates v]
add [30] to [Dates v]
add [9] to [Dates v]

Computing the Countdown

There is now a list but the countdown is not finished. Create a variable called Days left (since the coder is using days) or something else if wanted.

Back to the list, the script below will need to be added to complete the countdown. The first month will be the current month and the last month will be the month of the event. In this case, March (3) is used as the first one and May (5) is used as the last one.

when gf clicked
if <(current [month v]) = (3)> then
replace item (1) of [Dates v] with ((item (1) of [Dates v]) - (current [date v]))
else
replace item (1) of [Dates v] with (0)
if <(current [month v]) = (4)> then
replace item (2) of [Dates v] with ((item (2) of [Dates v]) - (current [date v]))
else
replace item (2) of [Dates v] with (0)
if <(current [month v]) = (5)> then
replace item (3) of [Dates v] with ((item (3) of [Dates v]) - (current [date v]))
end
end
end
set [Days left v] to ((item (1) of [Dates v]) + ((item (2) of [Dates v]) + (item (3) of [Dates v])))
Cookies help us deliver our services. By using our services, you agree to our use of cookies.