AS3 for Noobs (Part 1): A class on classes
Alright so I’ve had some time to calm down from my initial AS3 adventures and things are going much better now. Once I finally got over the hump of trying to compare AS3 to AS2 and just thought of it as something completely different (even though it’s not completely) I’ve actually been enjoying the experience. Now that I’ve been working with it for long enough, I can tell you that I’ve joined the OOP fan-club and got a cool t-shirt that says I heart OOP.
NOTE: The point of this article is not really to teach you how to use classes, but more to help you understand what classes are and why you should use them, because to me knowing “why” and “when” was much harder then learning the “how”. If you want to know how to use them check out the help file that came with flash because it gives a lot of good examples. Also I’ll be oversimplifying what classes are and do in this article because at the moment we are just taking some baby steps hence the “AS3 for Noobs” title.
My biggest hurdle was that in AS2 I never really had to worry too much about how items related to one another. I could put a movie clip or a function anywhere I wanted and just attach variables to the root or make it global. But then they took away those things as I had come to know them and I felt like a fish out of water trying to catch my breath. The other main obstacle was I never really had worked with classes before. I’ll rephrase that, I had never created my own classes before, I worked with them all the time whether I knew it or not.
I had read about classes before because everyone else said that’s the way to go but to me it seemed overly complicated. When I’m learning something completely new I can get overwhelmed pretty easily if it seems overly complicated and I never really found any tutorials on classes that dumbed the explanation down enought for me. Why would I write a class when I could just write a function and make it global? The main argument I heard was that it made your code more modular or reusable because you could just import it anywhere but then I thought “How is that different from just doing an include anywhere?”. Well I’ll tell you the difference and to me it’s all about organization.
Have you ever had a desk that just got insanely cluttered but you didn’t want anyone to touch it because you like it all right in front of you where it’s easy to get to and you swear YOU know where it all is? Now have you ever had someone come along and help you orgainze all that junk into folders and drawers so that all your bills go in a folder, and all your tax stuff goes in a folder. Sure it takes a little more effort to keep them orgainized and only pull out things when you need them, but the benefits of doing things this way far exceed the extra effort. I mean you only need your tax stuff once a year so why dig through it all the time to find something else? Plus if you ever need anyone to help you with your taxes they can quickly come in and just grab your tax folder. Maybe it’s a long analogy but it’s an important one because classes did exactly that for me.
Classes are folders for your code. I can put all my functions that relate to a certain topic or type of object in a single class. So all my functions that help me load images could be in a class called “ImageLoader” and all my functions that help me load data could be called “DataLoader”. If you are already seperating your code into seperate actionscript files and grouping them this way without using classes, you might not see their real benefit at the moment. Simply putting functions and variables in seperate actionscript files would allow you and others to quickly know how all the items relate to each other but it doesn’t help flash to know how they relate which I think is where the biggest difference occurs.
What do I mean by this? Well when you have some code in a seperate actionscript file or even just store them in seperate frames in your movie flash doesn’t know that any of that code relates to any other code in your movie. To flash it’s still just as big of a mess as if it wasn’t organized at all. Classes tell flash how objects and code are related to each other so it make’s it’s job that much easier. It says “Ok, I’m loading an image so this is the only chunk of code that I need to worry about right now.” Flash doesn’t have to worry and in fact won’t worry about anything outside of what’s in that class unless you tell it to. So you can see how creating classes not only speeds things up with you but also the flash player as well.
This is of course the mere tip of the iceburg, I plan to cover most of the more dramatic benefits of classes in later articles. I think for now this is enough to get you thinking about why you need to make the switch and realize that it’s nothing as intimidating as you might think. Sometimes the first step is always the hardest and hopefully this will make your first steps into the world of classes a little easier.
September 10th, 2007 at 9:49 am
As I saw on a paper on a office, “all easy things was hard on beginning”.
September 12th, 2007 at 4:32 am
Great article. This is just how I felt when I started getting into AS3. Im much more comfortable now, but it seems we have shared the same thought proccess and experience, and its great to know that you will doubtlessly be helping many people who shared our initial concerns.
Keep up the good work!
October 8th, 2007 at 10:19 am
Great, just what I need right now, thx a lot!