I see my co-workers use this horrid syntax a lot:
var mc1: MovieClip;
var mc2: MovieClip;
var mc3: MovieClip;
var mc4: MovieClip;
var mc5: MovieClip;
for (var i:int = 1; i <= 5; i++) {
addChild(this["mc" + i]); // UURRGGHHH
TweenLite.to(this["mc"+i], 1, {alpha: 0}); // FNNNGGGGGHHHH
}
Because I'm a bossy sod, I'm putting together a list of reasons why they should use arrays for iteration, not nasty square-bracket syntax. I know it's wrong to use that syntax but I can't think of enough compelling reasons why they should abandon it.
Hit me with some facts, please.
for
loop when you add another item.Type-safety.
The square-bracket syntax will not result in the a decent type so that the compiler can check for problems at compile time. I show no mercy to people who come to me with runtime exceptions caused by type problems.
Seriously, the type system in AS3 is there for a reason. Use it. Don't figure out ways to work around it. It prevents problems.
I'll argue with you here, and say that you shouldn't be using arrays, but vectors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With