wrote.
package
{
import flash.display.Sprite;
public class HelloWorld extends Sprite
{
public function HelloWorld()
{
}
}
}
Your ActionScript editing window should now look like Figure 1-22 .
Figure 1-22. Add the constructor method inside the class.
As you can see, the constructor method is simply another block statement with empty curly braces that looks an awful lot like the class definition. In fact, it has the same name:
HelloWorld
. This is no accident: all classes need to have constructor methods that are named exactly the same as the class name.
The other thing you'll recognize is the keyword
public
.
public function HelloWorld()
{
}
As with the class definition, using the word
public
tells Flash that the constructor method is freely available to be used by different classes from different packages. (A strange quirk of the AS3.0 language, however, is that constructor methods can only ever be public.)
One new thing here is the
function
keyword.
public function HelloWorld()
{
}
It tells Flash that you're creating a function definition . Function definitions are simply block statements that perform actions. They do all the work in your program. You can think of function definitions as dutiful servants who snap to attention with a prearranged set of chores as soon as you call on them. The constructor method, which will always be the first function definition you write when you create a class, has the special job of running any actions it contains immediately—as soon as the class is called upon and before any other methods you might be using are put to work. The constructor method is a bit like the head servant who's up at the crack of dawn, gets all the other servants out of bed, and greets you with a fresh pot of tea and the morning paper before you've even found your slippers.
The last thing you should notice about the constructor method are the parentheses after the method name, which are highlighted here in bold:
HelloWorld()
Those empty parentheses allow you to provide the method with extra information, known as parameters , if the method needs it. You'll look at method parameters in detail fairly soon, but for now you just need to know that you must provide a set of parentheses when creating a function definition, even if those parentheses are empty.
Aligning code
You might have noticed an interesting pattern developing in the format of the code. Like a set of hollow wooden Russian dolls, the
HelloWorld
constructor method is inside the
HelloWorld
class, which is inside the
package
block statement. Each item sits inside the outer item's pair of curly braces, and you now have three levels of block statements. The only way that you can tell where one ends and the other begins is by whether the block statement's curly brace is open or closed.
As you can see, this could easily result in a confusing tangle of curly braces. If you weren't absolutely sure which pair of braces belonged to which block statement, you could start adding new code in the wrong place, and you'd get all sorts of errors when you tried to run the program.
The code formatting that Flash Builder does for you automatically helps solve this potential confusion somewhat. I recommend you use this style of formatting for the projects in this book. Figure 1-23 shows that you can draw an imaginary line between a block statement's opening brace and its closing brace. It very clearly shows you at which indentation level you should be adding code.
Figure 1-23. You can make sure that you're adding code in the right place by keeping each block statement's opening and closing braces aligned along an imaginary line.
You can clearly see from Figure 1-23 that the import statement is part of the package, not the class. And you can also see that the constructor method is part of the class because it's contained within the class's curly braces. That's thanks to the indentation.
There's another way of