In part 1 of this article we had a look at Object Oriented programming from a conceptual point of view and we observed that we could find an analogy between the way a lot of organic systems are organized, from a solar system to a human body and the way OOP revolves around classes, objects, properties and methods.
We've also seen that we can define generic patterns or templates that we can use to create objects. Those patterns are called classes in OOP, they are the archetypal representation of an object. Today we are going to get down to earth and I am going to try to give you the very basic elements that you need to start OOP with actionscript.
Creating a class in actionscript (2.0 and 3.0)
If you have never used OOP before you might be wondering why we suddenly start talking about classes. Well classes have always been there in Flash it's just that they are well hidden and you never had to import them because Flash was doing it for you. When you write:
| myVars:LoadVars = new LoadVars(); |
you are actually creating an instance of the LoadVars class, it's just that you don't need to import the class before doing it because it is a native class. What we are going to do here is creat our own custom class so let's start simple and see how we can do that.
Since there are some changes from AS2.0 to AS3.0 regarding class declaration I will make sure to give you both versions like that you can see the differences.
Classes need to be created in an AS file that needs to be saved with the same name as the class.
To create a new AS file you can, in Flash, click on NEW and in the new window select
"Actionscript File".
You will then start with a new fresh window where you can start your coding.
The first thing you need to do is to find a name to your class, the convention also is to capitalize the first letter of the class. Let's call our class
MyClass
and see below how I declare it in AS2.0 and in AS3.0
You will notice that the only difference is that we are using the
package keyword on AS3.0. Just imagine packages as directories on your hard drive. When we just write
package with no values after that it means that the flash compiler needs to look for the class in the same directory where the FLA resides, as simple as that. If the class is in a directory called "classes" you will just need to write
package classes, then the compiler will know where to look for the class.
By default the compiler will always look for your class in the directory where your FLA is sitting, if it can't find it there it will check your default class path in your settings. You can change the default class path from the area by clicking on the little button near the area. (FLASH 8 example below)
Let's come back to our class. We have declared it but it is not finished. We now need to declare the properties of our class, if it has any. Let's decide that initially we are not going to have any properties so what we need to do now is to create what is called a
constructor. The constructor is the main engine of the class, the constructor is the function that will create the objects of the class, the instances. The rule to create a constructor is that it needs to have the same name as the class so in our case the constructor will be called
MyCLass
and we just need to use the
function keyword in front of it just as we would do for a normal function.
Also notice that for the AS3.0 version we use the keyword
public in front of class. Just know for now that we need to set the class as
public to be able to access it from outside the package, in our case from the timeline. We will talk about that point in more detail in part 3 of the article.
That's it our class is ready, save it as an AS file in your working directory and make sure to save it with the same name as the class itself so you will end up with a file called
MyClass.as
Creating an instance of MyClass from the FLA
Now we have a class sitting in an AS file and we want to be able to use it so how are we going to do that? In Flash there are really 2 ways to do that:
1. We can create instances using the
new keyword followed by the name of the class, you are surely familiar in a way with that syntax.
With that method we first need to import the class using the
import
keyword. We need to do that because it is a custom class and Flash does not know about it.
That's it - it's all you need to do, if you you should see in the output window. We just created an object in OOP :)
2 . The second way to create an instance of a class is to associate a Movie clip with the class using the property. That method is probably the one you will use more often since Flash is very much designed to create visuals.
To associate a class with a movie you obviously first need to create a movie clip, then right click on that movie clip in the library and select .
In the new window in the area just enter the name of the class that you want to associate with the movie clip MyClass in our case.
That's it, we are all set, again and you should see in the output window. We just created an object in OOP using the Linkage property of a movie clip :)
 |
In the next part of this article we will have a look at properties and methods, what they are and how to use them.