简体   繁体   中英

How to pass variable From .swf File to .as file?

I have a main in .as and I load .swf in my .as.It's Working fine,Now I want the take variable From .swf and Pass it Into .as. How can i do this? pls. help! My .swf file coding

function formatMessage(chatData:Object)
{
var number:uint = chatData.user;
trace(chatData.user);
}
private function addText()
    {
_loader = new Loader();
_loader.x=10;
_loader.y=200;
_loader.load(new URLRequest("receive.swf"));//Here i Can Load the Swf.Here how Can i acces  the Variable of number ?
 addChild(_loader);
    }

First thing to keep in mind is that this will not work across ActionScript VM versions. Meaning only AVM2 and AVM2 can make communication (it is possible between AVM1, but takes more ingenuity).

Add a completion event listener to the _loader.contentLoaderInfo property called onLoad

_loader.x=10;
_loader.y=200;
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);

Now with the onLoad function will need to extract the loaded content and a little casting.

function onLoad(evt:Event):void {
    var target:DisplayObject = LoaderInfo(evt.target).content as DisplayObject; // cast as DisplayObject
    trace("readMe = ", target["your_variable"]); // trace output "your_variable"
}

There are cleaner forms of getting this done, but this will allow you extract the value from the nested SWF.

DisplayObjectContainer(_loader.content).getChildByName("someName");

someName variable must be public, declared in main time line, or main file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM