Skip to main content
New Participant
February 5, 2020
Question

"if then" help request

  • February 5, 2020
  • 1 reply
  • 272 views

 

first and most important many thanks in advance from a very novice user

i have a very simple HTML5 doc that i could use some assistance in the script-

the essentials are this:

i have 6 unique movie clips (just a still graphic) where only 2 will be visable at a time

6 buttons that will select which 2 objects will be visable-

i am looking for a "if/then" script for a new button to swap visable objects just not sure the correct syntax... something like this but with all possibilities?

--

if

this.grnSqmc.visible = true;
this.bluSqmc.visible = false;
this.redSqmc.visible = false;
this.grnbCrclmc.visible = false;
this.bluCrclmc.visible = true;
this.redCrclmc.visible = false;
 
then
this.grnSqmc.visible = false;
this.bluSqmc.visible = true;
this.redSqmc.visible = false;
this.grnbCrclmc.visible = true;
this.bluCrclmc.visible = false;
this.redCrclmc.visible = false;

 

--

 

 

 

here is what i have that works great for me so far:

 
***
this.grnSqmc.visible = true;
this.bluSqmc.visible = false;
this.redSqmc.visible = false;
this.grnbCrclmc.visible = false;
this.bluCrclmc.visible = true;
this.redCrclmc.visible = false;
 
this.btnshwGrnSq.addEventListener("click", fl_ClickToHide.bind(this));
 
function fl_ClickToHide()
{
this.grnSqmc.visible = true;
this.bluSqmc.visible = false;
this.redSqmc.visible = false;
 
}
 
this.btnshwBluSq.addEventListener("click", fl_ClickToHide_2.bind(this));
 
function fl_ClickToHide_2()
{
this.grnSqmc.visible = false;
this.bluSqmc.visible = true;
this.redSqmc.visible = false;
 
}
 
this.btnshwRedSq.addEventListener("click", fl_ClickToHide_3.bind(this));
 
function fl_ClickToHide_3()
{
this.grnSqmc.visible = false;
this.bluSqmc.visible = false;
this.redSqmc.visible = true;
}
 
this.btnshwGreenCrcl.addEventListener("click", fl_ClickToHide_4.bind(this));
 
function fl_ClickToHide_4()
{
this.grnbCrclmc.visible = true;
this.bluCrclmc.visible = false;
this.redCrclmc.visible = false;
}
 
 
this.btnshwBlueCrcl.addEventListener("click", fl_ClickToHide_5.bind(this));
 
function fl_ClickToHide_5()
{
this.grnbCrclmc.visible = false;
this.bluCrclmc.visible = true;
this.redCrclmc.visible = false;
}
 
this.btnshwRedCrcl.addEventListener("click", fl_ClickToHide_6.bind(this));
 
function fl_ClickToHide_6()
{
this.grnbCrclmc.visible = false;
this.bluCrclmc.visible = false;
this.redCrclmc.visible = true;
}
 
***
 

again thanks in advance!

 

 

This topic has been closed for replies.

1 reply

JoãoCésar17023019
Community Expert
February 6, 2020

Hi.

 

You're going to need the logical NOT operator.

 

For example:

function toggleVisibility(e)
{
	this.grnSqmc.visible = !this.grnSqmc.visible;
	this.bluSqmc.visible = !this.bluSqmc.visible;
	this.redSqmc.visible = !this.redSqmc.visible;
	this.grnbCrclmc.visible = !this.grnbCrclmc.visible;
	this.bluCrclmc.visible = !this.bluCrclmc.visible;
	this.redCrclmc.visible = !this.redCrclmc.visible;
}

this.stop();
this.yourToggleButton.addEventListener("click", toggleVisibility.bind(this));

 

So

- if something is true and you set it to be NOT true, it will be false;

- if something is false and you set it to be NOT false, it will be true.

 

I hope it makes sense.

 

Regards,

JC

eng2020Author
New Participant
February 6, 2020

thank you JC 

 

that worked! but the results are pretty exact in that the 2 NOT visable squares and 2 NOT visable circles become visable- 

what i am hoping for is if a green square and red circle is visable, the toggle will make green circle and red square visable... while leaving the 2 blue object not visable at all since they were not initially selected -

that is where my "if/then" approach came from... "if blue sqaure and red circle are visable then toggle to red square and blue circle ignor green square green circle"

is this possible?

again, many thanks in advance!!

 

 

 

 

 

Brainiac
February 6, 2020

First, as you see many times in your own code, "visible",  not "visable".

 

Second, all you need is the && logical AND operator.

 

if (condition1 && condition2) {

     do something

}

 

You can have as many conditions as you want. You could even do something crazy like:

if (this.grnSqmc.visible && !this.bluSqmc.visible && !this.redSqmc.visible && !this.grnbCrclmc.visible && this.bluCrclmc.visible && !this.redCrclmc.visible) {
	this.grnSqmc.visible = false;
	this.bluSqmc.visible = true;
	this.redSqmc.visible = false;
	this.grnbCrclmc.visible = true;
	this.bluCrclmc.visible = false;
	this.redCrclmc.visible = false;
}

The ! prefix mean NOT. It flips Boolean values from True to False and vice versa. "If" conditions like this only execute if everything within them evaluates to true, so that's why you flip values that you want to be false to true for testing.