Here’s a handy bit of code. Although every release of the Flash Player provides full backward compatibility in viewing published content, the introduction of ActionScript 3.0 and the new Actionscript Virtual Machine (AVM2) in Flash Player 9 presented some challenges for interoperability at author time, specifically switching between code and SWFs of the different Actionscript versions.

Combining Actionscript 3.0 code with previous language versions can’t be achieved as easily as the old school hacks of plugging together bits of Actionscript 2.0 or 1.0 wherever they fit  - but that’s half the intention, I’m sure.

It’s fully documented at Flash LiveDocs, but now, essentially:

  • A single SWF file cannot combine ActionScript 1.0 or 2.0 code with ActionScript 3.0 code.
  • ActionScript 3.0 code can load a SWF file written in ActionScript 1.0 or 2.0, but it cannot access its variables or functions.
  • SWF files written in ActionScript 1.0 or 2.0 cannot load SWF files written in ActionScript 3.0 (without a couple fiddly exceptions).
     

Generally, avoid it – it’s bad practice anyway. But if you have no other option – or, as I recently encountered (thus this post), your designer inevitably sends you an AS2 SWF for your AS3 project ;) – you can use the LocalConnection class to create a simple interface between the two files that actually also gives you quite an amount of scope for interoperability as and when desired.

If you’ve not used it before, the LocalConnection class facilitates communication between LocalConnection objects instantiated within the same or multiple SWF files. With that ‘access’, you can invoke any amount of methods on connected clients as you can write. The communication exists within a specified domain and all clients must be on the same computer, but it’s not limited only to the Flash player. SWFs can communicate within a browser sandbox, or from browser to standalone SWF viewer, or to a projector etc etc – and anything in between.

For this example, we establish an AS3 SWF as our sending file, calling methods within an AS2 SWF receiving file, which handles the call and invokes the methods.

In the AS3 (sending) file:

var localConnection:LocalConnection = new LocalConnection();
localConnection.addEventListener(StatusEvent.STATUS, onStatus);
localConnection.send(“testConnection”, “connectionHandler”, “Boo.”);

function onStatus(event:StatusEvent):void {
switch (event.level) {
case “status” :
trace (“LocalConnection.send() succeeded”);
break;
case “error” :
trace (“LocalConnection.send() failed”);
break;
}
}

In the AS2 (receiving) file:

var localConnection:LocalConnection = new LocalConnection();
localConnection.connect(“testConnection”);

localConnection.connectionHandler = function(string:String):Void {
trace (“Message from AS3 SWF: ” + string);
}

Easy peasy. You can send any amount of arguments and call any method on the listening SWF – so long as there’s a handler to deal with it.

Roll down the window and let the wind blow back your hair.