![]() |
Please expand this article or section. You can help by adding more information if you are an editor. More information might be found in a section of the talk page. (April 2020) |
Click sensing is the detection of mouse clicks on a sprite or the stage. It is commonly used in many Scratch projects. Multiple scripts and hat blocks can be used to sense when a sprite or backdrop is clicked.
Hat Blocks
The two hat blocks to sense when a sprite/stage is clicked are the When This Sprite Clicked block and the When Stage Clicked block. When the sprite/stage is clicked, the script underneath these hat blocks will run.
when stage clicked :: hat events
when this sprite clicked :: hat events
When This Sprite Clicked
- Main article: When This Sprite Clicked (block)
A hat block that senses clicks is the When This Sprite Clicked block. When the sprite in question is clicked, the script underneath this block will run. If the sprite is hidden (when the Hide block is in use), the code will not run. The code will also not run when transparent areas of the sprite is clicked.
when this sprite clicked :: hat events
When Stage Clicked
- Main article: When Stage Clicked (block)
Another hat block that senses clicks is the When Stage Clicked block. Like the When This Sprite Clicked block, any code under this block will run when the stage (or backdrop, in other words) is clicked. It will not run when a sprite is clicked. This block is only shown in the Block Palette if the stage is selected.
when stage clicked :: hat events
Other Scripts
To sense if a sprite is being clicked in the middle of a script, use this:
if <<mouse down?> and <touching [mouse-pointer v]>> then ...
These scripts need to be in a loop to check for clicking more than once. |
This script only runs when the mouse is clicked and not when it is held down.
wait until <not <mouse down?>> wait until <mouse down?> if <touching [mouse-pointer v]> then ...