When This Sprite Clicked | |
when this sprite clicked | |
Category | Control/Event |
Type | Hat |
Introduced in | 1.0 |
The When This Sprite Clicked block is an Events block and a hat block. Scripts that wear the block will activate once its sprite or clone of the sprite is clicked. Contrary to its definite name, the block will also execute the clone's script when the clone is clicked on.
Clicking on transparent areas of the sprite does not trigger this event, except for transparent areas surrounded by using the hollow rectangle and oval tools in the bitmap editor.
If a sprite has been hidden by the hide block, when its location is clicked on, the script under the When This Sprite Clicked block will not activate.[1]
In the stage, this block is called When Stage Clicked.
when stage clicked
History
In Scratch 1.x, this block was called When () Clicked. It was automatically filled in with the name of the sprite it was placed on.
In Scratch 2.0, its name was changed to When This Sprite Clicked, likely to make the block name consistent for all sprites. In early versions, it was called When I Am Clicked, but this was changed for unknown reasons.
Activation
This will run the scripts below it whenever the sprite is clicked. If the sprite is set to be draggable by the set drag mode block, it will activate when the sprite is released from being dragged.
Note: | This will also run for clones of a sprite when they are clicked. |
Example Uses
- Making buttons
when this sprite clicked broadcast (open menu v) //an example button that opens a menu
when this sprite clicked switch backdrop to (meadow v) //changing backdrops
- Interacting with objects
when this sprite clicked repeat (20) change [whirl v] effect by (5) end repeat (10) change size by (-10)
- Input controls
when this sprite clicked say [Hello!] for (2) secs ask [How are you feeling today?] and wait if <(answer) = [Happy]> then say [That's good!] for (2) secs
- Menus
when this sprite clicked set size to (90)% wait (0.5) secs set size to (100)% broadcast (Menu v)
Workaround
- Main article: List of Block Workarounds
This block can be replicated with the following code:
Note: | These do not perfectly replicate the behavior of this block, as they will not detect tap to click when using a trackpad. |
when gf clicked forever wait until <not <mouse down?>> wait until <mouse down?> if <touching (mouse-pointer v)?> then . . . end
or
when gf clicked forever if <<mouse down?> and <touching (mouse-pointer v)>> then . . . wait until <not <mouse down?>> end
or
when [timer v] > (0.1) wait until <<mouse down?> and <touching (mouse-pointer v)?>> . . . wait until <not <<mouse down?> and <touching (mouse-pointer v)?>>> stop [this script v]