Category Archives: ActionScript

Updated Class Browser with hidden classes

The “class browser” has been updated – it now includes hidden class files for data connectivity.

Unless you add these hidden files to the actual paths, you’ll see a little yellow triangle (see diagram) and won’t be able to double-click and see the class definitions. However, they’re still useful as a quick lookup of the class paths for entering into your code.

Note that the Include folder contains files that are part of Flash Remoting Components for Flash MX 2004.

For non-English installations, you may have to change the class paths in this .flp file. Open it up in a text editor, Find & Replace words like “/Program Files/” and “/en/” to your OS settings.

You can download the updated file here.

Inheritance in ActionScript 2.0

Back in the good old days of Flash 5 and MX, trying to do inheritance in ActionScript required some knowledge of what goes on behind the scene. Also, the infamous superclass invocation bug haunted developers for almost three years.

Now in ActionScript 2.0, with the new OOP-specific keywords, inheritance is much simpler:

That’s all. And the keyword “super()” in the Child class constructor is not even mandatory because the compiler inserts it for you!

As for the superclass invocation bug, it is now fix for Flash player 7!

Code hints

In Flash MX 2004, some of the component code hints are not listed in AsCodeHints.xml or UIComponents.xml. As a result, what used to be a common thing to do in MX by using code hints such as _cb for ComboBox, does not work now.

Well, there are two solutions:

1) Add you own code hints information to the two files mentioned above.

2) Reference the component class in your code:

var sp:mx.containers.ScrollPane;

Now when you type sp. the list of ScrollPane methods and property is displayed!

Thanks go to Rebecca Sun of Macromedia for this tip.

ActionScript 2.0

Here are some of the new features in ActionScript 2.0, and why one would want to use it instead of ActionScript 1.0 (yes, you can still use the older syntax in Flash MX 2004).

ActionScript 2.0 is not a new language. It is an enhancement to the previous version, by building on the existing language. In fact, Flash MX 2004 compiles AS2 into AS1 equivalent bytecodes. One reason for this is backwards compatibility in Flash Player 6. As a result, one can code in AS2 and compile it for Flash Player 6. For better optimization, target for Flash Player 6.0.65.0 or higher. Some version 2 components won’t run except in FP7, but some can run in 6.0.79.0 as well.

In terms of building applications, ActionScript 1.0 (and ECMAScript 3 in general) requires getting intimate with something called the prototype (and __proto__ if you’re a rebel trying to avoid unwanted side effects). The reason being that there was no ‘class’ construct, and inheritance was done in a very odd way.

Now in ActionScript 2.0, it is more suitable for OOP because of the following new keywords and features:

class, interface, extends, implements, private, public, dynamic, instrinsic, static, get, set, import; class path (similar to package), and strict data typing (useful in locating problems at compile time).

With these new features in ActionScript 2.0, building robust applications is becoming more attractive, and in a way easier. However, because Flash MX 2004 does not care if AS1 or AS2 is used (in most cases), one can still continue using AS1 and slowly migrate over to AS2.

abstract class

Since ActionScript 2.0 does not have the “abstract” modifier, here’s one way to create a class that acts like an abstract class, by making the constructor private:

When the following statement is compiled, the compiler will complain that one can’t instantiate from this class because the constructor is private:

var o:PretendToBeAbstractClass = new PretendToBeAbstractClass();

Although the constructor is private, but because it behaves as protected, you can still extend this class:

class MyClass extends PretendToBeAbstractClass {
}

What is missing is the compiler does not actually know what an abstract class is; therefore there won’t be any warning messages.

As I mentioned in the last post, you can get around all the type-checking at runtime, and even instantiating an “abstract” class at runtime like this:

var o = new _global["PretendToBeAbstractClass"]();

Or even access “private” properties from it:

trace(o.somePrivateVar);

Obviously these actions defeat the purpose of strict-typing, but it is possible (until runtime type-checking is implemented).

Singleton

Here’s one way to implement a Singleton in ActionScript 2.0:


The count property is for keeping count of the number of Singleton objects referenced (obviously it is not aware of destroyed objects), and is not really a necessary member of the Singleton pattern.

To use this class, one would write something like this:

var s1:Singleton = Singleton.instance;
var s2:Singleton = Singleton.instance;

Now s1 and s2 are the same instance of the Singleton class.

private, protected & public

In ActionScript 2.0, there are “private” and “public” modifiers, but there is no “protected”. However, “private” behaves like “protected” – i.e. subclasses and instances can access private members; so there is no true private scope.

To make sure the compiler catches access to private properties/functions, strict-typing has to be used:

Because type-checking is only performed at compile time, there is no guarantee that private members cannot be accessed at runtime.

By default, if the “public” modifier is left out, the member is assumed to be public.