Beginning ActionScript 3.0 – Class 1

Dolphin-256x256 In the last section, we use some code to show the OOP style code. But the code can’t run. In this section, we’ll try to make it run.

Type the example code into your flash authoring tools, and then test it. If you’re using flash cs3, you will get the following result.

clip_image002

The error message means the compiler can’t find any information about the data type Author. And according to the last section, we need to define a class named Author.

Define the class

Go back to the flash CS3, clicked the [File] menu, and select [new], then a dialog will opened.

clip_image004

Select [ActionScript File], and then click OK. A new tab will open in the flash CS3. Now, you can type the definition code of the Author class here.

Before we write the code, we need to know something about the class definition.

ActionScript 3.0 class definitions use syntax that is similar to that used in ActionScript 2.0 class definitions. Proper syntax for a class definition calls for the class keyword followed by the class name. The class body, which is enclosed by curly braces ({}), follows the class name. In our example, the Author class maybe looks like:

public class Author {
//class body
}

One significant syntax change involves class definitions that are inside a package. In ActionScript 2.0, if a class is inside a package, the package name must be included in the class declaration. In ActionScript 3.0, which introduces the package statement, the package name must be included in the package declaration instead of in the class declaration. The following code shows the difference of class definition between ActionScript 2.0 and ActionScript 3.0.

// ActionScript 2.0
class packageName.Author{
//class body
}

// ActionScript 3.0
Package packageName
{

public class Author {
//class body
}
}

Here, we ignore the packageName now, and this means this package is a default package. So, the code will be

Package
{
public class Author {
//class body
}
}

Now, focus on the class definition statement.

public class Author

Ok, only three words here. The first one is “public”, this is a modifier of class attribute. There’re four modifiers internally in ActionScript 3.0.

l dynamic: Allow properties to be added to instances at run time.

l final: Must not be extended by another class.

l internal: (default) Visible to references inside the current package.

l public: Visible to references everywhere.

Here, we use public to modify the class, so we can reference this class in the main program.

The second word is”class”, eh, this is the keyword of defining class. When you want to define a class, you should use the keyword “class”.

The third one is “Author”, you should know that this is the class name.

So, the form of class definition statement is like:

Attribute modifier + class + class name

Moreover, we often follow the definition statement by a open curly brace ( { ), and begin the class body on the next line, this is a recommended style, if you’re using Flash CS3, you can use [Ctrl+Shift+f] to auto format your code.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

One Response to “Beginning ActionScript 3.0 – Class 1”

  1. [...] part 1 of class section(Beginning ActionScript 3.0 – Class 1), we have got some basic ideas about the package and the class attributes. So, we can write the [...]

Leave a Reply