best way to make a on/off button?

guillefaceguilleface Member Posts: 1,014
edited May 2012 in Working with GS (Mac)
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

  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    make your power attribute a integer attribute
    then do when touch is pressed
    change attribute power to (power+1)%2
  • tenrdrmertenrdrmer Member, Sous Chef, Senior Sous-Chef Posts: 9,934
    Instead of true false use an integer attribute

    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
  • guillefaceguilleface Member Posts: 1,014
    oh my god all this time adding many attributes to make it work and it was that easy, i should asked early lol, thanks a lot you have no idea how much time i will save from now on ,but i still don't get the formula, if power is =1, then when press i am changing 1 to (1+1)%2 = 2%2
    now i have no idea how i get 0 from 2%2?
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    @guilleface The % symbol is a modulo operation (http://en.wikipedia.org/wiki/Modulo_operation) that returns the remainder when dividing two numbers. So for example, 6%4 means that when you divide 6 by 4, you get 1 with a remainder of 2. So 6%4=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

  • tenrdrmertenrdrmer Member, Sous Chef, Senior Sous-Chef Posts: 9,934
    You can also use this for a series of numbers.

    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
  • MarkOnTheIronMarkOnTheIron Member Posts: 1,447
    I love the % operation. It's perfect for loops.

    Since it always result in a positive whole number you can also use it on index attributes instead of integer ones.

  • guillefaceguilleface Member Posts: 1,014
    Wow using gamesalad for a year and didn't know this amazing rule, thanks for the great info guys.
  • AngryBoiAngryBoi Member Posts: 586

    my boy socks with the hookup

  • SocksSocks London, UK.Member Posts: 12,822

    @MoneyBags said:
    my boy socks with the hookup

    What !? :)

  • SocksSocks London, UK.Member Posts: 12,822

    @MarkOnTheIron said:
    I love the % operation. It's perfect for loops.

    Since it always result in a positive whole number you can also use it on index attributes instead of integer ones.

    %-100 . . . . !

    You can have negative values.

Sign In or Register to comment.