January 6, 2008

Custom classes and inheritance in AS3, for n00bs: Build a Mass class

Defining packages and classes

The first step in writing custom classes is understanding the template format for writing them. Flash classes are written in .as files, which are then imported into .fla files for use. An .as file must also have the same name as the class it defines. In AS3, all class definitions must be organized into packages in the following format. In this example, the name of the .as file is "Mass.as", since it must be the same as the class.

 
package {
    public class Mass {
         // Class code goes here.
    }
}
 

It is fundamental to know that all code pertaining to the Mass class must be within those brackets (with some advanced exceptions). Code outside of them may generate errors.

The public keyword in the class definition allows you to call the class from outside of the package. Without it, you could only call it from within the package. If you forget this keyword you'll get an error when you try to call it from within your .fla!

Note: In the Flash help files (Programming ActionScript 3.0 > Object-oriented programming in ActionScript 3.0), you may come across examples of class code. To use this code, you may need to reformat it in the above format and then import it into an .fla.

Let's say that you want to write several related classes that should be grouped together. In such a case, you should organize them into a single specific, named package block, rather than just a basic, unnamed package block. Here, the .as file is in a folder called "physics".

 
package physics {
     public class Mass {
     }
}
 

When you name a package, you need to place it within a folder of the same name. This folder can be within another folder, which can also be within another folder, and so on. In this case, the package name must reflect this folder hierarchy, relative to the .fla file into which you're going to import the class. Here, the .as file is in a folder called "physics", the "physics" folder is in a folder called "cenizal", and the .fla file which will import this package is in the same folder as "cenizal":

 
package cenizal.physics {
     public class Mass {
     }
}
 

Another look at this hierarchy:

myFla.fla
cenizal
|| physics
|| || Mass.as

You can only define one class per .as file (excluding helper classes, but that's another topic). This means that if you decide to build more classes within the cenizal.physics package, they must all be in the "physics" folder, within the "cenizal" folder.

Pages: 1 2 3 4 5 6 7 8

 

17 Responses to “Custom classes and inheritance in AS3, for n00bs: Build a Mass class”

  1. MikeTheVike

    pretty good tutorial. I’m working on looking AS3 now. Been a little sidetracked working with loading XML files at the moment, but I still have to dive into understanding classes. Keep up the good work!

  2. samedi

    finally I have understand the super statement… thanks a lot

  3. charlie

    Really great. In depth but generally easy to understand. It doesn’t get into what Point does exactly, so that part gets foggy for me.

  4. Bob

    Amazing. I’ll subscribe on your RSS. How much time did you write a post?

  5. Great article! Really interesting stuff that will help a LOT in my AS3 exploits. Thank you for sharing. :: I would be very interested to read your teachings on garbage collection gc(), and the importance/implications of removing eventListeners immediately to help responsibly manage memory. Esp. whereas related to working with multiple video/flv files.

  6. Richard Persson

    Very good, this was just what I was looking for, ridiculous that it should be so hard to find a good tutorial with the basic syntaxes of as. classes.

  7. Thanks heaps for this tutorial, Christian.

    You may want to fix up most of the code examples though — they have formatting HTML in them which is actually appearing in the code, which makes them hard to read!

  8. CJ Cenizal

    Oh, thanks man, I didn’t even notice that! I’m glad you still found the tutorial useful. You have a nice website, by the way!

  9. If only all tutorials were as clearly written as this. Very helpful. Thanks.

  10. Charles

    Great tutorial!! As Charlie said, exposing what does the Point class would be very helpful to fully understand you constant x and y references… Maybee it’s really obvious for some but I think this tutorial is adressed to noobs :) But as you mention, I’ll go have a look with google.

    Thanks a lot for keeping it simple, I’m really tired of those tutorials where the authors put complicated bits of code to expose simple concepts just for “showing of”

    Cheers

  11. Great tutorial. I am putting together classes tonight and this helped me get there quickly.

    Thanks for sharing you knowledge with us all so that we didnt have to go through all the pain.

    Great work mate.

  12. Marco

    Very nice tutorial but what if I want to give the masses the appearance of a particular shape drawn by hand and saved as a movieclip?

  13. admin

    @Marco: Try looking into Bitmap and BitmapData. I imagine you’ll have to embed the bitmap of the shape you want to use, and use BitmapData to create multiple instances of it. Try approaching the problem one step at a time, and use Google to research your problems (can’t emphasize this one enough). Start with asking, “How to import an external image into AS3?” Once you’ve figured that out, you need to figure out how to duplicate it. Just keep asking what the next step is until you’ve reached your goal.

  14. Marco

    Thank you very much however my problem is not to import external image, I draw it within the graphic interface of Flash and then convert it to movieclip. Now how can I make this object have all the methods and properties of the mass class?

  15. zvi shalem

    thank you.

    This was very helpful for me.
    exactly the little bit of clear information I somehow did not come across, and really needed.

  16. If the .as file is within a folder(s), you must write out the entire path:

    This will throw an error:
    import cenizal.physics.ClassName.as;

    The correct way:
    import cenizal.physics.ClassName;

    You do not need to write the extension of the file. If you write the extension the flash will throw an error:”1084: Syntax error: expecting identifier before as.”

    You only need the path and the classname to import a class in flash “ex: import path.classname; ”
    where :
    -classname : is flash actionscript file name and defined class name;
    -path: is the path structure where the file is located separated by dot “.” flash will read the structure of the path automatically from the compilable file “fla” movie file.

  17. Uros

    Thank you for this!

Say something!