actionscript 3.0 for journalists

The Trace Command

In this section, we will put into practice some of the concepts covered in the syntax and variables section. This section is a follow-along instruction. If you have your own copy of Flash CS3 or CS4, open it now and choose ActionScript 3.0 from the new document menu.

Choose ActionScript 3.0 from the new document menu

Once inside the Flash program, select the Window menu at the top of the screen, and choose Actions. This will open the ActionScript window.

The Actions option is in the window pull down menu.

When writing ActionScript, it does not matter what line number you put your code on. The line number are for your reference only. It is generally acceptable to spread the code out by inserting some carriage returns.

The trace command

Trace is a command in ActionScript that will become invaluable throughout the process of learning to code. Trace does only one thing: It sends a message (also known as an expression) to the output window when you test your program. These messages allow you to know what is going on with your program, and are helpful in the debugging process. Trace commands can only be seen during the authoring phase. This means that if you publish a Flash project to the Web with trace commands, the public will not see these messages.

Let's try it out. In your ActionScript window type trace("Hello World!");

tracing the message hello world

Now test your program. Mac users can do this by holding down the Command key on their keyboard, and pressing Return. PC users should hold down the Ctrl key and press Return. Or, find the test movie option under the control menu at the top of the screen.

Once you do this, you will notice your project simply shows a blank stage. That's because we haven't actually produced a project yet. However, behind our movie, we will see the message we typed in the output window.

The Flash output window showing hello world

Pretty simple so far. It's important to notice that the trace command only caused our exact message to display because we put it in quotes. Remember, when quotes surround text, it becomes a string of text.

Tracing variables

Let's try to output a variable. Close the project preview window to get back to your Actions window. Then type the following code:

var myName:String = "Jeremy"; 
trace(myName);

As you type in this code, you may notice that when you type the colon character, a drop down menu appears.

Code hints in the Actions window of Flash

This is called a code hint and it's optional. It simply a way for Flash to give you a list of the available options. In this case, Flash is giving you a list of built-in datatypes and classes. You can either scroll down and select String or begin typing the word. As you type the letters, the selection will drill down and eventually highlight the option you want. When it finds it, you can press the return key and it will select that option.

Trace outputing the myname variable

When you test this program, you will notice that the output window does not display the actual "myName" text, but the value of the myName variable we created. Try going back into the code and assigning your own name to the myName variable and test it again.

Concatenation of strings and variables

In programming languages, the process of concatenation means linking several strings of text together to form one sentence. In ActionScript, we use the ( + ) symbol to link together strings of text. Type the following into your Actions window and test your program:

trace("Jeremy" + " " + "Rue");

Concatenating two strings

Notice how Flash will connect the three strings together including the second string, which is nothing more than a space to separate the words. Let's take this a step further:

var firstName:String = "Jeremy"; 
var lastName:String = "Rue"; 
trace(firstName + " " + lastName);

Concatenating two strings

As we can see in this last example, we are using variables to store information and send that information to the output window. Event though these examples might not seem like they have a lot of applicability just yet, it sets a foundation of how everything in ActionScript works.

This last example shows how some code might appear in a video game program:

var firstName:String = "Jeremy"; 
var score:Number = 320; 
trace("Congratulations " + firstName + ", you earned " + score + " points");

Concatenating strings with numbers

Writing code like this makes it reusable. All we have to do is change the firstName and score variables and the message will adapt to many situations.