Flash AS3 Tutorial – trace() Command

flash-logoThis tutorial will show you how you can use trace() command in AS3. The trace() is one of the very basic, yet essential, command for debugging your Flash file.

Why trace() is so useful?

In Flash console using trace() command you can debug flow of your code. It fires comments inside your output console every time the command executed. When you work with large file and many variables, its challenging to keep track of what value the variable will hold during the certain piece of your code. You can figure that out using trace() command very efficiently. It will allow you to pass variables as an argument as well.

Basic use of the trace() Command

trace(“Hello World”);

When you put the code above within your ActionScript, it will fire “Hello World” text within your output console when you manually run your flash file or project on your local machine.

How you can test value of your variables within your Flash file

You can simply pass variables within trace() as shown below.

Var sitename:String = “DGlobaltech.com”;

Also you can pass the variable as :

trace(“I am learning tutorial on “+sitename);

Your output in flash console will be :

I am learning tutorial on DGlobaltech.com

A bit complex trace() example using array

var myArray:Array = [“Flash”, “HTML5”, “Flex”];
for (var i:Number = 0; i<myArray.length; i++)
trace(i+ ” = ” + myArray[i]);

Above is the best example to monitor and debug the value for each loop of your array. It gives you the value hold by a variable within your array when it executes loop by loop. You will be seeing “i=Flash”, “i=HTML5” and “i=Flex” when the code executes.

Hope this helps. Let us know your comment or feedback below. We’re continuously trying to share knowledge.