View comments | RSS feed

Writing the code for a class

Once you have a design plan for your class, or at least some idea of what information it will need to keep track of and what actions it will need to carry out, the actual syntax of writing a class is fairly straightforward.

Here are the minimum steps to create your own ActionScript class:

  1. Open a new text document, in an ActionScript-specific program such as Flex Builder or Flash, in a general programming tool such as Dreamweaver, or in any program that allows you to work with plain text documents.
  2. Enter a class statement to define the name of the class. To do this, enter the words public class, and then the class's name, followed by opening and closing curly braces that will surround the contents of the class (the method and property definitions). For example:
    public class MyClass
    {
    }
    

    The word public indicates that the class can be accessed from any other code. For other alternatives, see Access control namespace attributes.

  3. Type a package statement to indicate the name of the package in which your class will be found. The syntax is the word package, followed by the full package name, followed by opening and closing curly braces (which will surround the class statement block). For example, we'd change the code in the previous step to this:
    package mypackage 
    { 
        public class MyClass
        {
        }
    }
    
  4. Define each property in the class using the var statement within the class body; the syntax is the same as you use to declare any variable (with the addition of the public modifier). For example, adding these lines between the opening and closing curly braces of the class definition will create properties named textVariable, numericVariable, and dateVariable:
    public var textVariable:String = "some default value";
    public var numericVariable:Number = 17;
    public var dateVariable:Date;
    
  5. Define each method in the class using the same syntax that's used to define a function. For example:

There are a few more class elements that you can define.These elements are more involved.


Flash CS3


Comments


philggg said on Apr 22, 2007 at 2:09 PM :
What is the relationships between:
.as files
packages
classes

Can there be more than one class in a package?
djtechwriter said on Apr 23, 2007 at 2:39 PM :
Only one class per package, but you can add classes within the same file outside the package declaration. For more details see this section:
http://livedocs.adobe.com/flash/9.0/main/00000041.html
LPCpatrick said on May 31, 2007 at 1:10 PM :
Having issues with calling private methods from event listerners inside custom classes. Is there something I am missing or any documentation on this?

ex.

package {
// imports
class MyExample extends Sprite {
// propreties and constructor....
xmlLoader.addEventListener(Event.COMPLETE, myXMLHandler)

private function myXMLHandler(evt:Event):void{
// dostuff to XML doc and call other function....
myOtherFunction()
}
private function myOtherFunction():void{
// do something with some propreties
}
}
}

results in : 1009 error.

thanks
No screen name said on Dec 1, 2007 at 1:42 PM :
I think you need to add "this." in your eventlistener to make it work.
Todays Past said on Jul 31, 2008 at 3:11 PM :
Okay, you lost me. The whole thing seems very buried to me and I need some help to climb out.

Why do you need the "Package" statement? What's so special about what it does?

In a single Package{} there should be only one class defined?

Can there be multiple Package{}'s in a single .as file?

Is there a naming restriction on the Package Name, the class name, the file name - is there a relationship between them?

This question comes from previously on page 28 where you stated

"Use the import statement to specify the full name of the class, so the ActionScript compiler knows where to find it. For example, if you want to use the MovieClip class in ActionScript, you first need to import that class using its full name, including package and class:

import flash.display.MovieClip;

Alternatively, you can import the package that contains the MovieClip class, which is equivalent to writing separate import statements for each class in the package:

import flash.display.*;"

I'm assuming this all ties together, but I'm confused about how.

BTW - If the answer is "please see page 41" which I have not done yet, then let me propose that this section be enhanced, eliminated, or worst case include a forward reference, because too many questions/answers are being forward referred.

All was said with a smile on my face and a song in my heart.
Joe ... Ward said on Aug 8, 2008 at 10:40 AM :
The package both organizes your classes, and in the case where two class names are the same, provides a mechanism for distinguishing between them. The full class name is really package.Class, but the import statement allows you to use just the class name in your code. If you were using two classes that had the same name from two different packages, you would have to use the full package.Class syntax whenever you referred to them in code.

You can think of a packages as directories and subdirectories of class files (and in fact that is how the compiler finds the source files). Each segment of the package name between the dots is a directory. The class at the end is the file defining that class. Using "*" in an import statement is like using a wildcard when listing a directory, all the classes/files in that directory are included. So when the classes in the flash.display package was compiled, the compiler looked for the classes in the following directory structure:
flash/
display/
MovieClip.as
Sprite.as
...

Yes, packages are more fully explained further on. They have to be mentioned here since the package statement is required to get a class to work.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000033.html