Switching Between Screens

Материал из Поле цифровой дидактики
Описание Как переключаться между различными сценами в Scratch. Скрипт проверяет, совпадает ли переменная экрана с экраном для отображения спрайтов. Если да, то спрайты будут отображаться и делать все, что от них требуется. Если нет, то спрайт останется скрытым и не запустит блоки для того времени, когда он будет открыт.
Область знаний Информатика, Управление, Game design
Область использования (ISTE)
Возрастная категория 8


Поясняющее видео
Близкие рецепту понятия ветвление
Среды и средства для приготовления рецепта: Scratch, Snap!

This article provides a method for switching between screens.

Variables

First, create a variable called screen:

(screen)

This variable will store which backdrop is shown.

Scripts

Then, in every sprite that would transition, put this:

when green flag clicked
... //Code before loop
forever
if <(screen) = [backdrop]> then //checks if the screen variable is the same as the wanted screen for the sprites to be shown
show //If true, sprite is shown
... //Code for when it is shown
else
hide //If not true, sprite is hidden
... //Code for when it is hidden.
end
end

In short, the script checks whether the screen variable is the same as the screen for showing the sprites. If so, the sprites will show and do whatever is required. If not, it will stay hidden and run the blocks for when it is hidden.

Stage Transitioning

For the stage, put this:

when green flag clicked
... //whatever code is wanted before loop
forever
switch backdrop to (screen) //Switches the backdrop to the string stored in the variable
end

This script constantly switches the backdrop to the string stored in the variable (screen).

Music Transitioning

If music should change based on the screen, add the following script to the Stage (or the sprite playing the music if a sprite is playing the music):

when I receive [change music v]
stop all sounds // this method could also be used to stop the music
if <(screen) = [screen 1]> then
forever // so the music doesn't randomly stop
play sound (music 1 v) until done
end
end
if <(screen) = [screen 2]> then
forever
play sound (music 2 v) until done
end
end
...

Execute this broadcast every time the music needs to be changed.

Black Screen

To make a black screen, first create a variable called (next screen). Then, insert this into the black screen sprite:

when I receive [black screen v]
go to [front v] layer
set [ghost v] effect to (100) // invisible
show
repeat (20)
change [ghost v] effect by (-5) // fade into view
end
set [screen v] to [] // nothing
broadcast (change music v) // stop the music
wait (1) secs
set [screen v] to (next screen) // the screen to switch to
broadcast (change music v) // change the music to the screen
repeat (20)
change [ghost v] effect by (5) // fade out of view
end
hide

Use this to execute the black screen:

set [next screen v] to [wanted]
broadcast (black screen v)

See Also