PDA

View Full Version : LocalConnection b/t AS3 and AS2 not working



metaphist
May 20th, 2008, 03:46 PM
I get this error on my AS3:

1119: Access of possibly undefined property addTicket through a reference with static type flash.net:LocalConnection.

I'm trying to do this on the AS3:


var LC:LocalConnection = new LocalConnection();
LC.connect('myCon');
LC.addTicket = function() {
trace("whateva");
}

And this on the AS2:


var LC:LocalConnection = new LocalConnection();
traceBtn.addEventListener(MouseEvent.CLICK, traceMe);
function traceMe():void {
LC.send('myCon', 'addTicket');
}


From what I understand, when the AS2 is embedded in the AS3, it should trace when the button on the AS2 is clicked. I've tried between two AS3 movies as well and same error. I've also tried making "LC.addTicket = function ()" into just a regular "function addTicket()" but nothing. Any help?

wvxvw
May 20th, 2008, 04:59 PM
No, you have to extend LocalConnection to be able to add functions to it.

package {
import flash.net.LocalConnection;

public dynamic class MyLocalConnection extends LocalConnection {
private var _name:String = '';
public function MyLocalConnection (sName:String) {
super();
client = this;
_name = sName;
}
}
public function testFunction ():void {
super.send(_name, 'someAS2Function', 'par1', 'par2');
}
}
Or smth like this. Notice, the class is declared as dynamic, this means you will be able to add methods to it in the runtime, like you intended.

Or, more simple, set LC's client to some object that has the methods you want to call/or is dynamic (i.e. you may add any methods to it)

ActionScript Code:


var lc:LocalConnection = new LocalConnection();
var foo:Object = {myMethod:function(){trace('Hi!')}};
lc.client = foo;

Now you can call myMethod() on lc from another SWF.

metaphist
May 21st, 2008, 06:19 AM
I used the object/client method, since it was simpler. Works like a charm, thanks! I wonder why all the other info on the net about LocalConnection doesn't mention this...

wvxvw
May 22nd, 2008, 01:05 AM
Hey, thanks =)
:angel: