best way to make a on/off button?
guilleface
Member Posts: 1,014
hi, i always run into this problem, ok so i have a actor, when "power" is true display green image, when "power" is off display red image.. now on my actor i have, when touch is pressed and "power" is false, change "power" to true, and when touch is pressed and "power" is true, change "power" to false, now this sound to me a good logic, but the thing is , when pressing and changing power to on, then the other rule takes effect immediately, changing power to off, i try using when releassed too ,many ways but is not working.
Answers
then do when touch is pressed
change attribute power to (power+1)%2
and then make all your true rules based off when the attribute is 1 and false is 0
Now in your button all you need is
When touch is pressed
--Change game.buttonOnOff to (game.buttonOnOff + 1)%2
That will switch the attribute between 1 and 0 constantly
now i have no idea how i get 0 from 2%2?
The reason it works well for switches in GameSalad is because if power is 0 (off), then (0+1)%2=1 (on), and if power is 1 (on), then (1+1)%2=0 (off). So each time you add one to power, the modulo toggles the value between 0 and 1.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Lets say you had 4 separate states of an actor you wanted to cycle through. 0-3
Using (game.attribute+1)%4 it cycle through 0, 1, 2, 3, 0, 1 , 2, 3, Etc…for each touch
Since it always result in a positive whole number you can also use it on index attributes instead of integer ones.
my boy socks with the hookup
What !?
%-100 . . . . !
You can have negative values.