Dispatching events between AS2 and AS3
Even though AS3 has been out for some time now, there are still situations when you will need to load an AS2 compiled SWF into an AS3 compiled SWF and communicate with it. A good example would be if you need to use the ActionSource component for Omniture tracking or some other AS2 based Flash component in a Flash 9 or Flex 2 project. Well, what if you could simply dispatch an event and listen for it on the other side? That's what ASBridge does.
ASBridge consists of 4 classes, ASBridge and ASBridgeEvent classes for both AS2 (cookiejar package) and AS3 (sugarcookie package). The core idea of ASBridge comes from the Adobe LiveDocs LocalConnection entry, check out the second paragraph where it says that AS 1,2, and 3 can communicate with each other using a LocalConnection, that's where the magic is. Only thing to really be aware of is the 40kb limit on the amount of data that can be passed as arguments to the send() method.
Displayed below is the source code for the ASBridge_Example class which is the main class for the AS3 Project and the AS2_ASBridge class used in the Flash 8 Fla with brief descriptions. To get all the source code including the ASBridge classes for both AS2 and AS3, download all the source as an Eclipse ActionScript 3 Project.
To use the ActionScript 3 Project zip:
- unzip file anywhere
- open Eclipse
- File -> Import, choose Existing Projects into Workspace and click next
- browse for the root of the extracted directory
- click Finish
AS2_ASBridge
Demonstrates sending and receiving events with data between AS2 and AS3 implemented as a class that is instantiated on the timeline of a Flash 8 Fla.
ASBridge consists of 4 classes, ASBridge and ASBridgeEvent classes for both AS2 (cookiejar package) and AS3 (sugarcookie package). The core idea of ASBridge comes from the Adobe LiveDocs LocalConnection entry, check out the second paragraph where it says that AS 1,2, and 3 can communicate with each other using a LocalConnection, that's where the magic is. Only thing to really be aware of is the 40kb limit on the amount of data that can be passed as arguments to the send() method.
Displayed below is the source code for the ASBridge_Example class which is the main class for the AS3 Project and the AS2_ASBridge class used in the Flash 8 Fla with brief descriptions. To get all the source code including the ASBridge classes for both AS2 and AS3, download all the source as an Eclipse ActionScript 3 Project.
To use the ActionScript 3 Project zip:
- unzip file anywhere
- open Eclipse
- File -> Import, choose Existing Projects into Workspace and click next
- browse for the root of the extracted directory
- click Finish
ASBridge_Example
Demonstrates loading a AS2 SWF into an ActionScript 3 Project generated SWF and using ASBridge to send and receive events with data between AS3 and AS2.
package { import com.sugarcookie.adapter.ASBridge; import com.sugarcookie.adapter.events.ASBridgeEvent; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; public class ASBridge_Example extends Sprite { private var _adapter:ASBridge; private var _loader:Loader; public function ASBridge_Example() { _adapter = ASBridge.getInst(); _adapter.addEventListener("onAdapterReady",onAdapterReady); _loader = new Loader(); _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete); var request:URLRequest = new URLRequest("AS2_ASBridge.swf"); _loader.load(request); addChild(_loader); } private function onLoadComplete(evt:Event):void { trace("onLoadComplete: "+evt.toString()); } private function onAdapterReady(evt:ASBridgeEvent):void { trace("successfully received event with data from AS2"); trace("onAdapterReady: "+evt.toString()); _adapter.sendEvent("onBridgeComm",{id:"test data"}); } } }
AS2_ASBridge
Demonstrates sending and receiving events with data between AS2 and AS3 implemented as a class that is instantiated on the timeline of a Flash 8 Fla.
import com.cookiejar.adapter.ASBridge; import com.cookiejar.adapter.events.ASBridgeEvent; class AS2_ASBridge { private var _adapter:ASBridge; public function AS2_ASBridge() { _adapter = ASBridge.getInst(); _adapter.addEventListener("onBridgeComm",this); _adapter.sendEvent("onAdapterReady",{status:true}); } private function onBridgeComm(evt:ASBridgeEvent):Void { trace("successfully received event with data from AS3"); } }