Discuss Scratch

SeanRed111
Scratcher
7 posts

How do i make an on and off switch

i would like to make an on and off switch that when clicked changes a variable and when clicked another time changes it back.
mstone326
Scratcher
1000+ posts

How do i make an on and off switch

You have the right idea. Use a variable to track the current state. Here is an example.

whenthisspriteclickediflight=onthensetlighttooffturnlightoffscriptselseiflight=offthensetlighttoonturnlightonscripts

Last edited by mstone326 (May 25, 2018 23:11:32)

SeanRed111
Scratcher
7 posts

How do i make an on and off switch

mstone326 wrote:

You have the right idea. Use a variable to track the current state. Here is an example.

whenthisspriteclickediflight=onthensetlighttooffturnlightoffscriptselseiflight=offthensetlighttoonturnlightonscripts

how do i get the red blocks?
mstone326
Scratcher
1000+ posts

How do i make an on and off switch

The red blocks are just for the forum. I put them where you would put what should happen when the light goes on and when the light goes off. Maybe you have a costume with the light on, and a costume with a light off. See if this project helps

https://scratch.mit.edu/projects/194036878/#editor



Last edited by mstone326 (May 25, 2018 23:38:12)

SeanRed111
Scratcher
7 posts

How do i make an on and off switch

mstone326 wrote:

The red blocks are just for the forum. I put them where you would put what should happen when the light goes on and when the light goes off. Maybe you have a costume with the light on, and a costume with a light off. See if this project helps

https://scratch.mit.edu/projects/194036878/#editor




i tired the script but when i click the sprite it changes the variable but will not change it back

where you put the red blocks i set it to change a variable to 0 then 1 but when it changes to 1 it will not go back if i click it again
mstone326
Scratcher
1000+ posts

How do i make an on and off switch

Did you look at the project I shared?

If not, look at that. Also, share the project you need help with and we can look and help you along.
SeanRed111
Scratcher
7 posts

How do i make an on and off switch

thank you it worked, what went wrong is that i used the wrong variable in the script. well thank you
mstone326
Scratcher
1000+ posts

How do i make an on and off switch

Welcome to Scratch. Best of luck on your project!
OTSFan203
Scratcher
500+ posts

How do i make an on and off switch

make a sprite for switch, and write this script:
whenthisspriteclickediflight=offthensetlighttoonwhenthisspriteclickediflight=onthensetlighttooff

Last edited by OTSFan203 (May 26, 2018 14:14:02)

deck26
Scratcher
1000+ posts

How do i make an on and off switch

OTSFan203 wrote:

make a sprite for switch, and write this script:
whenthisspriteclickediflight=offthensetlighttoonwhenthisspriteclickediflight=onthensetlighttooff
Both scripts will run when the sprite is clicked but even if you're controlling the order you'll have a problem. Imagine they run in the order shown and light=off. It'll gets switched to on in the first script and then off again in the second. If it's on it'll get switched off by the second.

Change the order ofthe scripts and you get the opposite problem - it will always end up on.
ScratchUser1008666
Scratcher
3 posts

How do i make an on and off switch

when green flag clicked

setvaribletoonsetvaribletooff
MeIzAwezomeDede
Scratcher
1000+ posts

How do i make an on and off switch

ScratchUser1008666 wrote:

when green flag clicked
setvaribletoonsetvaribletooff
That wouldn't really work… and this post in 2 years old.
You're a new user so you're OK, but please pay attention to when things have been published next time
SMSK
Scratcher
1 post

How do i make an on and off switch

This was helpful, thanks!
Harakou
Scratcher
1000+ posts

How do i make an on and off switch

SMSK wrote:

This was helpful, thanks!
That's great! However, please don't post on old questions like this - it bumps them back to the top of the index, which is confusing for others.

Powered by DjangoBB