Using the Trace() Command in AS3

By Blue_Chi | Flash CS4 | Beginner

This tutorial will teach you how to use one of the most basic, yet essential, debugging tools in Flash ActionScript: The trace() command. This command shows any value you pass to it in the output window when a Flash movie is tested in the Flash authoring tool. This tutorial will teach you how to use this command through a number of examples.

What is the trace() command used for?

The trace() command is used for testing the values of variables in the Flash authoring tool. It has no function in the regular browser-based and standalone Flash Players. In small and large projects, it is difficult to work blindly with variables and expect them to achieve their goals without having an actual numeric output indicating their value throughout the movie. The trace() command helps you test the value of any variable easily.

Basic Usage of the Trace() Command

The trace() command is used in conjunction with at least one value. For example, if you want to simply show a text value in the output window you could do that in the following way:

trace("Hello World");

You can test this code in Flash by creating a new FLA, right-clicking the only frame you have on the timeline, selecting Actions, and then pasting the code above in the Actions window. Test your code by going through Control>Test Movie to see the text "Hello World" shown in the output window.

Testing the Value of a Variable

If you would like to test the value of a variable you can simply pass the name of the variable to the trace() command this way:

var myName:String = "John";
trace (myName);
You can learn more about AS3 Variables by reviewing our tutorial on this topic.

Testing that code would output "John" in the output window.

It is also possible to output more than one value using the same trace() command by separating these values with a coma:

var myName:String = "John";
var myAge:String = "30";
trace (myName, myAge);

In more complex projects, you might want to place a number of trace() commands throughout the project and you want to know which value was for which variable. You can do this by adding a string to indicate the name of the variable along with your variable value. Here is an example:

var myName:String = "John";
var myAge:String = "30";
trace ("myName is "+ myName);
trace ("myAge is "+ myAge);

Testing this code would give you a more informative feedback regarding the content of each of your variables.

Testing Other Object Properties

Remember that the trace() command can be used to test any value and not just variables you created. For example, it could be used to retrieve the values of properties of objects you have on stage.

For example, if you have a movie clip placed on stage that is named myBox_mc, you can use the trace command to output information about that movie clip this way:

trace ("The X position of myBox_mc is "+ myBox_mc.x);
trace ("The Y position of myBox_mc is "+ myBox_mc.y);

It is also possible to use the trace() command to retrieve all values inside an array or an XML list by using a loop. In the example below, we will retrieve the elements of our array one by one:

var myApps:Array = ["Flash", "Fireworks", "Dreamweaver"];

for (var i:Number = 0; i<myApps.length; i++){
trace(i+ " = " + myApps[i]);
}
You can learn more about Arrays and Loops by reviewing our tutorials on these topics.

Summary

The trace() command is one of the most helpful tools in ActionScript. If you ever face an error or something in your movie does not work as expected, use the trace() command to test that your objects and variables have the values that you expect them to.

This concludes our tutorial, I hope that you learnt something new from it. If you have any comments or questions feel free to make a post at the Republic of Code Forum.

- End of Tutorial