visualizing how the code is being organized. I've been throwing around the term “block statement” quite a bit. This term is actually a very concrete description of how indentation and curly braces are used to structure the code. You can think of each pair of curly braces as an empty box or block. Every time you create another statement with its own set of curly braces, you create a new block inside the first one. Figure 1-24 illustrates how you've created three blocks of code in your program so far. Each block sits inside the other.
Figure 1-24. Each pair of curly braces contains a block of code. Blocks can contain other blocks.
A frequent confusion when programming code is not being certain where one block statement ends and another begins. If you use this suggested format and can see these imaginary divisions while you write, it will really help you to prevent accidentally adding a line of code in the wrong place.
If you're using Flash Builder, you can use its Outline window to see all the code blocks in your program and in which of those blocks you're currently working in. You'll find the Outline window to the bottom left of the code editing window. Figure 1-25 shows what your Outline window will look like if you've followed the instructions in this chapter so far.
Figure 1-25. The Outline window shows you all the code blocks in your program.
The Outline window clearly shows that you've got three blocks: the package, the class definition, and the constructor method. It even throws in a few cute little icons to illustrate them. The import statement is part of the package block because it's at the same level of indentation.
The Outline window and the code editing window are linked. If you click on a block in the Outline window, the code editing window will highlight the same block, and vice versa. For example, if you click the constructor method block in the Outline window, the constructor method in the code editing window is also highlighted, as shown in Figure 1-26 .
Figure 1-26. If you select a code block in the Outline window, the same block of code is highlighted in the editing window, and vice versa.
In long or complex programs, you can use the Outline window to quickly find the blocks of code that you want to work on.
What's your directive?
The next bit of code that your program needs is a directive . One of the great pleasures of computer programming is being able to bandy about terms like “constructor method” and “directive” as though you were the captain of your own personal galactic cruise liner. Very simply, a directive is a single action that you want the program to perform. It tells your program, “Do this now!” Methods are made up of one or more directives, and with them the program springs to life.
4. The directive you'll add to the constructor method will tell the program to display the words “Hello World!” in its Console window when the program runs. Add the following bold text to your program:
package
{
import flash.display.Sprite;
public class HelloWorld extends Sprite
{
public function HelloWorld()
{
trace("Hello World! Can you read this?");
}
}
}
Your code editing window should now look like Figure 1-27 .
Figure 1-27. Add the new trace method to your code. It will display any text in quotation marks that is between its parentheses.
This is the new line of code that you wrote:
trace("Hello World! Can you read this?");
This is called a trace method . It's a special directive that tells your program to send whatever is in parentheses to Flash Builder's Console window when you debug your program. You haven't seen the Console window yet, but you will very soon! In this case, the trace method will send the words, “Hello World! Can you read this?” to the Console window. The important keyword here is trace.
If you want to send the Console window some text using trace, that text needs to be surrounded by quotation