Working with strongly typed variables and LocalConnection in ActionScript 2

Oddly the LocalConnection class is not defined as dynamic in the ActionScript 2 intrinsic classes, even though the documentation demonstrates dynamic usage as an example of the LocalConnection class.

However, the fact that it is not declared dynamic can be turned into a positive thing and force us to use good OOP practices instead. Normally when we use LocalConnection we create an instance and then assign a function on that instance. This method of declaring functions should be avoided in AS2 as it violates several core concepts in object oriented programing.

The better way to implement LocalConnection is to create a class that extends LocalConnection and adds the functions you need in your application. That way you can strongly type the instances to your extended LocalConnection class and still benefit from all the advantages of AS2.

Here’s an example of a LocalConnection subclass we used in a recent application.


class AssetListenerConnection extends LocalConnection {

private var __sessionId:Number;

public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
private static var evnentDispatcherInitialized =

function AssetListenerConnection() {
__sessionId = (new Date()).getTime();
connect("QuestionnaireExpress_" + __sessionId);

public function get sessionId():Number {
return __sessionId;

private function newAssetQuestion(questionType:String, assetId:Number) {
dispatchEvent({ type: "assetUploaded",
questionType: questionType,
assetId: assetId});

This was used to listen for a message sent by a tiny broadcaster SWF on the results page after the user uploaded a file through an HTML pop-up form.

5 thoughts on “Working with strongly typed variables and LocalConnection in ActionScript 2

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>