When Backdrop Switches to () | |
when backdrop switches to [ v] | |
Category | Event |
Type | Hat |
Introduced in | 2.0 |
The When Backdrop Switches to () block is an Event block and a Hat block. Scripts that wear this block will be triggered once the specified backdrop has been switched to on the stage.
Example Uses
- Starting a level
switch backdrop to (level1 v) // in stage
when backdrop switches to [level1 v] //in a sprite go to x: (0) y: (0)
- A game over message
switch backdrop to (game over v) // in stage
when backdrop switches to [game over v] // in a sprite hide
- Switching among menus
switch backdrop to (menu 3 v) // in stage
when backdrop switches to [menu 3 v] //in a sprite go to [front v] layer //so the menu item displays in the front show //this opens up the menu
Recursion
This block can use recursion to continuously run scripts. An example is as follows:
when backdrop switches to [snow storm v] //a background switch must first be triggered to begin the recursion . . . switch backdrop to (sunny day v) //which will trigger the next script when backdrop switches to [sunny day v] . . . switch backdrop to (snow storm v) //starts the cycle over
Workarounds
When the backdrop changes, sprites with the When Backdrop Switches to () block, and the attribute as the backdrop being changed to (meaning the selected backdrop within the block is what is being switched to), the sprites will react and run their scripts beginning with this hat block. Broadcasts can be used to replicate this situation, though there may be a miniscule delay due to an extra block being added. The following is a workaround, often used in Scratch 1.x:
switch backdrop to (valley v) broadcast (valley v) when I receive [valley v] . . .
Another workaround is:
when gf clicked wait until <not<(backdrop [name v]) = [backdrop1]>> forever if <(backdrop [name v]) = [backdrop1]> then ... wait until <not<(backdrop [name v]) = [backdrop1]>> end