gskinner.com: gBlog: FlashMX 2004 source code, news, reviews and opinions
Back to gBlog Main gskinner.com website: source code, portfolio, etc. contact Grant Skinner

Syndication
RSS 1.0
RSS 2.0


Subscribe
Enter your email address to be notified when posts are added.

Search

Resources
Conference Session Notes

Flash Blogs
Waxpraxis
Philterblog
W3Blog
Jonas Galvez
Josh Dura
Quasimondo
Flashguru
Sean Voisen
Colin Moock
Flazoom
Greg Burch
Pope De Flash
Peter Hall
Glyn Thomas
actionscript.com
Princess Pegg


Aggregators
Flog
FullAsAGoog
MXNA
Hall of Justhese


« Support your user group... | Back to Main | Global search and replace »

September 10, 2003


IMPORTANT NOTE: This is an old archive. It is only here to support outdated external links. To view the updated version of this archive, please go to the blog index, and search for the title of this document using the search form.

OOP4AS2#2: Class members
Posted by Grant
In installment 1, we learned how to define a class in AS2, and how to make instances of that class - if you didn't read installment 1, you should. Now, we're going to have a look at how to add members (properties and methods) to our class. We'll also have a quick look at using strict-typing.

Read on...

Throughout this, it's important to note that AS2 IS case sensitive, so mind your P's and q's.

Adding members to a class is super easy - just define variables and functions inside a class block, and they become properties and methods of that class. Those properties and methods will then be available to every instance of the class. Let's take a look:

class GThing {
   var myName = "gMan";
   function getGreeting(p_salutation) {
      return p_salutation+" "+myName;
    }
}

A couple of important things to note in the above example:
  1. You need to prefix property definitions with var.
  2. Unlike AS1 where you had to prefix property names with "this" in methods, you can now reference them directly. The default scope for methods in AS2 classes is the instance scope (ie. "this" is implied).

Now if we jump back to the FLA, and test this out, you'll (hopefully) see it all works:

myG = new GThing();
trace(myG.getGreeting("Cheers"));
// traces "Cheers gMan"

In AS2, you also have the ability to use compile-time strict-typing. Basically that means that you can ask the Flash compiler to alert you if you try to squeeze the wrong type of Object (say a Number) into the wrong type of variable (a String for instance).

To do this for properties, variables and parameters you simply tack a colon, followed by the object name after the variable name when you declare it. Defining a return type for a function is similar, just tack the colon and object name after the parameter parenthesis of the function.

Let's see what this looks like with our example above:
class GThing {
   // myName is typed to String:
   var myName:String = "gMan";
   // getGreeting returns a String
   // the parameter p_salutation is a String
   function getGreeting(p_salutation:String):String {
      return p_salutation+" "+myName;
    }
}

Then in the FLA:

// myG is a GThing
var myG:GThing = new GThing();
trace(myG.getGreeting("Cheers"));
// traces "Cheers gMan"

// We could cause a compiler error with:
var myG:GThing = new GThing();
trace(myG.getGreeting(true));
// error, type mismatch - Boolean != String

An important note on the above example is that in order to use strict typing with variables you must declare the variable with "var". You can see this in the first line of the FLA code.

Remember, the type checking is only compiler-time - at run-time an external SWF could still plug a String into your GThing typed variable (something about that sounds very wrong).

Stay tuned for more OOP goodness.

And don't forget, this is just the tip of the iceberg... to get all the juicy details, you have to lay your greasy hands on the upcoming "Flash MX 2004 Demystified" from Macromedia Press.

Posted @ 11:14 AM by Grant | TrackBack


Comments

Question relating with strict-typing:
before I used somethink like this:
function erer ()
{
for () {
...
if () return false;

}

return myObject;
}

Does such construction have sence?

Posted by: Serge at September 11, 2003 08:22 AM

function getGreeting(p_salutation:String):String {
...
}

Does the colon and datatype after the arguments parentheses indicate the function returns a string? That the function itself is a string?

Posted by: Mike Britton at September 11, 2003 01:10 PM

Serge - You can return undefined or null to end function execution without returning a value of the specified type. The easiest way to do this is by just using "return;" with no value.

Mike - Yes, it indicates the function will return a value of type String.

Posted by: Grant Skinner at September 11, 2003 02:28 PM

i've translated this too... :-)

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

Posted by: Zooil at September 14, 2003 03:24 AM

Latvian translation:
http://www.flash.lv/news/articles/article.php?id=9770

Posted by: darklow at September 17, 2003 03:13 PM

Spanish translation:
http://blog.innocuo.com/index.php?p=24&more=1&c=1&tb=1&pb=1#more24

Posted by: argonauta at September 20, 2003 04:10 PM

Translated this article into Dutch: http://www.flashpro.nl/detail.asp?ID=134

Posted by: Flash.Pro at September 22, 2003 01:09 AM

This article is now available in german!

Check it out at:
http://www.urbanspice.de/blog/archives/000004.html#more

Posted by: UrbanSpice at September 22, 2003 03:48 PM

Hosting by NetKeepers.ca | Powered by Movable Type 2.661
The text content of this blog is licensed under a Creative Commons License. Graphics are ©2003 Grant Skinner.