Moynzy
Moynzy

Reputation: 28

How can I stop an If statement from running

private function BatCondition():void 
    {

        //updateScoreText();
            if (nScore == 10)
            {
                trace('run once')


            }
    }

okay. so you see when the score is 10. it keeps tracing one once, x amount of times when i want it to run once, help.

p.s break does not work. C:\Users\Admin\Desktop\update2\src\Main.as(337): col: 22 Error: Target of break statement was not found.

Upvotes: 0

Views: 1593

Answers (3)

Creative Magic
Creative Magic

Reputation: 3141

Here's a way how break out of an if statement.

mainIf: if (some_condition)
{
   trace("Main condition")   

   innerIf: if (your_condition)
   {
      trace("inner if");
      break mainIf;

   }
   trace("this won't work")
}

If you want to break out of an if statement or a loop use the break keyword. If you have multiple if's and or loops you can add labels to them to target the if/loop you want to break out of.

Upvotes: 3

adamh
adamh

Reputation: 3292

How about a class level var to act as a flag.

eg,

private var score_at_ten_flag:Boolean = false;

private function BatCondition():void {
     if (nScore == 10 && !score_at_ten_flag) {
          score_at_ten_flag = true;
          trace('run once');
     }
}

Upvotes: 1

PATIL DADA
PATIL DADA

Reputation: 379

private function BatCondition():void {

    //updateScoreText();
        if (nScore == 10)
        {
            trace('run once')
            break;


        }
}

Upvotes: 1

Related Questions