Tips & Tricks



Class browser

In Flash MX 2004, one thing I wished it had is a class browser. But with all the new features in this release, we can’t have everything on our wishlist all at once, can we?

Here’s a little tip on how to make your own “class browser” with the Project panel:

Because all the classes have external .as class definitions inside the folder:
[Flash installation folder]\en\First Run\Classes\, we can add these to the Project panel and browse the classes from there. Double-clicking on an .as file from this list opens up the class definition in the Script window (in the Pro version only):

ClassBrowser.gif

To save you all the work of browsing through the folders and making your own project, download it here: ClassBrowser.flp

posted on Sep 11, 2003 at 3:29 am in Tips & Tricks | 20 Comments

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:

class PretendToBeAbstractClass {
    // private constructor
    private function PretendToBeAbstractClass() {}
}

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).

posted on Sep 4, 2003 at 5:08 am in ActionScript, OOP, Tips & Tricks | 5 Comments

Singleton

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

class Singleton {
    // the only instance of this class
    private static var inst:Singleton;

    // keep count of the number of Singleton objects referenced
    private static var refCount:Number = 0;

    // constructor is private
    private function Singleton() {}

    // get an instance of the class
    public static function get instance():Singleton {
        if (inst == null) inst = new Singleton();
        ++refCount;
        return inst;
    }

    // return the number of instance references
    public static function get referenceCount():Number {
        return refCount;
    }
}


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.

posted on Sep 3, 2003 at 10:11 am in ActionScript, Design Patterns, OOP, Tips & Tricks | 5 Comments

 
Copyright © 2003-2010 Dave Yang / Quantumwave Interactive Inc.