This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-05-06 20:32:06

WoofCakes
Scratcher
Registered: 2012-04-02
Posts: 9

On and off

is there anyway to make a button toggle on and off without having to use different keys to do it? so say for example if i have a sound sprite and i want it so when i click it it turns on and off like a switch. i cant get this to work and im having to use 2 different keys to do it. ex M for music and then O to turn it off. so yeah. idk how to explain it much clearer than that so hopefully you understand what i mean.  smile  hope you guys can help me! thanks!


I told you about the stairs bro. I told you dog. :y

Offline

 

#2 2012-05-06 20:43:38

AgentRoop
Scratcher
Registered: 2012-02-11
Posts: 1000+

Re: On and off

try this:

when gf clicked
forever
  if < key [toggle v] pressed? > //make the toggle key any key you like
    if < (toggle) = [on] >
    set [toggle v] to [off] //if it's on, it will turn off
    else
  set [toggle v] to [on] // if it's not on, it will turn on
  end
  wait until < not < key [toggle v] pressed ? > > 
end
This should work.  tongue

Last edited by AgentRoop (2012-05-06 20:46:33)


La La
I wrote an album.

Offline

 

#3 2012-05-06 21:07:50

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: On and off

when key [m v] pressed
set [music v] to (((music)+(1)) mod (2)) // 1 for on, 0 for off
set volume to ((100)*(music)) %


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#4 2012-05-06 21:45:16

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: On and off

Both of these approaches would work, though MoreGamesNow's is more efficient.

Offline

 

#5 2012-05-06 22:28:09

ManaUser
Scratcher
Registered: 2009-03-11
Posts: 100+

Re: On and off

This is a bit cryptic:

when [sprite v] clicked
next costume
set volume to (((2) - (costume #)) * (100)) %
But it should work nicely if you give the sprite two costumes. That way you get a visual indication whether it's on or off for free.  smile


http://i.imgur.com/SPYSM.gif http://i.imgur.com/t9k1Z.gif http://i.imgur.com/OwYVa.gif http://i.imgur.com/0qlZq.gif

Offline

 

#6 2012-05-07 07:24:14

WoofCakes
Scratcher
Registered: 2012-04-02
Posts: 9

Re: On and off

thankyou for the advice! its really helped!  big_smile  yay


I told you about the stairs bro. I told you dog. :y

Offline

 

#7 2012-05-07 09:49:38

AgentRoop
Scratcher
Registered: 2012-02-11
Posts: 1000+

Re: On and off

amcerbu wrote:

Both of these approaches would work, though MoreGamesNow's is more efficient.

yeah... I am so UNEFFICENT!  wink


La La
I wrote an album.

Offline

 

#8 2012-05-22 09:30:00

selenamarte
Scratcher
Registered: 2012-04-21
Posts: 78

Re: On and off

when gf clicked
switch to costume[sound on]
set{on?} to (1)

when [Sprite1] clicked
next costume
set{on?} to < [-1] * (on?) >
if < (on?) = [1] >
  set volume to [100] %
end
if <on?> = [-1] >
  set volume to [0] %
end

when gf clicked
play sound [any music] until done


http://scratch.mit.edu/static/projects/selenamarte/2691436_sm.png

Offline

 

#9 2012-05-22 09:31:07

selenamarte
Scratcher
Registered: 2012-04-21
Posts: 78

Re: On and off

selenamarte wrote:

when gf clicked
switch to costume[sound on]
set{on?} to (1)

when [Sprite1] clicked
next costume
set{on?} to < [-1] * (on?) >
if < (on?) = [1] >
  set volume to [100] %
end
if <on?> = [-1] >
  set volume to [0] %
end

when gf clicked
play sound [any music] until done

when gf clicked
switch to costume [sound on]
set {on?} to (1)

when [Sprite1] clicked
next costume
set {on?} to < [-1] * (on?) >
if < (on?) = [1] >
  set volume to [100] %
end
if <on?> = [-1] >
  set volume to [0] %
end

when gf clicked
play sound [any music] until done
this one


http://scratch.mit.edu/static/projects/selenamarte/2691436_sm.png

Offline

 

#10 2012-05-22 09:32:37

selenamarte
Scratcher
Registered: 2012-04-21
Posts: 78

Re: On and off

the set on? to 1 is the variable.


http://scratch.mit.edu/static/projects/selenamarte/2691436_sm.png

Offline

 

Board footer