09
Jul
04

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.


import mx.events.EventDispatcher;

class AssetListenerConnection extends LocalConnection {

private var __sessionId:Number;

public var addEventListener:Function;
public var removeEventListener:Function;
public var dispatchEvent:Function;
private static var evnentDispatcherInitialized =
EventDispatcher.initialize(AssetListenerConnection.prototype);

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 Responses to “Working with strongly typed variables and LocalConnection in ActionScript 2”


  1. 1 global home loan Nov 2nd, 2004 at 11:08 am

    Hello there,

    Iwas browsing the web and found this blog. Some interesting quotes. Keep them coming!

    Alice
    global home loan

  2. 2 ambien Nov 24th, 2004 at 5:14 pm

    hey, nice site, perhaps youd like to check out mine: ambien

  3. 3 Bellame Jan 22nd, 2007 at 6:21 pm
  4. 4 Caiek Feb 3rd, 2007 at 8:06 am

    So much spam (

  5. 5 CXZa Mar 24th, 2007 at 11:41 pm

    Looks nice

Comments are currently closed.