ActionScript 3 Error: 1038: Target of break statement was not found.
ActionScript 3 Error #1038 Description:
A break statement is used only with loops. They will not work in if statements. This is directly from the documentation:
Appears within a loop (for, for..in, for each..in, do..while, or while) or within a block of statements associated with a particular case in a switch statement. When used in a loop, the break statement instructs Flash to skip the rest of the loop body, stop the looping action, and execute the statement following the loop statement. When used in a switch, the break statement instructs Flash to skip the rest of the statements in that case block and jump to the first statement that follows the enclosing switch statement.
In nested loops, break only skips the rest of the immediate loop and does not break out of the entire series of nested loops. To break out of an entire series of nested loops, use label or try..catch..finally.
The break statement can have an optional label that must match an outer labeled statement. Use of a label that does not match the label of an outer statement is a syntax error. Labeled break statements can be used to break out of multiple levels of nested loop statements, switch statements, or block statements. For an example, see the entry for the label statement.
Pay attention to the last two paragraphs if you want to break all the way out of a function/method rather than just the loop that the break statement is inside then you need to use label. A very handy but seldom used method
Flex / Flash Error #1038 Fix:
Make sure that your break statement is within a valid loop such as for, while, switch statements, etc… Another alternative is to use a return statement without any parameters. Warning: Using return will break out of the function entirely and will not execute any code after the if statement. return is typically used as the last line of code in a function.
Bad Code:
if (totalScore < 10)
{
finalMsg = “Sorry!<br /> You need more practice”;
break;
}
else if (totalScore < 20)
{
finalMsg = “Congratulations!<br /> You kept yourself and others safe”;
break;
}
Good Code:
if (totalScore < 10)
{
finalMsg = “Sorry!<br /> You need more practice”;
return;
}
else if (totalScore < 20)
{
finalMsg = “Congratulations!<br /> You kept yourself and others safe”;
//break can be removed to get rid of the error;
}
This should help you resolve Flex / Flash Warning #1038
Thanks and as always Happy Flashing
Curtis J. Morley