OOP4AS2#1: Classes and instances

This post on classes and instances begins my little expos� on ActionScript 2.0 and Object Oriented Programming. I won’t be going into tons of detail (for that you’ll have to buy the forthcoming “FlashMX 2004 Demystified” book from Macromedia Press!), but I’ll be giving a basic introduction.

I’ll also be covering a lot of these materials at FlashParade in October, and more briefly at the next Edmonton Flash User Group meeting on September 15.

Read on for the real content… 🙂

In ActionScript 2.0, we have finally been blessed with an actual “class” keyword, which thankfully means that you no longer have to define classes as bastardized functions. Defining functions is really easy too, just create a class block, and put the methods and properties you wish to assign to the class inside its brackets:

class GThing {
// ain't nuthin but a GThing, baby.
var pureGoodness = true;
function canIGetSomeLove() {
return"love";
}
}

I won’t go into details about typing and actually defining properties and methods here – I’ll cover it separately in another post. Suffice it to say, classes are nice and easy to define, it’s a little trickier knowing where to define them…

In AS2.0, every class must be defined in an external .as file with exactly the same name as the class (ex. “GThing.as”). This class file is then placed inside of a “classPath”, the most simple of which is just the same directory as the FLA using the class.

Once you have saved the .as file, you can create instances of it easily from within your FLA the same way you do in AS1:

myGThing = new GThing();
trace("I gotst some "+myGThing.canIGetSomeLove());

FlashMX 2004 sees the reference to the GThing class, and searches it’s classPaths for a file called GThing.as. When it finds it, it imports it into the compiled SWF, and makes it available for creating instances.

Next time on AS the OOP turns… defining properties and methods with strong typing. Stay tuned.

Check out some basic OOP concepts in my Flash in the Can 2003 notes.

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

14 Comments

  1. mmmh … thanx, but it’s really just an introduction, i thought when i read your announcement last week that we’ll got something strong to learn. 🙂

    No problem, i just found 2 really good articles on http://www.person13.com/articles/

    Good continuation, and i hope u’ll give us some more next time 🙂

  2. There’s lots more to come… as I mentioned, this is serial content (ie. ongoing). I am a little bit limited in the amount of detail I can go into due to my contribution to FMX4 Demystified.

    The publisher was nice enough to let me give away some info on my blog, but I can’t give away the whole cake. 🙂

    Still, I’m striving to make it useful for everyone as I continue. Suggestions are welcome.

    Cheers.

  3. I’ve translated this posting to korean.

    thanks Grant!

    here is the link.

    http://feople.com/run/oo/view.php?id=crazymew&no=293

  4. This article have been translated in Latvian:

    thnx to !ob

    http://www.flash.lv/news/articles/article.php?id=9768

  5. http://blog.innocuo.com/index.php?p=5

    Esta página es una traducción de un artículo publicado por Grant Skinner, publicado en este blog.

    Este “post” acerca de clases e instancias comienza mi pequeña exposición sobre ActionScript 2.0 y Programación Orientada a Objetos. No entraré en m…

  6. OOP para AS2: Clases e Instancias

    Esta página es una traducción de un artículo publicado por Grant Skinner, publicado en este blog.

    Este “post” acerca de clases e instancias comienza mi pequeña exposición sobre ActionScript 2.0 y Programación Orientada a Objetos. No entraré en m…

  7. And to join all previous posters, I’ve translated the article into Dutch. Can be found over here: http://www.flashpro.nl/detail.asp?ID=133

  8. This article is now available in german!

    Check it out over here:

    http://www.urbanspice.de/blog/archives/000002.html#more

  9. Here’s the link for the 1st article translated to brazilian portuguese: http://www.gabriellaet.com/blog/index.php?p=31&more=1&c=1

    Thanks Grant!

  10. i can’t follow the translated to spanish link posted by argonauta its a dead link. Would like this article, thanks

  11. my link didn’t work either, maybe with a link in comments i could get that article argonauta.

    thanks

    http://www.fishpuertovallarta.com

  12. i want to switch my link to espano but cant figure it out?

Comments are closed.