First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing! | Flash Jobs
Search tutorials
Author: Kortex | Website: http://www.visualflowdesigns.com

Examining The Class File:

So by now you have probably noticed that class files are just external ActionScript Files with a specific structure. Let's take a closer look at that structure.

The Class File:


Here is the whole file again.

class Person {
var _myname:String
function Person(myName:String){
_myname = myName;
trace("New Person");
}
function sayHello():Void{
trace ("Hello, my name is " + _myname);
}
}

Class Definition:

Everything that makes up a class is contained between opening and closing brackets { }. The name of the class is preceded by the keyword class. So an empty class named person would look like:
class Person {
}

Since this is a simple tutorial, we have created our class inside the root of the class path that we added. You can further breakdown your class path into folders called packages, but I will not go into that in this tutorial.

Instance Variables or Properties:

Shortly following the opening bracket of the class, we see the following line:
var _myname:String

This is an example of an instance variable or class property. You can have multiple instance variables, but for simplicity's sake I just used one. Instance variable hold information about the state of the class. Each instance of the class (more on this in a bit) gets copies of these variable. The fact that each instance gets copies allows us set different values for these properties for each instance (we actually did this in our example, but I will explain this more shortly).

Also notice that I have not actually assigned value to this variable yet. And notice the :String. The:String is an example of a concept called variable typing. I will not go into this in great detail here, but just know that when I type a variable it can only accept values of that type. So in this case _myname can only be a string.

Finally, notice the _ at the beginning of the variable name. It is a common practice to put an _ before instance variable names to distinguish them from other variables.

The purpose of instance variable should become clearer when we examine the code we used in the class_basics.fla.

Constructor Function:

Following the instance variable, we see a function with a name that matches the class name:
function Person(myName:String){
_myname = myName;
trace("New Person");
}

This function is known as the constructor and is automatically called when the class is instantiated. Instantiated simply means I create an instance of the class using the new keyword (e.g., new Person();). In our example we pass something called a parameter to the function. A parameter is anything that appears between the () when you call a function. In our case we pass a string and use that value to set the _myname instance variable. Please note the following regarding constructors:

  • The common use for constructors is to set the initial state of the class instance (commonly this is setting instance variable values or calling initialization functions/methods). Basically, this is the set up function.
  • If you do not need, or want, anything to happen when a new instance of the class is created, you can supply an empty constructor (one that does nothing when it is called), or you can leave it out completely. However, in our example that would not work well as the _myname var would never be set. Hence put some thought into whether the construtor is need and what it should do.

Methods:

Finally, at the end of our class we see:
function sayHello():Void{
trace ("Hello, my name is " + _myname);
}

Functions within a class are referred to as methods. While the constructor method is called automatically, other methods within a class have to be specifically called (though you can call other methods from within the constructor). To call a method of a class instance you use the following syntax:

instanceName.methodName();

You will see examples of this when we go over the code from the class_basics.fla in the next section. These methods can also accept parameters, in which case, the syntax would look like:


instanceName.methodName(parameter);

Summary:

So that concludes our brief look at the structure of a simple class. There is a lot more that can go into a class file and there are quite a few things that I did not explain, but for now, let's move on to exploring what happened when we used the code in the class_basics.fla.

'
» Level Intermediate

Added: 2007-02-20
Rating: 8.83 Votes: 29
(10 being the highest)
» Author
Kortex (aka Jeremy Wischusen) is a Flash/PHP developer for myyearbook.com and the lead software architect for the DigitallyU Digital Portfolio Software Suite.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: work2survive
» Title: Girl Walking 1kb
» Description: Walking girl, animation just takes 1kb, using high heels shoes, and fashion model attitude.
Featured Sound Loops
Image for equalize

Audio Player

» Author: BoxCat
» Title: CPU Talk
» Description: From the cyberpunk RPG, "Nameless: the Hackers" Completely Free to use! Keywords: edm hip hop rap electronic mysterious mission hacking chase exciting spy ios ambient chilling love happy sad angry suspense
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: nathan
» Description:

A recorded sound of the Dimensional Fork Gate.