Foundation Game Design with ActionScript 3.0, Second Edition

Read Foundation Game Design with ActionScript 3.0, Second Edition for Free Online Page A

Book: Read Foundation Game Design with ActionScript 3.0, Second Edition for Free Online
Authors: Rex van der Spuy
examples, it will make your program a little easier to read.
    As with the import statement, the new code is indented from the left margin to show that it's inside the package's curly braces.
    Figure 1-20 shows what your code editing window should now look like.

    Figure 1-20. Create a class inside the package.
    So what have you just done?
    You've created a class called HelloWorld. You've created it inside the package. The words
public
,
class
, and
extends
will be colored blue and purple to show you that they're reserved keywords that AS3.0 understands.
    Any ActionScript program you write must have at least one class, but most of the programs and games you'll be building in this book will have many. Take a quick look at the new code.
    public class HelloWorld extends Sprite
{
}
    This code is called a class definition . Just like the package, a class definition has a pair of empty braces hanging there in space. This shows you that, like the package, it's also a block statement. Those braces are empty at the moment, so the class can't do anything yet. That will change very soon.
    A class definition does three main things.
It creates the class. The keyword
class
tells Flash that you are creating a class. Simple enough!
It gives the class some rules to follow. This should be obvious: how many of you have been in a class without rules? This code tells Flash that the class you're creating is public . This means that the information it contains can be shared by any other classes that are part of the same package or part of any other packages your program might be using. The information the class contains is freely available to all. (Although most of this book uses public classes, sometimes you'll want to prevent classes from sharing information, much like a school football team wouldn't want to share its strategy with a competing school. In such cases, you would define a class as internal. Internal classes share their information only with other classes that reside in the same package.)
It gives the class an identifier, a name, which can be any name you choose. In this case, the class is called HelloWorld.
    You're not experiencing déjà vu! You have seen the phrase “HelloWorld” before. It's the name of the AS file that you're working on: HelloWorld.as. This is no coincidence. When you create a class, it has to be saved in a file that has an identical name to the class name. For every new class you create, you must create a new AS file that shares the same class name.
Finally it extends another class. This means that it's using some other class to help it do its work. In this case, it's using the magic of your infamous Sprite that you imported in the previous line ofcode. The keyword
extends
is telling the
HelloWorld
class to use the magic of Sprite to make text and images visible.
    There's a lot going on here, so let's look at this new line of code and simplify it in plain English so you can better grasp what it is saying.
    public class HelloWorld extends Sprite
{
}
    It's saying this: “Make a public class that other code you write can easily access. The name of this new class is HelloWorld. It extends another class called Sprite, which gives it the magical ability to display text and images.”
    Figure 1-21 illustrates what this new code means.

    Figure 1-21. The class will contain most of your program's code, the core of your games and programs, between its two curly braces.
    The class definition's poor little curly braces are still empty. Let's put them to use!
Using the constructor method
    The constructor method is the section of the program that makes things happen. Specifically, it throws the class into action as soon as the class is told to start working. Any programming code it contains is run instantly. If the class definition alone is just an empty shell, the constructor method is like its heart and lungs.
    3. Let's create the constructor method for the
HelloWorld
class. Add the code in bold to the code you already

Similar Books

Mickey & Me

Dan Gutman

Asher's Dilemma

Coleen Kwan

Brushed by Scandal

Gail Whitiker

Mayan Lover

Wendy S. Hales

Kiss My Name

Calvin Wade

Kamchatka

Marcelo Figueras