In my code I have a few for loops in the main timeline that look like this
for (i = 0; i<2*speedY; i++)
{
code
}
I've done this exact syntax many times and there have been no issues, however when I did it it gave me the error that i is undefined. I then tried the same loops defining i as var i:int; however now it just gave me a namespace error. What am I doing wrong here?
If you only need the variable i
within the loop itself and don't need the variable beyond the scope of the loop, you can also declare it within the loop parameters:
for(var i:int = 0; i < 5; i++) {
trace(i);
}
In terms of performance it's a marginal difference, however it's generally a good practice to declare variables only within the scope in which they will be used.