Introduction
This series of articles are not intended to teach you object oriented programming. They are intended to teach you the framework and syntax around OOP. This is like the Pre-Calculus class you took before taking Calculus. It makes things a lot easier down the road. At least that’s my hope.
Pre-Object Oriented Programming
The problem with learning to apply OOP practices in your Actionscript is that it’s a radical departure from your existing workflow. Unless you have time to sit down and peruse Colin Moock’s Essential Actionscript book, a few times, then it’s just a frakking pain to try to switch over all at once when you know full well you could go about doing things as you always have and get the job done.
Besides, who cares if you use OOP practices? It’s likely that no one but yourself will ever see your code. And if the end result is the same then why bother. Right?
Wrong. Adopting OOP coding into your projects will make them much easier to write in the first place, and they’ll be easier to update later.
Plus, you can put it on your resume and make more money. :-)
Note: I’m assuming here that you are comfortable with at least creating variables and the occasional function. If you know how to pass parameters to a function then you’re in good shape and if you know how to return a value from a function then everything’s shinny.
The best way to learn OOP in Flash Actionscript is to start with the syntax. Here is a list of some of the things you don’t need to know in order to get started.
- instantiate,
- public vs private,
- instances,
- objects,
- constructors,
- methods and properties,
- design patterns
Yep, you heard me. Skip all that crap. You can pick that stuff up later.
No more _root
When you write code do you create variables on the _root, like say myVariable, and then refer to them from code inside MovieClips all located all over your FLA with: _root.myVariable? Well, no more.
Here’s what you do:
- Create an FLA and save it with any fileName you want.
- In the same folder as the FLA create a new .as file.
- Give the .as file a fileName of: Root.as
- Copy the code from below and paste it into the the Root.as file
- Make sure you save the Root.as file.
- In the FLA you created write this: trace(”from the Root class: ” + Root.myVariable);
- Publish and your Output panel should say: from the Root class: some value
Here’s the code for the Root class:
This is the first step into a much larger world. From anywhere in your FLA you can access the variables in your Root class just as you did with _root before. But now your variables are in a different file and easier to locate.
Lets go over this class file real quick.
- The public static var is just what you put before each and every variable - variables are called properties in a class.
- The :String or :Number are how you type your properties. You don’t have to do that but you really, REALLY should. Trust me on this. Just get used to it.
- It is a convention, and one you need to follow, to make all class names start with a capital letter. So don’t make a class name such as myClass. It should be MyClass.
A few gotchas
Very, very important: the fileName of the class file and the name of the class must be identical. Except the class name does not get the .as extension. So if the filename of your class file is SomeClassName.as then the name of the class must be SomeClassName. If you named the class SomeClassname - lowercase n - it wouldn’t work.
I’ll tell you right now, this above issue gets me all the time. The problem is that you usually give the file a fileName before you give the class a class name. Once you start write your code you think to yourself, “This ClassName would be more descriptive if it were called SuchAndSuch. So you change it, then forget to change the fileName and everything’s a mess. You’ll may do this a few times yourself so be on the lookout.
You know how with FLA files you can publish them anytime to see your changes without saving first? Well, you can’t do that with .as files. If you change something in your .as file you have to save that file (Ctrl + s) before you can publish the FLA to see those changes.
You do not need to upload the .as files to the server in order for the SWF to work. When you publish your FLA file, creating the SWF, it takes the code in all your .as files and wraps it up into the SWF. Now the SWF can stand on it’s own.
If you share your FLA with someone then you have to give them all the .as files as well. The FLA won’t work without them.
What’s next
You put some variables - properties - in your class file. So what about functions, called methods in a class file? That’s just as easy to do. Here’s an example added to the code above.
Lines 17-19 make up the method doSomething. If from anywhere in your FLA you typed: Root.doSomething(); you would see the Output panel display “this is code running in a method of the Root class.”. Which doesn’t do much of anything. Lets add another one that’s a little more useful.
Take a look at lines 21-25. The method average2numbers receives to arguments: a and b. It takes those two numbers and adds them together, then divides by two and then returns that result. That’s why line 21 has :Number appended. That declares the type of value that the method will return.
So back in your FLA you can say something like this:
Line #1 says, I want to create a variable, of type Number, and I want it’s value to be equal to the result that comes from sending the parameters (2,6) to the method average2numbers in the Root class. Then line #2 just traces that result to the Output panel.
This should enhance your workflow right off the bat by giving you a place to put your variables and functions to get at them faster than if you placed them on the _root of your FLA.
In part 2 you’ll learn how to make those public parts private and create getters and setters.