ActionScript 2 Class Caching Problem

I’ve been using the following method of instantiating classes for a few years now. It’s never proven a problem until recently. I’ll start off by detailing the circumstances that lead to me using this setup. I write about 95% of my code in external .as files. I store all of these in a directory called Source within each projects main directory. Inside Source I’ll create sub directories which lets me package the various classes. For the classes that require actual drawing on Stage I create an empty MovieClip in the library and give it an instance name of classClip. Each time I want to instantiate one of my MovieClip based classes I associate that clip with the class and then create an instance of it on Stage.

This is achieved with the following code:

Object.registerClass("classClip", misc.HelloWorld);
this.attachMovie("classClip", "hello_world_app", 0, {_x:50, _y:100}).init();
Object.registerClass("classClip", null);

The above associates the classClip MovieClip with the file stored in the misc directory, within Source. It then creates an instance called hello_world_app with a depth of 0 at x:50, y:100 and calls an init function I wrote inside the class. Afterwards we unassociate the class from classClip so that we don’t accidentally create more misc.HelloWorld instances later on (amongst other things).

I use an init function instead of a constructor since I can control when the class gets initialised. I can create an instance of it at program startup but leave actually initialising it till later on, that’s handy when you want to perform some other actions first, say class A depends on something class B has to do during its initialisation. Another handy thing with this is that you can re-initialise an instance later on.

A small hangup with this setup is that it takes three lines of code to initialise a single class. It’s not to bad if you’re creating a series of the same class say in a loop like so:

Object.registerClass("classClip", misc.HelloWorld);
for(var i:Number = 0; i < 100; i++)
this.attachMovie("classClip", "hello_world_" + i, i, {_x:50, _y:i * 20}).init();
Object.registerClass("classClip", null);

Recently this system has started to cause me serious problems in my most recent projects. It seems under certain situations some classes are not being recognised. A very simplified example would be when I have a MovieClip in the library with a single button. I create an instance of that MovieClip inside my misc.HelloWorld class called Panel. Inside this MovieClip I have drawn a design time button and given it the instance name “PauseButton”. To keep it in perspective the path to that would be:


The problem is that inside either the constructor and/or the init function I cannot access PauseButton at all. It traces as undefined. If I associate a class with PauseButton that does not get loaded. This effectively means I have no way of controlling what PauseButton does besides writing inline code on a keyframe.

There is a simple fix for all of this although the ease of implementing it depends from project to project. As it stands I’ll probably be doing this for all new projects until I switch up to ActionScript 3. The fix is to add the Class Name to the linkage panel for each library object. It’s just under the Linkage Name textbox. So in there I would write misc.HelloWorld. Then my three lines of code would become:

this.attachMovie("HelloWorld", "hello_world_app", 0, {_x:50, _y:100}).init();

It might not seem like that big a deal but it can prove to be a real pain trying to find the objects that use a certain class. Before you could just look inside a single init function and find a long list of clip creations. The other downside is that you might end up with 50+ empty MovieClips in the library whereas before you had just one. That later situation is what I now find myself in.

PS. another slightly similar issue is Flash Hates The Future.

Comments: None so the first!

Leave a reply

Your email address will not be published. Required fields are marked *

You serve yourselves best, when you serve me.

— Darth Sidious, Cloak of Deception