interpolate on and then off again using same key?
Hi there,
I want to set up a key so that when it is pressed something interpolates into view, but when the same key is pressed again, it interpolates away again. Then if it is pressed a third time it will interpolate back into view, and then away again on the fourth press, and so on... So, I want the alpha to continue to switch between 0 and 1 each time the button is pressed.
At the moment I have a rule set up so that the actor interpolates into view when the button is pressed, but I don't know how to interpolate it away again using the same key (I need to stress that is has to be the same key). Is this possible? Do I need to use an attribute to do this, and if so, how? Or a table? (I don't know much about tables at this stage).
PS. It doesn't have to be the interpolate behaviour necessarily, if it is easier to do it another way.
Thanks for your help, I'm sure this is a dumb question!
Comments
Here's a demo for you. Use the space key to fade in/out.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
You'll need an "integer" attribute
Let's name the attribute, "INTERP"
RULE
When touch is pressed ** Change attribute ** ( game.INTERP +1)%2
RULE
If game.INTERP =1 ** interpolate self.color.alpha to 1
If game.INTERP =0 ** interpolate self.color.alpha to 0
This will change the attribute from 1 to 2, then back to 1 etc. with every touch.
@Jesse75 I think I'm doing something wrong - how does the integer know that on the next press (every second press) it should go back to one rather than forward to three and onwards? I know you have a %2 written in there, which may have something to do with it, but perhaps I'm not writing it in to the expression window properly...
Just curious... did you try the demo I posted?
Using the mod() function (aka '%') is another way to go. Mod() returns the remainder of a division problem. So if you change attribute game.INTERP to (game.INTERP+1)%2, then if the attribute is 0, the result will be (0+1)%2=1; if the attribute is 1, the result will be (1+1)%2=0. So it switches the value back and forth between 0 and 1.
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User
Yes, what taitang said. This way is called mod which is short for modulo or modular. It's basically a wrap for your integer.
The only problem I could see is if you were to type in the "game.INTERP" manually. Start off by selecting the INTERP attribute with the dropdown menu. Next, add the rest of the items to the expression manually. Set the integer to "0" initially and when you press the key, it will change to "1". The next press will take it back to "0" and so forth. I haven't looked at taitang's demo above but he knows his stuff, so if you still can't get this, check out his demo for a sure fire way to get whet you need.
Thanks guys ( @jesse75 and @tatiang)! I did work out a strange work around before posting that reply which included turning the attribute back to 0 on the 3rd press or something messy like that, but it included having to have a separate integer for each actor that I want to interpolate, even though they're interpolating on the same keyboard key (or maybe that is the case no matter what)? I'll definitely play around with that mod function - didn't know what that was before!
I had to admit, I did download your demo @tatiang, but I haven't upgraded my creator in so long that it won't open (still on 11.1.11). I'm a bit weary of upgrading at the moment as this game is due in a week so if the upgrade changes anything about it I will be in a bit of strife. Though I've heard that some people are having problems with Yosemite, and I am still on Mavericks, so it might be ok.
The demo I made uses the newest version of GameSalad and I run it in Mavericks. If you don't want to upgrade yet, here are the rules:
New to GameSalad? (FAQs) | Tutorials | Templates | Greenleaf Games | Educator & Certified GameSalad User