From Test-Scratch-Wiki

(Redirected from Eng:When Green Flag Clicked)

When Green Flag Clicked
2.0 When Green Flag Clicked.png
Category Events
Type Hat
Introduced in 1.0

The When Green Flag Clicked block is an Events block and a Hat block. Scripts that wear this block will activate once the Green Flag has been clicked — these scripts can activate other scripts and enable the entire program.

Without this block, the only way a project could run would be that it would sense the pressing of a key or clicking a sprite; the project would only last until all scripts depending on the starting scripts have ended. This block is also the most used block in all of Scratch.[1]

While it is fully possible to script projects without this block, it is not recommended; the Flag represents the start of a project, so this block will sense when the project has been started.

Example Uses

When a project is started, numerous things happen — these happenings occur at this time because they are hatted with this block. Some common uses:

when gf clicked
delete (all v) of [list v]
when gf clicked
set [variable v] to (0)
when gf clicked
hide
switch costume to [costume1 v]
  • Clearing all pen
when gf clicked
clear
  • Playing (a) song(s)
when gf clicked
forever
play sound [song v] until done
when gf clicked
create clone of [myself v]

After these occurrences, the script can continue normally. For example, (with several of those occurrences):

when gf clicked
clear graphic effects
go to x: (0) y: (0)
broadcast [Start! v]
forever 
if <(meter) < (1)> then
broadcast [You Lose v]
end

Errors

Using more than one of these blocks in a single project can create errors that are difficult to diagnose, often only appearing with every other start of the project and can cause the timing to go off. A simple solution is to use only one of these blocks followed by a broadcast to all other scripts that need to run when the project begins. Another solution is to click the flag multiple times or use turbo mode.

when gf clicked
broadcast [initialize v]

Workaround

Main article: List of Block Workarounds


when [timer v] > (-1)
say [Hello world!]

This is the only current workaround for the When Green Flag Clicked block. Scratch suppresses all hat blocks when the project first opens, and when the green flag is clicked, the timer is set to 0, and the hat block is triggered. The only problem with this method is that it automatically triggers if a user goes into a project.

when I receive [Scratch-StartClicked v]
say [Hello, world]

Before Scratch 2.0, this workaround could be used because clicking the green flag broadcasted "scratch-startclicked".[2] However, it no longer broadcasts the message, so this workaround will only work in Scratch 1.4 and below, as well as the Java Player.

When Stop Clicked

"When Stop Clicked" is an event block requested by many Scratchers, that triggers when the stop sign is pressed. These requests are usually turned down, however, because the stop sign was made to stop the project, not to trigger other scripts.

A workaround for the "When Stop Clicked" block uses the following code:

define check
forever
reset timer // resets the timer every tenth of a second to keep it below 0.2
wait (0.1) secs
end

when gf clicked
check

when [timer v] > (0.2) // when stop is clicked, the timer will not reset, allowing its value to rise above 0.2
... // what is done when stop is clicked
check

References

Cookies help us deliver our services. By using our services, you agree to our use of cookies.