954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Toggle objects on/off using AS3

Hi, I have two objects: One is a button (instance name button1) and a square (instance name square1), and I would like the square's visibility to be toggled on or off when the button is clicked. So far, my code can make the square disappear, but not reappear. Below is my current code:

square1.visible = true;
button1.addEventListener(MouseEvent.MOUSE_UP, squareVisible);
function squareVisible (e:MouseEvent):void {
	if (square1.visible = true){
		square1.visible = false;
	} else if (square1.visible = false){
		square1.visible = true;
	}
}


Thanks for your help! :)

csterling
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Try these code with some modifications in the condition

square1.visible = true;
button1.addEventListener(MouseEvent.MOUSE_UP, squareVisible);
function squareVisible (e:MouseEvent):void{
	if (square1.visible == true)
{		square1.visible = false;	} 
else if (square1.visible == false)
{		square1.visible = true;	}
}

QUOTE=csterling;1711095]Hi, I have two objects: One is a button (instance name button1) and a square (instance name square1), and I would like the square's visibility to be toggled on or off when the button is clicked. So far, my code can make the square disappear, but not reappear. Below is my current code:

square1.visible = true;
button1.addEventListener(MouseEvent.MOUSE_UP, squareVisible);
function squareVisible (e:MouseEvent):void {
	if (square1.visible = true){
		square1.visible = false;
	} else if (square1.visible = false){
		square1.visible = true;
	}
}


Thanks for your help! :)[/QUOTE]

misd_bunder
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Thanks for the help, but it still hasn't fixed my problem...The square disappears but when it is not visible it does not reappear when the button is clicked...

csterling
Newbie Poster
6 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 
I had tested that code. It still works. If you are not having the result , then make change in the function declaration as
function squareVisible (event:MouseEvent)
instead of
function squareVisible (e:MouseEvent)
and use a separate layer for actionscript3.0
misd_bunder
Newbie Poster
2 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: