# Simple If/Else with Button

**URL:** <https://forum.kirupa.com/t/simple-if-else-with-button/186333>\
**Category:** flash\
**Created:** [April 20, 2006, 4:06pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333 "2006-04-20T16:06:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkohne](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mkohne](https://forum.kirupa.com/u/mkohne)\
**Post date:** [April 20, 2006, 4:06pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/1 "2006-04-20T16:06:33Z")

</div>

Hello. This is probably an easy question. I have a button that turns a movieclip on and off. I want to be able to use the same button to turn this on/off. I figured you could use the if/else statements but can’t seem to figure it out. Basically, when the mc is on, the button should turn it off, and vice versa. I have been using an ugly work around for this and I’m just looking for a cleaner, more proper way.

Thanks.

---

<div class="post-metadata">

**Author:** ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)\
**Post date:** [April 20, 2006, 4:11pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/2 "2006-04-20T16:11:19Z")

</div>

do you mean make it visible/invisible? You probably want to create a variable that sets the state of the movieclip at the beginning (e.g. variable = on), then you can say if variable = on set visibility to false and variable = off, else if variable = off set visibility to true and variable = on.

:hr:

---

<div class="post-metadata">

**Author:** ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)\
**Post date:** [April 20, 2006, 4:17pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/3 "2006-04-20T16:17:01Z")

</div>

What lunatic said, but if you want it as simple as possible, exclude the variable and use [AS]myMc.visible = !myMc.visible;[/AS] :cowboy:

---

<div class="post-metadata">

**Author:** ![mkohne](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mkohne](https://forum.kirupa.com/u/mkohne)\
**Post date:** [April 20, 2006, 4:22pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/4 "2006-04-20T16:22:55Z")

</div>

Thanks for the quick reply. Toggling the visibility would be fine. Something is wrong with the code I’m using. I have the following:

```auto
myMc = "on";
if (myMc == "on") {
	myMc._visible = true;
} else if (myMc == "off") {
	myMc._visible = false;
}

myButton.onPress = function() {
	if (myMc == "on") {
		myMc = "off";
	} else {
		myMc = "on";
	}
};

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)\
**Post date:** [April 20, 2006, 4:26pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/5 "2006-04-20T16:26:02Z")

</div>

[AS]myButton.onPress = function() {  
\_parent.myMc.\_visible = !\_parent.myMc.\_visible;  
};[/AS]change \_parent to the appropriate scope.

---

<div class="post-metadata">

**Author:** ![mkohne](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@mkohne](https://forum.kirupa.com/u/mkohne)\
**Post date:** [April 20, 2006, 4:28pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/6 "2006-04-20T16:28:49Z")

</div>

That worked. Thanks for the help, this will save me lots of time.

---

<div class="post-metadata">

**Author:** ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)\
**Post date:** [April 20, 2006, 4:30pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/7 "2006-04-20T16:30:00Z")

</div>

No problem! :cowboy:

---

<div class="post-metadata">

**Author:** ![lunatic](https://avatars.discourse-cdn.com/v4/letter/l/73ab20/32.png) [@lunatic](https://forum.kirupa.com/u/lunatic)\
**Post date:** [April 20, 2006, 4:37pm UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/8 "2006-04-20T16:37:19Z")

</div>

nice bluenar, i’ve never seen that before 🙂

---

<div class="post-metadata">

**Author:** ![B\_L\_U\_E](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@B\_L\_U\_E](https://forum.kirupa.com/u/B_L_U_E)\
**Post date:** [April 21, 2006, 12:57am UTC](https://forum.kirupa.com/t/simple-if-else-with-button/186333/9 "2006-04-21T00:57:24Z")

</div>

> [@lunatic](#):
>
> nice bluenar, i’ve never seen that before 🙂

Aye, you can negate any boolean variable/property. 🙂 Really useful.
