Dispatching events between AS3 and AS2

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. What if you could simply dispatch an event and listen for it on the other side? That’s what sugarcookie’s ASBridge does. Let’s see how it all works.

ASBridge consists of 2 classes, ASBridge classes for both AS2 and AS3. 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.

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 sugarcookie.adapter.ASBridge;
    import sugarcookie.events.GenericEvent;
 
    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:GenericEvent):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 sugarcookie.adapter.ASBridge;
import sugarcookie.events.Event;
 
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:Event):Void {
        trace("successfully received event with data from AS3");
    }
}

2 Responses to “Dispatching events between AS3 and AS2”

  1. chris says:

    Add the swc file to the Library Path in Publish Settings -> ActionScript Settings. Then your import should work correctly.

  2. Jim Huang says:

    Hi,
    I downloaded sugarcookie-framework_build_0.4.4.swc but how do I install it on flash CS3?

    My code in flash CS3:

    import sugarcookie.adapters.ASBridge;
    _adapter = ASBridge.getInst();
    trace(_adapter);
    _adapter.sendEvent(“onAdapterReady”,{status:true});

    _alapter always undefined

    Best Regards,
    Jim Huang

Leave a Reply