When Green Flag Clicked
when green flag clicked
Category Events
Type Hat
Introduced in 28Feb05

The When Green Flag Clicked block, commonly called the Start Block,[1][2] is an Event 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.

A sketch of the block with its counterpart in ScratchJr.

Without this block, the only way a project could run would be that it would sense the pressing of a key, clicking of a sprite, or the timer or project loudness passing a specified value; the project would only last until all scripts depending on the starting scripts have ended.

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 occur at this time because they are hatted with this block. Some common uses:

  • Deleting all items in a list
when gf clicked
delete all 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
erase all
  • Playing (a) song(s)
when gf clicked
forever
play sound (song v) until done
play sound (song2 v) until done // optional
when gf clicked
create clone of (myself v)
  • Activating other Scripts
when gf clicked
broadcast (Start v)

when I receive [Start v]
...
  • Resetting Sprite positions
when gf clicked
go to x: (0) y: (0) // numbers can be changed

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 sometimes create errors that are often difficult to diagnose and can cause the timing to go off. This happens because when the green flag is clicked, they are all run based on the sprite layering from front to back, and it is common to adjust the layering of sprites when the green flag is clicked; this can lead to behavior that appears once every few times the flag is clicked. 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 minimize layering changes throughout the project, as that way, there will be far lower of a chance for unexpected behaviors to arise, and running everything through a single broadcast can still cause some of the same issues.

when gf clicked
broadcast (initialize v)

Workaround

Main article: List of Block Workarounds


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

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.

Another method is:

when video motion > ((-1)/(0))
say [Hello World!]

Another method is:

when [loudness v] > (-1)
say [Hello World!]

Old Workaround

Archive.png This article or section documents something not included in the current version of Scratch (3.0). It is only useful from a historical perspective.


Green flag workaround.gif

Before Scratch 2.0, this workaround could be used because clicking the green flag broadcasted "scratch-startclicked".[3] 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 Sign Clicked

The when @stopSign clicked:: events hat block is an event block requested by many Scratchers, that would trigger whenever the stop sign is pressed. These requests are usually turned down[4], 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:

when gf clicked
forever
    set [timer v] to (timer) // A variable called timer will hold the value of the timer
end

when [timer v] > (timer::variables) // When stop is clicked the variable will not change allowing the value of the actual timer to rise above that of it.
... // what is done when stop is clicked

Another more common workaround for the same block uses the following code:

when gf clicked
forever
    reset timer
end

when [timer v] > [0.1] // The smaller the number, the more likely the script will trigger on laggier and slower devices.
...

References

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