When Backdrop Switches to () (block)

When Backdrop Switches to ()
2.0 When Backdrop Switches to ().png
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.

How to Write it in the Forums or Wiki

Using the scratchblocks plugin in forums and the wiki, the block can be recreated using the following code inside <scratchblocks> (wiki) and [scratchblocks] (forums) tags:

when backdrop switches to [backdrop name v]

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 //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 workabout is

when gf clicked
forever
if <(backdrop name)=[backdrop1]>  then
...
end

See Also