Getting an on/off type of button to work properly
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
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
-
IsabelleK Posts: 2,807
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
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
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
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
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!
Lump Apps and My Assets
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
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.
Lump Apps and My Assets
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
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