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'); } } }
Hi
(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
Hi!
Most blog post that discuss Local Connection between as2 and as3 deals with as3 files executing actions in the as2 file.
Your tutorial is the only one that shows interaction the other way.
My problem is that I’m not able to change your generic sample code to my needs.
What I would like to do is this:
My as3 contains a movie clip ‘modul_container’, that loads the as2_test.swf.
The as2_test.swf contains a botton.
I would like a click on this button to change the swf loaded in the as3 movie clip ‘modul_container’ (load ‘another_as3.swf’).
The as3 file code:
var ld:Loader = new Loader()
ld.load(new URLRequest(“as2_test.swf”));
modul_container.addChild(ld)
The as2 file (“as2_test.swf”) button code:
on (release, releaseOutside) {
“new code executing this: load ‘another_as3.swf’ into modul_container in the parent as3 file”
trace(“does this work?”);
}
I hope you or someone else “out there” can help me with this code problem.
@Hilmar- in your as3 you will need the LC’s client to listen for a method that is coming from your as2 LocalConnection. SInce you are putting the code directly on the button, be sure your LocalConnection instantiations are occurring before the buttons themselves (ie: first frame)… also be sure your buttons are getting the appropriate LC connection names.
Given the frame work in that source code from this post, you can add that new method to the lcSend connector:
in your as3 just have the receiver’s (lcRec) client listen for that method:
let me know if that helps….we JUST had to to the exact same thing from as2 on a site we worked on. The only difference in you scenario is that you will have code directly on your buttons, which may or may not have an effect. good luck!
e
At last!!! I’ve been asked to include a load of AS3 games into an old AS2 site. As the budget is zero we’ve been stuck for a few months trying to figure this out. This should allow us to pop the games into a new browser and carry the user’s ‘logged on’ credientials. Thanks a lot!!!
Andy
Cool….hope it helps you out!
Hi Eric – I think I’m 50% of the way there – but really I’d like to parse just the variables from the AS2 home site into the AS3 games. Is this possible?(i.e. I dont want to send the whole AS2 site – only the user name (i.e. a string variable). NB: Before finding this I’d tried sharedObjects (AS2 and AS3 arnt compatible) and POST variables etc… (just so you know I’d tried
Hi Eric – is it possible to use localconnection to send variables from AS2 to AS3? I only want to parse the users name and status (i.e. logged on) and not the entires swf file?
Thanks
Mortimer:
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???).