How to toggle a variable with a button

May 10, 2012

How do you flip-flop a variable from true to false when a user clicks a button? This is what I've used below, but it only changes the variable once and then does nothing. HELP!

28 Replies
Steve Flowers

No go. I think what's happening, the trigger chain executes everything in the chain in order. So it senses the opposite, changes it then senses the opposite and changes it again. Might try this based on a toggle state rather than a set of triggers and variables.

An object that isn't in a group automatically has a toggle behavior. Click once, selected. Click again, not selected. If you've already wired up your variables you should be able to drive your switch condition based on the state of the object.

saffir test

Yep, have to change the variable based on the state of the button. Seems convoluted. Wish there was a "stop processing further triggers" option on the object triggers actions. I've already submitted 2 feature requests, so I'll let that one go for now.

For those wondering about the answer, the picture below shows the triggers needed to have a button change the state of another object using a variable.

What you do is:

Object Triggers on Click: change the state of the object  based on the value of the variable.

Change the variable based on the state of the object.

Slide Triggers:

Change the state of the target object when the variable changes

Ackbasswards a little bit, but that's what you have to do, or maybe you don't? any suggestions?

Gerry Wasiluk

There's also a far less elegant way but may be easier for some folks to comprehend. 

Use two buttons instead.  Call them Button 1 and Button 2.  Button 2 is initially hidden.  Button 2 is on top of Button 1.

Then add triggers to do the following:

  • Button 1, when clicked, changes the variable and then shows Button B and then hides itself.
  • Button B, when clicked, changes the variable back and then shows Button 1 and then hides itself.

Again, not as elegant, but it works.

Tamara Muroiwa

Thanks all. Was rather stuck till I came across this. (Had an intro layer on my slide that played, then hid itself to play a video on the base layer. But the video wouldn't autoplay, so I had a variable, 'IntroLayerPlayed' set at false when the layer timeline started and switching to true when it ended, and a trigger to play the video when the variable changed. But as I could only set the trigger by 'variable changed', not 'variable = true', if I clicked away and back onto that slide, changing the variable to 'false' again would play the video with the layer still showing above it. I had no idea about toggling [hope this is in the tutorials!]). Now it all works perfectly!

Thank you!

André Rodrigues

Phil Mayor said:

Russ Still said:

How 'bout just toggling it:

Where did that come from, 9 months and many sleepless nights and I have never seen that one

I bet you photshopped that one

Best topic ever here. I laught a lot with this I spend almost one day looking for this option until find the NOT assignment :DAnd for me is normal use variable = !variable in AS3. I don't know why I take too much time to realize that on SL :Dhahahahahha really cool :D

This discussion is closed. You can start a new discussion or contact Articulate Support.