QucikTip: Trace an Array With Correct Formatting

This entry is part of a series, Quick Tips»

If you’re using arrays to dynamically store information that you need to embed in your project later on, I got just the thing for you: A class that will keep the formatting of the array!

Usually it’s quite a pain to get data out from an array in flash – or at least you need to do some extra work!


I’m going to use this array as an example:

var testArray:Array = [1,2, "test", [3,4],  MovieClip];


If we trace that with flash built in command, we get this:

1,2,test,3,4,[class MovieClip]


That’s okay for getting an overview of what’s in the array. But notice that our nested array ( [3,4] ) doesn’t show up as nested anymore. That’s not a big problem in this case, as I knew it was going to do this. But if you’re debugging someone else’s code, you could be confused by the fact that there’s 6 elements in the array, but the testArray.length is 5!

If the content of the array was generated dynamically (fx a level editor), you would have to spend a lot of time formatting the array back into something readable by Flash.


Here’s the solution

I made a class that handles the formatting itself , and it’s really easy to use. Download it here.

To use it, call:

ArrayTracer.traceArray ( testArray ) ;

That will output:

[ 1, 2, "test", [ 3, 4 ], flash.display.MovieClip ]


If you don’t like to have fully qualified class names, you can avoid them by setting the second parameter (optional) to false:

ArrayTracer.traceArray ( testArray , false ) ;

That will output

[ 1, 2, "test", [ 3, 4 ], MovieClip ]


If you by some reason don’t want to use the built in trace function, you can just call ArrayTracer.getArrayString which has the same functionality, but will return a String!


Before I forgot it, you must import the class :

import com.rasmuswriedtlarsen.utils.ArrayTracer;

The class has special operations for Class, String and nested arrays – if you throw anything else at it, it will just call String() on that!


Once again, here’s the download link.

Posted on February 1st, 2011
Filed under ActionScript 3, Flash, Tutorial, Util |

Leave a Reply