My interest in Flash started mostly because of a Jib-Jab cartoon ("This Land") in 2004. I'm the author of a feature I call "Mazoons," which are a combination of mazes and cartoons. In 2002, I even had a book published, "Super Silly Mazes." I'm not a professional programmer, but making my mazes interactive by programming them with Flash has become a hobby/obsession of mine in recent years. My efforts so far can be seen at http://www.mazoons.com/Flash_mazes.htm.
In my previous two tutorials, I had you create some custom classes that controlled MovieClips that were stage instances. These stage instances were given instance names in the fla, and the instance name was sent to the class as an argument.
You might have wondered why I would have you create a class that has a MovieClip as a property, instead of just creating a class that is a MovieClip. The first technique is called "composition" and the second "inheritance." This is not to imply that this only applies to MovieClips, it's just that MovieClips make a great, easy to understand example of it. So, the difference between composition and inheritance (in the case of MovieClips) is the difference between whether an object HAS A MovieClip as a property, or whether the object IS A MovieClip. So the former two tutorials were both examples of the first technique, composition, even though I didn't tell you that at the time.
I did it this way for several reasons: First, I knew that a lot of people would already be familiar with drawing something on the stage, converting it to a symbol, and giving it an instance name. That being the case, I could take you from what you already knew, and then go into how to use a class to control that instance. Secondly, I wanted you to realize that although a MovieClip is an object, certainly not all objects are MovieClips. An object is basically a custom datatype, and can be composed of anything you want, even instances of other classes. So, it was to get you to start thinking in terms of objects, but not to confuse objects with MovieClips. And the final reason: composition is a valid technique, and it allowed me to demonstrate at the end of the first tutorial that you could control the same MovieClip instance using two different classes.
I'd also like to clarify something here. I don't have an OOP background, don't know design patterns or anything (yet, haha!), and am really just getting started with classes myself. I had just begun to get into using classes in AS2, just before AS3 came out. I don't do flash development for a living, rather it's a hobby of mine at the moment. However, since I'm basically a beginner with OOP and AS3 myself, and I've had to dig to find out a lot of things, I feel that rather than disqualifying me, this actually helps me to better relate these things to other beginners. While I can't bring you past whatever level I'm at, what I can do is explain the things that I've learned in terms you can understand, because I've experienced it too, and not all that long ago. Sound good? Read on.
The next page will explain how to use the other technique, inheritance, to link a MovieClip in the library to a class file.
public class KeyInheriter extends MovieClip {
private var uKey:Boolean;
private var dKey:Boolean;
private var lKey:Boolean;
private var rKey:Boolean;
private var speedSetter:uint = 5;
public function KeyInheriter() {
stage.addEventListener(Event.ENTER_FRAME, run);
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, unPressed);
}
Comment #2
(Posted by Jeremy - jermiester2 at juno.com) Rating
Great tutorial! I tried to apply this to the KeyMover class, but I keep getting the null error (even though it works). I tried adding the ADDED_TO_STAGE command, but to no avail. Here's the code I worked up:
public class KeyInheriter extends MovieClip {
private var uKey:Boolean;
private var dKey:Boolean;
private var lKey:Boolean;
private var rKey:Boolean;
private var speedSetter:uint = 5;
public function KeyInheriter() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressed);
stage.addEventListener(KeyboardEvent.KEY_UP, unPressed);
stage.addEventListener(Event.ENTER_FRAME, run);
}
Comment #3
(Posted by Scott - swood03 at jcu.edu) Rating
Jody,
Excellent tutorial (impressive that you are new to ActionScript...I wish I picked it up that quickly!).
Here's my question:
At the end of your tutorial you state that you can't think of a reason why you would manually want to create the Circle and Square classes. Well I have a need (I think) and it is driving me crazy trying to figure it out.
What if there is something unique about the Circle that the Square doesn't have? So I think you would need to define a Circle class and define those items there instead of using the generated Flash Circle class. Well I thought this would be simple. Just put Circle as the Class and use the ClipDragger as the Base Class (ClipDragger would still have to extend the MovieClip class). I can not get it to work at all.
This is the error I get if I try to create my own Circle class:
5000: The class 'Circle' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.