Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  AS3 Classes Using Inheritance

AS3 Classes Using Inheritance

By Jody Hall | Published 12/27/2007 | Beginner | Rating:
Jody Hall
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.  

View all articles by Jody Hall
Introduction: Inheritance vs. Composition
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.


Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Comments
  • Comment #1 (Posted by Jeremy)
    Rating
    I have tried this with the kaymover class, but keep getting this error: 1120 Access of undefined propert keyCode.

    Here is the code:

    [code]
    package com.mysite.keyboard {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    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);
    }

    private function pressed(event:KeyboardEvent) {
    if(keyCode == Keyboard.UP) {
    uKey = true;
    }
    if(keyCode == Keyboard.DOWN) {
    dKey = true;
    }
    if(keyCode == Keyboard.LEFT) {
    lKey = true;
    }
    if(keyCode == Keyboard.RIGHT) {
    rKey = true;
    }
    }

    private function unPressed(event:KeyboardEvent) {
    if(keyCode == Keyboard.UP) {
    uKey = false;
    }
    if(keyCode == Keyboard.DOWN) {
    dKey = false;
    }
    if(keyCode == Keyboard.LEFT) {
    lKey = false;
    }
    if(keyCode == Keyboard.RIGHT) {
    rKey = false;
    }
    }

    private function run(event:Event) {
    if(uKey) {
    this.y -= speedSetter;
    }
    if(dKey) {
    this.y -= speedSetter;
    }
    if(lKey) {
    this.y -= speedSetter;
    }
    if(rKey) {
    this.y -= speedSetter;
    }
    }
    }
    }
    [/code]

    Any ideas?
     
  • 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:

    [code]
    package com.mysite.keyboard {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    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);
    }

    private function pressed(event:KeyboardEvent) {
    if(event.keyCode == Keyboard.UP) {
    uKey = true;
    }
    if(event.keyCode == Keyboard.DOWN) {
    dKey = true;
    }
    if(event.keyCode == Keyboard.LEFT) {
    lKey = true;
    }
    if(event.keyCode == Keyboard.RIGHT) {
    rKey = true;
    }
    }

    private function unPressed(event:KeyboardEvent) {
    if(event.keyCode == Keyboard.UP) {
    uKey = false;
    }
    if(event.keyCode == Keyboard.DOWN) {
    dKey = false;
    }
    if(event.keyCode == Keyboard.LEFT) {
    lKey = false;
    }
    if(event.keyCode == Keyboard.RIGHT) {
    rKey = false;
    }
    }

    private function run(event:Event) {
    if(uKey) {
    this.y -= speedSetter;
    }
    if(dKey) {
    this.y += speedSetter;
    }
    if(lKey) {
    this.x -= speedSetter;
    }
    if(rKey) {
    this.x += speedSetter;
    }
    }
    }
    }

    [/code]

    Any ideas?
     
  • 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.
     
Submit Comment



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.