Getting an on/off type of button to work properly

part12studiospart12studios Member Posts: 620
edited June 2012 in Working with GS (Mac)
Hi there,

So I have a game that has a series of on/off buttons. the issue i run into is that it goes "up" and "down" super fast.. i've tried using timers to delay the true/false conditions.. but this isn't really ideal.. what i find even then is that if someone presses down and holds the timer runs out and flips to the other conditions.

I've tried using rules with "touch released" which seems like it should work but seems to fall short for me.. ideally i'd just want it to work as you'd expect.. if you touch and hold.. it goes up.. and then when you let go and touch it again. it would then go down and stay down.

Any leads would be super appreciated! I've tried a number of different approaches and nothing really seems to be working. I am sure there is an elegant solution, but it's alluded me so far.

Thanks!
Caleb

Best Answer

  • IsabelleKIsabelleK Posts: 2,807
    Accepted Answer
    If touch is pressed
    Change attribute game.Button to (game.Button+1)%2

    game.Button has to be an Integer attribute.
    1 is on, 0 is off.

Answers

  • part12studiospart12studios Member Posts: 620
    edited June 2012
    Thanks for the quick response, but to better show the context of my question here is the game and the problem you'll see in practice, quiz and game..

    http://part12studios.com/promo/perkins/

    also what is the % sybmol in your solution above? I understand in some languages % means "mod" but i don't see a mod function in the list if insert functions. perhaps its called by another name in the list of functions?

    about your suggestion above how is that different than using a bool? i'm just wanting to understand why this would work rather than me just saying.. when touch is press..ed change attribute to true (on) false (off)?

    Thanks!
    Caleb

  • IsabelleKIsabelleK Member, Sous Chef Posts: 2,807
    (game.Button+1)%2 - this means that game.Button is change to 1 (if it's 0), and to 0 (if it's 1). It's the best way to create on/off buttons, because you won't have a situation when you have button off, and want to turn it on, and it turns on, and then off again.
  • MotherHooseMotherHoose Member Posts: 2,456
    @part12studios@TheMoonwalls … gives you the elegant solution for buttonState changes

    for delaying the boolean changes:

    on buttonActors:
    add actorAttribute: index type … myTiming

    Rule: when
    Event: touched is pressed
    -nestedRule: when
    -Attribute: self.myTiming = 0
    --Interpolate: self.myTiming To: 1 … Duration: 0.5 (or whatever delayTime you want)
    -Otherwise:
    --Interpolate: self.myTiming To: 0 … Duration: 0.5 (or whatever delayTime you want)

    Rule: when
    Attribute: self.myTiming = 1
    -changeAttribute: game.buttonBoolean To: true
    Otherwise:
    Rule: when
    Attribute: self.myTiming = 0
    -changeAttribute: game.buttonBoolean To: false

    play around with this to get it working for all your button and associated booleans
    main objective is to interpolate an attribute rather than use timerBehaviors

    if my script is confusing … I will do a demo for you

    image MH
  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    Yes % is the modulo function. Its just not in the dropdown list. Use this for all your toggles.
  • part12studiospart12studios Member Posts: 620
    edited June 2012
    Ok Mother's suggestion is different than the %. @Mother, if you hold down on the button wouldn't the button still flip once the interpolate finishes? like if i did a press an hold?..

    i understand what modulo does, but i don't understand how it gets around the instant true condition. I guess i just don't understand what is special about modulo.. i mean why does that make a difference than just saying game.button +1.. since its the same answer (1)?

    here is a link to my current setup. this version is lacking the artificial delay i had http://part12studios.com/Temp/ that worked, but it didn't help with heavy handed people only good for light/quick touches

    the issue for me is that for people who touch the screen.. say.. for 2 seconds.. it need to flip and stay until they release and THEN touch again.. i would think there is something that could be done with the release rule.. but my experiments got me nowhere.

    Thanks!
    Caleb
  • LumpAppsLumpApps Member Posts: 2,881
    You are making this way to complicated.
    Instead of a boolean, your Game.A1 should be an integer.
    That way it can switch from 0 to 1 instead of false or true.

    Just have one condition in your rule: when touch is pressed (or released whichever you prefer.)
    And in that rule do the modulus (change game.Button+1)%2 thing @themoonwalls suggested.
    What happens is when you pres it flips the switch on when it was of and of when it was on.

    Instead of modulus in cae of an on off switch I always use change 1-game.A1
    That way
    - when A1 is 1 it does 1 - 1 = 0
    - when A1 is 0 it does 1 - 0 = 1
    It has the same outcome as the modulus function used by themoonwalls but for me it is easier to remember.

    For the image you can use an image named button0 and button1.
    Then in the actor use a constrain behavior: constrain image to "button"..game.a1

    Hope this helps!
  • part12studiospart12studios Member Posts: 620
    ok i'll experiment with that. can anyone explain why using an integer is better? i mean isn't true false basically still just 0 and 1, just with less memory claimed for it?

    Thanks so much for this insight. I'm just really wanting to understand this for not just this example but to make better use of Mod in the future. I've never used it before just read about it.

    Thanks!
    Caleb
  • LumpAppsLumpApps Member Posts: 2,881
    I don't know if a boolean is claiming less memory compared to an integer. (I'm not an expert in these things).

    What I do know it takes less rules and and you can use simple math to change images and a button state which will help performance.
    Just look at your rules in the image you gave and compare it to what I do. It has 1 rule less, 1 condition less in the one that is left and one change image less compared to my method.
  • MotherHooseMotherHoose Member Posts: 2,456
    @part2studios … thanx for posting your code!

    the problem is the two 'touch is pressed' rules … firing one and then the other

    try one rule with nested Rule for the game.A1 condition …

    Rule: when
    Event: touch is pressed
    -nestedRule:
    -Attribute: game.A1 is false
    --your Group
    --changeAttribute: game.A1 To: true
    --Otherwise: (for nested rule)
    ---your Group
    ---changeAttribute: game.A1 To: false

    image MH
  • part12studiospart12studios Member Posts: 620
    edited June 2012
    Thanks @everyone for your advise. it was all very helpful! I understand the Modulo in this context now and yea that works great. I do see now that having two rules side by side was a bad idea.. it did take some time to unravel it all because of how things were setup.. but its good now.

    also yea the memory savings with bool is pretty insignificant really.. @LudwigHeijden but i figure simpler the better.. but yea using Modulo will really help in the future. I didn't know how i could do an on/off with a single rule without it.. which is why i did it the way i did.

    http://part12studios.com/promo/perkins/ although the HTML5 port does have some quirks in it that are not in the mobile..

    Just about ready to submit to app store.

    Thanks!
    Caleb
Sign In or Register to comment.