Skip to main content
Known Participant
March 13, 2012
Answered

Using One Series of Conditional Statements for Several Different Objects?

  • March 13, 2012
  • 2 replies
  • 554 views

-Let's say you have several doors on stage for a game.

-Each door has the ability to change color.

-When any two doors are the same color, a player can move between

those two doors.

To do this, I use code similar to the following:

doorThree.addEventListener(MouseEvent.CLICK,dooraction);

                function dooraction(event:MouseEvent){

    if ("Blue") {

              doorOne.gotoAndStop("Red");

              doorTwo.gotoAndStop("Blue");

              doorThree.gotoAndStop("Yellow");

              }

    else if ("Red"){

                  doorOne.gotoAndStop("Blue");

                      doorTwo.gotoAndStop("Yellow");

              doorThree.gotoAndStop("Red");

        }         

}

Is there a way to use this one series of If statements for all doors, or must I copy and paste

the same instructions for each door instance? In the game I'm planning, each door actually has more than two dozen states, so if I can use just one series of if statements, I'd like to do that.

This topic has been closed for replies.
Correct answer Ned Murphy

To answer your question regarding using "the same instructions"... if they are the same for all doors then you can assign that same function to all the doors...

doorOne.addEventListener(MouseEvent.CLICK,dooraction);

doorTwo.addEventListener(MouseEvent.CLICK,dooraction);

doorThree.addEventListener(MouseEvent.CLICK,dooraction);

2 replies

Ned Murphy
Ned MurphyCorrect answer
Legend
March 13, 2012

To answer your question regarding using "the same instructions"... if they are the same for all doors then you can assign that same function to all the doors...

doorOne.addEventListener(MouseEvent.CLICK,dooraction);

doorTwo.addEventListener(MouseEvent.CLICK,dooraction);

doorThree.addEventListener(MouseEvent.CLICK,dooraction);

withertonAuthor
Known Participant
March 13, 2012

Thanks, Ned.

"Blue" is actually the label of one of the frames of the door symbol. The code I posted actually seems to be working fine thus far, but it's really only a streamlined example that I don't plan on using in the finished product .  (I try to make my posts as succinct as I can and remove any extraneous info in an effort to make my questions as clear as possible.) 

Once again, thanks a lot! Really appreciate the info.

Ned Murphy
Legend
March 13, 2012

I thinkj I told you in the other posting that

    if("Some String"){ 

isn't going to do anything for you.  Before anyone can help you probably need to explain what you are intending in your if("Blue")... etc lines.