Learning how to OOP in Flash
WTF is OOP?
OOP stands for Object-Oriented Programming. When you are coding big projects you will have hundreds (even thousands) of lines of code and it can get very unorganized. This is where OOP comes in. You can make classes, which are external .as files (ActionScript files) and import them into your project. You can then use the methods and properties in that class.
Also, instead of having to write the same code over and over again you could make a class containing that code. Then whenever that code is needed instead of having to write it all you can just call it from the class.
You must have Flash 8 or Flash CS3 for this tutorial. In Flash CS3 when you open the .fla select an AS 2.0 document.
Classes in flash consist of Methods (functions) and Properties (variables). For example, an Enemy class might have the Methods hunt down player and guard base.
It might have some properties like HP left and range of sight.
The class is just like a stencil though. For each enemy we would have to create an object so each enemy can have its own HP and own range of sight. I will go more into this later.
Now open flash and make a new Actionscript file. Lets create a our first class. Declare a class with the ‘class’ keyword, followed by the class name. The class name needs to be the same as the filename (A class called Person needs to be saved as Person.as). Lets call our class ‘MyFirstClass’.
function MyFirstClass(){
trace("You have created an object of my first class");
}
}
Here we have created a class called MyFirstClass. You can see that it contains one function. This function is called the constructor. It doesn’t have a return type and runs everytime a new object of a class is made. Now save this somewhere as MyFirstClass.as and open a new .fla.
In the new .fla enter this code on the first frame.
var myObject:MyFirstClass = new MyFirstClass();
Save it in the same directory as MyFirstClass.as (it doesn’t matter what this file is called) and hit Ctrl+Enter. The output window should appear with “You have created an object of my first class” in it. This is because when we created an object of MyFirstClass (called myObject) it ran the constructor which outputted (is that even a word?) the string.
Let’s add a bit more code.
private static var Num:Number = 0;
public var Str:String = "Hello World!";
function MyFirstClass(){
Num++;
trace("You have created " + Num + " objects of MyFirstClass");
}
}
There are some more words you should get used too. These are Private, Public and Static.
Private: A method or property that is private can only be accessed inside that class or it’s subclasses. So we can only access the Num property/variable inside the MyFirstClass. It can’t be accessed through an object we create of the class.
Public: If a method or property is public it can be access anywhere. So we can access Str by typing myObject.Str.
Static: Only one static method or property is created per class. Even if there is 100 objects of the class. Since the Num property is static there is only 1 of those even if we had 20 MyFirstClass objects, but there would be 20 Str properties because Str isn’t static.
Now go back to the .fla and make another object.
var myObject:MyFirstClass = new MyFirstClass();
var myObject2:MyFirstClass = new MyFirstClass();
If you hit Ctrl+Enter you should see this message in the the output window:
You have created 1 objects of MyFirstClass
You have created 2 objects of MyFirstClass
That is because in the constructor of MyFirstClass the Num variable increases by one, and since it is static it stays at that number and will keep track of how many objects of that class you have made. Now add this code:
trace(myObject2.Str);
This will output “Hello World!” twice, because Str was created for both myObject and myObject2 unlike Num. Since Str is public we can edit it for files other than the class file.
var myObject:MyFirstClass = new MyFirstClass();
var myObject2:MyFirstClass = new MyFirstClass();
trace(myObject.Str);
trace(myObject2.Str);
myObject.Str += " Goodbye World.";
trace(myObject.Str);
trace(myObject2.Str);
Now if you test your movie it should output “Hello World! Goodbye World.” on the second last line, because we changed myObject’s Str property. It still says “Hello World!” for the last line because myObject2’s Str property wasn’t changed.
I hope I have explained this well enough. I will try and do a few more tutorials covering this subject. Click here to download the source files.















very good tutorial was clueless but you opened my eyes to the possibilities. Creating Movie Clips and so on. Will be playing with this
[…] Learning how to OOP in Flash […]
This really is a great tutorial for explaining Public, Private, and Static…but did not begin to tackle the greater question, in your words, of “wtf is oop?” I think a full length introduction to OOP would be great coming from you as you have a good teaching style!
i’m very impressed with your style of teaching, it is clear and interesting and the subject is just what i needed. thank you