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

放肆的年华 提交于 2019-12-21 02:59:07

问题


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);
    }

回答1:


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.




回答2:


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

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



来源:https://stackoverflow.com/questions/7964347/how-to-pass-variable-from-swf-file-to-as-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!