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

Take a survey


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.

 

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