How do I make a button that turns on when pressed then off when pressed again?

I am pretty new to this kind of stuff. I know how to make a normal button, but how do I make a button that turns on when pressed then off when pressed again?

Comments

  • DaGreatnessDaGreatness Member Posts: 82
    You will need to have 2 different images, so you can say when you put it in a rule

    when touch is pressed
    do
    change image to ( whatever is your turned on image)
    else
    change image to (the one that was already displaying when it was not pressed)

  • DinoBitDinoBit Member Posts: 23
    I think you misunderstand my question. I'd like it to turn on a bolean attribute when pressed, then turn off a bolean attribute when pressed again. how do i do that?
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited November 2013
    You can use a boolean but it works much better to use an integer with possible values of 0 (false) and 1 (true).

    With a boolean, there can be a problem with the rule running in a loop (if touch is pressed and true, change to false; if touch is pressed and false, change to true; BUT uh oh, when it changes to false and I'm still touching the actor, it then changes immediately to true... argh!)

    When touch is pressed
         Change attribute game.anInteger to 1-game.anInteger


    So if game.anInteger is 0, it will change to 1. If game.anInteger is 1, it will change to 0.

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • DinoBitDinoBit Member Posts: 23
    thank you so much!
  • TomCoffeeTomCoffee Member, PRO Posts: 175
    @Tatiang - Holy moly... I've been using GameSalad for 3 years almost all my apps have buttons that go on/off with a single tap... I never, ever came up with your simple idea of avoiding the loop problem (usually fiddled with timers!)...

    Thank you!
  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited November 2013
    You can use a boolean but it works much better to use an integer with possible values of 0 (false) and 1 (true).

    With a boolean, there can be a problem with the rule running in a loop (if touch is pressed and true, change to false; if touch is pressed and false, change to true; BUT uh oh, when it changes to false and I'm still touching the actor, it then changes immediately to true... argh!)

    When touch is pressed
         Change attribute game.anInteger to 1-game.anInteger


    So if game.anInteger is 0, it will change to 1. If game.anInteger is 1, it will change to 0.
    So neat, @tatiang, I really like that too! :-)

    For the record, for new users, the loop method is watertight providing it's done properly using the Otherwise section (although not as elegant as your solution, @tatiang ! )

    Rule:When touch is pressed
    Rule ---nested--:When BoolLoop is false
    Change Attribute BoolLoop to true
    --do your thing
    Otherwise
    Change Attribute BoolLoop to false
    --do your other thing

    @TomCoffee Any type of loop is only "fiddled" with timers if you want a pause before the next loop iteration; or to to give a time limit to the loop, for instance – both valid uses of timers.

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    Good to know @gyroscope... somehow I missed that.

    And I can't take credit for the subtraction method. I always see @Socks post it. B-)

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Sign In or Register to comment.