Skip navigation
Currently Being Moderated

For loops in functions?

May 25, 2012 11:06 AM

I am using the following code in a program:

It is a simple conditional statement

in which a button moves to a certain group of frames when clicked depending on its current frame. This is the code I'm using.

 

var group1:Array=["b1","b2","b3"];

var group2:Array=["b12","b22","b32"]

 

mainbutton.addEventListener(MouseEvent.CLICK, buttonflash)

function buttonFlash (e:MouseEvent)

{

     for (var gr1:uint=0; gr1<group1.length; group1++)

    {if

       (e.target.currentLabel == group1[gr1])

       {e.target.gotoAndStop(e.target.currentLabel +     "2")}

  

    else

       {e.target.gotoAndStop(e.target.currentLabel.slice(0,2))}  

 

 

  }

}

 

 

However, when I include the for loop in the function, it causes the function to run the number

of times of the variable. In testing, I switched out the else statement with a trace string and the string showed up in the output window three times.

When I place the for loop outside of the function, the output compiler says the variable is undefined. Any ideas as to what I'm doing wrong to have the loop run once?

 
Replies
  • Currently Being Moderated
    May 25, 2012 11:58 AM   in reply to witherton

    var group1:Array=["b1","b2","b3"];

    var group2:Array=["b12","b22","b32"]

     

    mainbutton.addEventListener(MouseEvent.CLICK, buttonflash)

    function buttonFlash (e:MouseEvent)

    {

         for (var gr1:uint=0; gr1<group1.length; group1++)

        {if

           (e.target.currentLabel == group1[gr1])

           {e.target.gotoAndStop(e.target.currentLabel +     "2");

           break;

           }

      

        else

           {e.target.gotoAndStop(e.target.currentLabel.slice(0,2))}  

     

     

      }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 12:45 PM   in reply to witherton

    Could you explain why you are trying to increment group1 instead of gr1?  group1 is not a numeric variable, so incrementing it by 1 is not going to get you very far and should be causing you errors.

     

    for (var gr1:uint=0; gr1<group1.length; group1++ )

     
    |
    Mark as:
  • Currently Being Moderated
    May 25, 2012 12:55 PM   in reply to witherton

    var group1:Array=["b1","b2","b3"];

    var group2:Array=["b12","b22","b32"]

     

    var isInGroup:Boolean=false;

     

    mainbutton.addEventListener(MouseEvent.CLICK, buttonflash)

    function buttonFlash (e:MouseEvent)

    {

         for (var gr1:int=0; gr1<group1.length; gr1++)  //Thank you Ned

        {if

           (e.target.currentLabel == group1[gr1])

           {e.target.gotoAndStop(e.target.currentLabel +     "2");

          isInGroup=true;      

           break;

           }

       } 

        if (!isInGroup)

         {e.target.gotoAndStop(e.target.currentLabel.slice(0,2))

          isInGroup=false;

         }  

     

     

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points