marks. AS3.0 has lots of built-in methods, such as trace, that you can use to do all kinds of interesting things, and you'll be looking at many of them in detail over the course of this book.
Directives end in a semicolon (;). The semicolon is a basic piece of punctuation that lets the program know that “The directive is finished! Do this work!” It's like a period at the end of a sentence. Whenever you see a semicolon at the end of a line of code, you know it's a directive. It's telling the program to perform a specific action. You used a semicolon earlier in the line of code that imported the Sprite from the library. That line of code was also a directive.
If you forget to add a semicolon at the end of a directive, Flash Builder will give you the benefit of the doubt and assume that you intended to add one. Your program will still run flawlessly without it. Thanks, Flash Builder! But in the interest of good programming style, you should always add a semicolon. If you go on to learn other programming languages that aren't as lenient (and most aren't), you'll have already developed an excellent habit.
Figure 1-28 illustrates how trace works to display text in the Console window when you run and debug the program, which you'll be doing in the steps ahead.
Figure 1-28. Use the trace method to display text in the Console window when you run and debug your program.
But don't go looking for that text in the Console widow yet! You won't see it until you debug the program. You're going to do that in the steps ahead.
Adding comments to your code
Let's add one more feature to your program before you see what it actually does: a comment . When you're writing a computer program, it is often useful to write a short note about what a particular section of code does. This is especially helpful if you've solved a complex programming problem that you're notcertain you'll actually understand the next time you look at your code. It also helps other people who might be looking at your code to understand it better.
There are two ways to add comments to your code. The first is by using two forward slashes (//). Any text you add on the same line after these forward slashes will not be interpreted by AS3.0 as programming code. You can write anything you like; it will be ignored it when your program runs. Here's an example:
//Hi mom!! This is a comment. You can write whatever you like here.
Sometimes you might want to write a comment that contains more text than you can easily fit on one line. You can do this by creating a multiline comment . Multiline comments look like this:
/*
Anything between the opening and
closing multiline comment characters
will not be interpreted as
programming code.
*/
Multiline comments start with the character sequence /* and end with the character sequence */. You can write anything you like between them over any number of lines.
In addition to leaving notes to yourself, comments are especially useful for disabling sections of code if you want to test how your program behaves without them.
1. To get used to using comments, add one to your program. Modify your code with the following line in bold:
package
{
import flash.display.Sprite;
public class HelloWorld extends Sprite
{
public function HelloWorld()
{
//The next line displays some text
trace("Hello World! Can you read this?");
}
}
}
Before you go any further, ensure that your program looks like Figure 1-29 .
Figure 1-29. Add a comment to your code.
The ActionScript editor window colors comments green so that you can easily differentiate them from your programming code.
2. Finally, if everything looks good, save the HelloWorld.as file.
You're now ready to see result of the program in action.
Running your program and compiling the SWF file
So far you've only been working on one file, the HelloWorld.as file. This is your ActionScript program, and it's known as an AS file. It's the
Missy Tippens, Jean C. Gordon, Patricia Johns