Author: Matt_Slaybaugh
Date: February 15, 2007
Level: Beginner
Some say that the art of programming is 90% debugging, that is, you spend most of your time finding and fixing problems in the code.
One of the most valuable ways to find problems in code is by printing the values of variables, that way you can confirm whether the code is doing what you want it to do.
Actionscript provides an easy way to print the values of variables, the Trace command.
This exercise will show you how to use the Trace command.
- Basic familiarity with Actionscript
- Flash, any version
This software is licensed to the public under the CC-GNU GPL.
Basic familiarity with Actionscript
Open goRabbit.fla.
Click on frame 40 of the layer named 'Layer 1'.
In the Actions panel, scroll down to line 128.
The code in this function tells the rabbit what to do in the game, and is run every frame. When adjusting the variables in the game, you may need to know wexactly where the rabbit is at any moment. In line 129, add the following code:
trace(rabbit._x);
so that the code looks like figure 1
Like comments, trace commands are ignored when the Flash movie is compiled.
Test the movie (Ctrl+Enter). The Output panel will open and display the 'x' coordinate of the rabbit for every frame.
However, when you have multiple traces it can get confusing to know which variables are being traced.
Exit the movie preview and edit line 129 to look like:
trace("Rabbit x: " + rabbit._x);
Save and test the movie. In the Output panel you should clearly see which lines show the rabit's x-ccordinate. In trace commands, text in quotes is displayed eactly as you type it, while variables not in quotes have their values displayed. Use the plus sign (+) to connect literal text with variables.
You can also trace multiple variables in a single line. Exit the movie preview and edit line 129 to look like:
trace("Rabbit x: " + rabbit._x + ", Rabbit y: " + rabbit._y);
Save the file and test the movie. You should see both the rabbit's x-coordinate and y-coordinate clearly displayed in the output panel.
This game uses arrays and even an array of arrays. How would you trace an array to easily see all the values?
