Local Connection as2 to as3, and back
Posted by eric | Filed under Lab Work, Programming
It’s been a while because things are so crazy busy. The last few weeks has been all about papervision. 3D is totally new to me. The terminology, 3D vertices, Matrix, etc…it’s deep…literally. Anyway, today’s post is not about that. I was tasked with importing the youtube chromeless player into papervision. This meant i had to also create the player controls. However, before doing anything, i needed to get these guys to talk with each other. Getting as3 to talk to as2 was no problem. A new local connection object, set a connection ‘frequency’ (using the connect method) get’s the one way communication all set up. Going the other way (from as2 to as3) was as simple, just not as intuitive. It comes down to the following key points:
- each swf needs two local connection objects – one to send, one to receive. Both objects will have unique connection names. However, all connection names must be shared in each swf.
- the as3 swf uses a ‘client’ (a generic object or custom class) to receive the events from as2. -ie…the callback. that was the key for me
in all, here is a generic sample (source)
as2:
var lcGet:LocalConnection = new LocalConnection() lcGet.connect('as2bridge') var lcSend:LocalConnection = new LocalConnection() lcGet.command = function(method:String,args:Array) { trace('command received'+method+" "+args); var ob:Object = this ob[method].apply(this,args[0]); textbox.text = args; lcSend.send('fromAs2','callingBackToAs3','yipeeeee!') } function write(msg:String) { textbox.text = msg; } textbox.text = ' i am the as2 swf';
as3 class
package com.loomisgroup { import flash.display.*; import flash.events.*; import flash.net.*; public class AS3Bridger extends MovieClip { public var ld:Loader; public var lcGet:LocalConnection; public var lcRec:LocalConnection; public var rec:LocalConnection; public function AS3Bridger():void { lcGet = new LocalConnection() ; lcGet.addEventListener(StatusEvent.STATUS,onStatus) lcRec = new LocalConnection() lcRec.connect('fromAs2'); var myClient = new Object(); lcRec.client = myClient; myClient.callingBackToAs3 = function (e){trace(e,'is the call back from as2')}; var req:URLRequest = new URLRequest(); req.url = 'as2.swf'; ld = new Loader(); ld.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded); ld.load(req); addChild(ld); } public function onStatus(e:StatusEvent):void { trace('calling back now') } public function loaded(e:Event):void { var m = this.getChildByName('mc') m.addEventListener(MouseEvent.CLICK,cl)</code> } private function cl(e:MouseEvent) { lcGet.send('as2bridge','command','write','and now it is as3'); } } }
Tags: Actionscript 2, Actionscript 3, Flash, LocalConnection
2 Responses to “Local Connection as2 to as3, and back”
-
Mortimer Says:
June 12th, 2009 at 9:36 amHi
(First: Interesting Blog. I’ve worked with PV3D too some Month ago. Maybe I should follow your Blog from now on
)Thanks for the source. I’m struggling with this Problem since 2 week now. I’ve used the SWFBridge by gSkinner and also tryed the ASBridge (ASB) by jumpeyecomponents (Havnt yet the time to look into FlashInterface by flashextensions.com).
gSkinners class works but got serveral limitations and bugs (e.g. You can only load a AVM1-SWF once and then never again). The ASB-Method is for Single-Use-Only (because you have to add a Component onto your stage and setting up static path to the AVM1-SWF). Both not what I’m looking for.I’m currently stuck in the problem to load into an AVM2-SWF an AVM1-SWF wich must be loaded and unloaded dynamicly and has to communicate in both directions. It sound like you might solved this problem, but I’m not quite sure, because the Problem is (at the gSkinner class as well), that the Connection Name has to be know by both SWF’s.
I give your Code a try and say thanks inb4. Hint how a semi-AS3-freshman can solve this problem are welcome =)
Greetings from Germany
-
eric Says:
July 16th, 2009 at 8:48 amMortimer:
I’m currently stuck in the problem to load into an AVM2-SWF an AVM1-SWF wich must be loaded and unloaded dynamicly and has to communicate in both directions
yeah, this one has had me tied up too. We did a piece that had and as3 app, with an as2 videoplayer (long story). If you navigated to that piece with two browser windows, you could actually choose a video from window B and see it play in window A. The local connection strings are the same for both instances, and for that reason only one gets priority. We will have to think of a solution (maybe using a serverside script???).