Skip to main content
October 13, 2011
Answered

button to add and remove a movieclip

  • October 13, 2011
  • 1 reply
  • 509 views

I'm trying to make a button that will add and remove a movieclip. For some reason it wont remove the movieclip when it's clicked on and I can't seem to figure out what I did wrong. Below is the code I'm using. Even if I remove the else part from the function it wont remove the movieclip when the button is clicked on.

var instructions = new instructionsFirewall();

var instructionsOut:Boolean=false;

instructionButton.addEventListener(MouseEvent.CLICK, instructionsClick);

addChild(instructions);

instructions.x = 48;

instructions.y = 99;

function instructionsClick(MouseEvent):void {

          if (instructionsOut=false){

                    removeChild(instructions)

                    instructionsOut=true

          }

          else{

                    addChild(instructions);

                    instructions.x = 48;

                    instructions.y = 99;

                    instructionsOut=false

          }

}

This topic has been closed for replies.
Correct answer kglad

= is the assignment operator.  in your if-statement you're not testing for equality, you're assigning instructionsOut to be false.  use:

.

var instructions = new instructionsFirewall();

var instructionsOut:Boolean=false;

instructionButton.addEventListener(MouseEvent.CLICK, instructionsClick);

addChild(instructions);

instructions.x = 48;

instructions.y = 99;

function instructionsClick(MouseEvent):void {

          if (!instructionsOut){

                    removeChild(instructions)

          }

          else{

                    addChild(instructions);

                    instructions.x = 48;

                    instructions.y = 99;

          }

instructionsOut=!instructionsOut;

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 13, 2011

= is the assignment operator.  in your if-statement you're not testing for equality, you're assigning instructionsOut to be false.  use:

.

var instructions = new instructionsFirewall();

var instructionsOut:Boolean=false;

instructionButton.addEventListener(MouseEvent.CLICK, instructionsClick);

addChild(instructions);

instructions.x = 48;

instructions.y = 99;

function instructionsClick(MouseEvent):void {

          if (!instructionsOut){

                    removeChild(instructions)

          }

          else{

                    addChild(instructions);

                    instructions.x = 48;

                    instructions.y = 99;

          }

instructionsOut=!instructionsOut;

}

October 13, 2011

Aw bugger, I keep forgetting about that. Yes, normally I'd use ==, however your way seems much better. Thank you.

kglad
Community Expert
Community Expert
October 13, 2011

yes, with booleans, there's no need to use ==.  with everything else, use == to test.