AS3 Tips: A better trace and for loops

It’s been some time since my last post. My apologies – I have been sucked into the Twitter vortex which I find fascinating to be honest. I mean, whodda thunk that 140 characters could reveal so many brand insights! But that’s a different post altogether.

Today I’m sharing some tips that I learned from Todd Dominey’s Twitter feed (@tdominey). For those of you who don’t know Todd Dominey, you’ll certainly know his work: SlideShowPro – the completely ubiquitous (and awesome) Flash slideshow gallery.

Both tips are AS3 specific, and oh so helpful.

Tip 1: A simpler trace function
I can’t tell you how many times I have had to type long, convoluted trace statements to see if whatever I’m working on is doing what I think it should be doing. Usually those trace statements look something like this:
trace ("array item " + i + ": " + my_arr[i]);

which would turn out something like this:

array item 0: blue
array item 1: red
array item 2: green

You’ll notice a lot of plus signs and quotes and stuff. Well, instead, try replacing the + signs with commas:

trace ("array item ", i, ": ", my_arr[i]);

Though it’s nothing truly magnificent, I find typing commas vs. + signs is much faster and easier to understand.

Tip 2: A simple for loop for arrays
This one is also quite simple in its function, but saves a lot of typing. Instead of looping through an array with a for statement, like so:

for (var i:int = 0; i < my_arr.length; i++) { myFunction(which:int); }

you can use this:


so long as myFunction accepts the right params:

function myFunction(element:*, index:int, arr:Array) {
trace (element, ": " , index);

Pretty nifty eh? Yeah, I thought so too. Thanks @tdominey!

1 thought on “AS3 Tips: A better trace and for loops

Comments are closed.