How to write my first Class and its constructor function
What is a Class?
Let us see it this way: think about the construction game LEGO and its colored blocks which put together can build wonderful construction
Here you are, a Actionscript 3.0 Class is a LEGO building block.
The Class can be used and called (instantiated) as wanted when and where we need it.
Once the instantiate of the Class is created, its methods and properties are available whenever we want them, but it is something I will explain in the next lesson.
For now, I would like to show you how to write a simple Class and tell you what is necessary to make it works.
First of all, with Actionscript 3.0, it is needed to implement/create a package. I find it useless explaining at this point in details what a package is as it would only confuse you even more. So let us simply say that a package is a box into which can contain more Classes. To this box, we also can assign a name. I will get into that later on.
Follow me...
For the time being, we implement a package without a name and accessible the following way:
I create an AS file and save it as 'Main.as' (PS: I recommend you to give it a name starting with a capital letter) and I write:
as Flash CS3 has a Class structure subdivided in packages, we will import the correct package of classes.
In this case, we will use that Class as a Document Class, meaning as the main class of our project or application. So we will import the MovieClip Class. (PS: who wants more info on the Document Class can read the article ėThe Document Class of Flash CS3ķ)
Why the MovieClip class?
Because, we need it to inherit its properties and its methods. It is still too premature to explain it in details so I invite to only keep in mind that the MovieClip Class needs to be imported.
To import the MovieClip Class, included in the package flash.display, we write:
Code:
package
{
import flash.display.MovieClip;
}
with the import command, we tell Flash to import something which will then be available for us to use while developing our application. We then tell Flash that we want the MovieClip Class contained in the package flash.display.
We can now declare our class the following way:
Code:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
}
}
public means that the class is public. This attribute makes the class accessible even from outside the package. Meaning that we will be able to instantiate the class Main.as from another class or from the timeline.
class let Flash know that we are declaring a new class.
Main is the name of the class. The name of the .as file and the declaration of the class needs to be the same.
extends is an attribute which I would tell you to take it as a rule. If our class is to be the main class of our project (the Document Class), it needs to include the attribute extends. The class extends and inherits the methods and properties of the MovieClip Class. In the following lessons, you will understand the use of it better.
MovieClip simply means that our class extends the pre-existing class in Flash named MovieClip
Now, we need to define the building function (or constructor) of our class Main.
I will explain the concept of the building function:
The building function is the function which will carry out the code contained into it at the time the class Main is instantiated, so when we will instantiate Main from the timeline via the text field named Document Class in the Property Panel of our FLA.
The building function needs to:
- have the same name and case as the class
- do not need to be called. We simply need to create the instance of the class and Flash will automatically carry out the codes
- can be implemented and receive parameters (see next lesson)
- do need to be defined. If you do not do it, Flash will do it for you and so you will not be able to assign codes to carry out when the class is being instantiated
- can only be public, hence with the attribute public
- can not return a value
The building class of Main.as is defined the following way:
Code:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
}
}
}
From now on, our class Main is ready to be used.
How to use it? Simple!
I create a main FLA and save it as ėmain.flaķ in the same folder as our Main.as.
In the option field named Document Class located in the Property Panel of the FLA, I write: Main (without the .as extension).
If we want instead to instantiate our class Main.as via coding, we would have to write:
var main:Main=new Main;
We will get later on into this concept. For the time being, let Flash do that job by simply inserting Main in the ready-made option.
We can now understand easily the point of the building function (public function Main). Back to the Main.as file, let us add a trace in the building function so to check that the trace is carried out as soon as the SWF is published:
Code:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
trace(" Main class has been instantiated correctly ");
}
}
}
we publish the SWF and if everything has been done correctly, Flash will immediately carry out the code contained in the building function and the result of the trace should then appear in the output window.
As you can see, even though the function Main has not been called directly, the code has been carried out. From the moment the class is being instantiated, the included code is being executed.
Source files:
Bookmarks