How to Make a Toggle

Материал из Поле цифровой дидактики

Шаблон:Expand Toggles are used to turn sound off and on, control a Sprite, and more. This tutorial will list two methods on how to make a toggle.

Method 1

This method uses the when this sprite clicked hat block to sense if the sprite is clicked. This one needs two Costumes, both of an on and off switch.

...
set [toggle v] to [off]

when this sprite clicked
if <(toggle) = [off]> then
set [toggle v] to [on]
switch costume to [on v]
else
set [toggle v] to [off]
switch costume to [off v]
end

Method 2

The same effect can be achieved with this script, using numerical values and logical operations to toggle between 0 (being off) and 1 (being on):

when gf clicked
set [toggle v] to [0]
when [space v] key pressed
set [toggle v] to <(toggle) = [0]>
if <(toggle) = [1]> then
. . . // toggle is on
else
. . . // toggle is off
end