(last edited on August 22, 2015 at 9:05 am)
A friend of mine, just getting into more advanced programming topics in Macromedia Flash, asked me what classes were.
“That’s an excellent question!” I beamed, pleased at the opportunity to share some hard-won knowledge. “They’re useful for constructing object hierarchies–“
“What are objects? And why are there hierarchies of them?”
“Well, you can model a problem as a series of interconnected objects…”
“Huh? Why? Where do you put them? Are they files?”
Uh oh.
There has to be a better way of explaining classes to people without going down the tired old “Classes allow you to build object-oriented programs that make it easier to model real-world problems” explanation. The entry in Wikipedia is a great example of a definition that makes absolutely no sense to a beginning programmer:
In, object-oriented programming, a class consists of a collection of types of encapsulated instance variables and types of methods, possibly with implementation of those types together with a constructor function that can be used to create objects of the class. A class is a cohesive package that consists of a particular kind of compile-time metadata.
It just gets worse the more you read. It’s all technically correct, but like a lot of technical writing it tends to describe rather than explain. Which is pretty useless when you’re trying to understand something new.
So I’m thinking next time some asks me about classes in Flash, I’m going to try explaining it first in non-OOP terms.
- Classes allow you to group functions and variables together. You do this by using the
class
statement, which contains all the functions and variables that you want to be related to each other. This can make it a lot easier to organize your program into groups of classes, as opposed to huge lists of functions. - Once you define a class, you can treat it as a new kind of data type. You can create new instance variables of a class you’ve defined using the
new
statement. To use the functions and data that you have defined inside the class, you use dot notation on the instance variable. - Class definitions can also be used to organize your global variables, if you use the
static
keyword. This is far more useful than using the_global
object. - A lot of Flash’s built-in features are packaged in the form of pre-defined classes, so learning how to use classes is essential to unlocking the advanced features of Flash’s script environment.
- There are two other big advantages to using classes in AS 2.0: the compiler will be able to find errors and tell you what is wrong, and you can put all your source code inside the Flash Project Window. No more hunting for script fragments inside of your buttons and movieclips…they’re all in one place, finally.
At this point, one has to start talking about how to set up classes syntax-wise, explaining classpaths and packages, and the “one class per file” rule. I think one could probably defer a discussion of object-oriented programming until after the syntax is understood.
However, I think a lot of Flash programmers come at AS2.0 from a “I want to do X” perspective, and they they just want to know what properties to twiddle so the right thing happens. Do you force them to understand classes and object hierarchies?
Sleepy, will reflect on this later.
UPDATE: I’m starting to dump some thoughts into the blog as a kind of “think aloud” exercise. The next part is here.
5 Comments
Your friend might benefit from this excellent book:
Object Oriented Programming with ActionScript 2.0
This book is a permanent fixture on my desk.
——-
Great post, Dave. I’m thrilled to see someone give an explanation in English for something I think is very fundamental to writing good code.
If you have never written actionscript outside the Flash MX 2004 IDE, you’re missing out. Even if you’re doing AS1, you can still use #include statements to import text files, and you’re using an editor that is generally regarded as a bug-ridden add-on tacked to what was essentially a tool for animators.
Because of this, the editor has very little polish, and besides being at times irritatingly unusable, does not let you hack it. When I say hack, I really mean teaching the program how you work with macros, snippets, shell integration, and a decent plugin architecture. When you move from an all-in-one to a more decentralized approach, you gain the advantage of being able to pick what works for you of what others have done, and write your own commands to, for example, automatically correct your most common speeling mistakes.
I got inspired by this post to write a series for anyone who’s new to OOP without a single line of code. People can research code samples, but they can’t research something that they don’t know the name of.
Beth: Thanks for the recommendation! I’ll pass it along. I’ll also check it out next time I’m at the book store…I’m curious to see how they handled MovieClips.
Ben: Great article! Looking forward to seeing the rest of the series! My AS development has been relatively simple, as I haven’t really felt a crying need to switch out of the IDE. There have been a few times where I wanted to get lists of functions and class hierarchies for reference, but that’s it. Maybe I was just thankful that there was finally a project manager in MX 2004, as crude as it was.
If you’re using a Mac, I can completely understand why you’d want to get out of the Flash IDE…it’s unusable, and the first release of MX2004 Mac was so bug-ridden that some kind of class-action lawsuit was started.
I’m having this exact problem. I;ve been trying to get into OOP with AS 2.0 (the prac tice, not the book, although I do have the book) but every time I try to develop a project that way I get frustrated and end up doing things mostly procedurally.
I’ve read both “Object Oriented Programming with Actionscript” and “Essential Actionscript 2.0” and while I understand the concepts behind it, I always get lost when trying to use it in a real world situation.
I think one of my main problems is I never know how much functionality to include within a particular class, or even what classes to break things up into.
I know there’s not a single answer to this stuff, I guess I just wanted to say I appreciate you trying to explain things in real world terms.
I’d love to see a tutorial (or a whole book) that covered an entire project done in AS 2.0 with lots of OOP. Step by step instructions and the ideas behind why it was done a particular way.
Hm, that’s a cool idea. Okay.
I was thinking of writing up my experiencing building my first real AS2.0 app, which is Showing Evidence. That way, I didn’t have to claim to be an *expert*, and could just say what I did and let people discuss among themselves :-)
What kind of topics would you want to cover?