[英]How to pass variable From one Swf to another Swf in ActionScript3?
I have two swf(a.swf anb b.swf) in as3 . 我在as3中有两个swf(a.swf anb b.swf)。 Now I want pass variable From a.swf to b.Swf.i follow this link Tutorial i used the Loader Class.Here my Coding. 现在我想要将变量从a.swf传递到b.Swf.i,请点击此链接教程我使用了Loader类。
var loader:Loader = new Loader();
loader.load("a.swf?test=hi");
But i got Error Message 但是我收到错误消息
TypeError: Error #1034: Type Coercion failed: cannot convert "textchat.swf?test=hi" to flash.net.URLRequest. TypeError:错误#1034:类型强制转换失败:无法将“ textchat.swf?test = hi”转换为flash.net.URLRequest。 at BasicTickerSprite_fla::MainTimeline/frame1() 在BasicTickerSprite_fla :: MainTimeline / frame1()
How Can i rectify this problem?How Can i pass a String From one Swf to another Swf? 如何解决此问题?如何将字符串从一个瑞士法郎传递给另一瑞士法郎? Anyone Kindly Explain me in Detail Thaks in Advance 有人请详细向我解释一下
ExternalInterface is a good approach. ExternalInterface是一个很好的方法。
Perhaps better yet would be the LocalConnection class: 也许更好的方法是LocalConnection类:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/LocalConnection.html http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/net/LocalConnection.html
Maybe a good start to understand basic concepts is from this answer provided here at Stack Overflow: 可能是从Stack Overflow此处提供的答案中了解基本概念的一个好的开始:
How to pass a String from a SWF into another SWF 如何将字符串从SWF传递到另一个SWF
Answered by Plastic Sturgeon: 由Plastic鱼回答:
Let's clarify a bit: 1. The loader swf, we will call the parent. 让我们澄清一下:1.加载程序swf,我们将调用父级。 2. The swf loaded by the parent we will call the child. 2.由父母加载的瑞士法郎称为孩子。
The Child contains a string, and you want the parent to be able to read that string> So... The Child must define a public variable for the string. 子级包含一个字符串,并且您希望父级能够读取该字符串>所以...子级必须为该字符串定义一个公共变量。 (This means you have to use a Class file for it, since you cannot declare a property public on the timeline.) (这意味着您必须为此使用Class文件,因为您无法在时间轴上声明公共属性。)
Finally, the parent will try and get that property. 最后,父级将尝试获取该属性。 You may want to wrap that in a try/catch to handle cases where the string will not be present. 您可能希望将其包装在try / catch中,以处理字符串不存在的情况。
Here is an example Child Class. 这是一个子类示例。
package { import flash.display.Sprite; /** * ... * @author Zach Foley */ public class Child extends Sprite { public var value:String = "This is the child Value"; public function Child() { trace("Child Loaded"); } } }
And here is the parent loader class: 这是父加载器类:
package { import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; /** * ... * @author Zach Foley */ public class Parent extends Sprite { private var loader:Loader; public function Parent() { trace("PArent Init"); loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded); loader.load(new URLRequest("child.swf")); } private function onLoaded(e:Event):void { trace("Child Loaded"); trace(loader.content['value']); } } }
The Output will be: PArent Init Child Loaded Child Loaded This is the child Value 输出将是:PArent Init加载的子项加载的子项这是子项值
var loader:Loader = new Loader();
loader.load(new URLRequest("http//localhost/a.swf?test=hi"));
you'll need a local server for it 您需要一个本地服务器
update : 更新 :
private var _ldr:Loader;
private var _mc:MovieClip;
public function Main():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
_ldr = new Loader();
_ldr.contentLoaderInfo.addEventListener(Event.INIT, onInit);
_ldr.load(new URLRequest('a.swf'));
}
private function onInit(e:Event):void{
_mc = _ldr.content as MovieClip;
_mc['test'] = 'hi';
addChild(_mc);
}
I know this is an old topic but I recently had to reference a smartfox server instance and variables between multiple swfs. 我知道这是一个老话题,但是最近我不得不引用一个smartfox服务器实例和多个swf之间的变量。 I thought i'd share the basics on how i achieved this. 我以为我会分享如何实现这一目标的基础知识。
ParentSWF 父母SWF
public var MyVar:String = "Hello";
public var sfs:SmartFox = new SmartFox(true);
private var ChildMC:MovieClip = new MovieClip();
private var myLoader:Loader;
private function LoadChildSWF(evt:Event):void //i used button click event to load swf
{
myLoader = new Loader(); // create a new instance of the Loader class
var url:URLRequest = new URLRequest("YOURSWF.swf"); // in this case both SWFs are in the same folder
try
{
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,FFLoaded);
}
catch (error:Error)
{
trace("Unable to load requested document.");
}
}
function FFLoaded(e:Event):void
{
ChildMC= myLoader.content as MovieClip;
addChild(ChildMC);
ChildMC.init(this);
}
public function RemoveChildMC():void
{
removeChild(ChildMC);
ChildMC=null;
}
ChildSWF 儿童SWF
package
{
import com.smartfoxserver.v2.SmartFox;
public class ChildDocumentClass extends MovieClip
{
private var refDocument:*;
private var ChildVar:String;
private var sfs:SmartFox;
public function ChildDocumentClass()
{
trace("Initilise ChildDocumentClass");
// Nothing to do
}
public function init(refDoc:*):void
{
// Get the references to the Document Class
refDocument = refDoc;
// Get the references to the Server Instance
sfs=refDocument.sfs;
//Pass Parent Variable to child variable
ChildVar=refDocument.MyVar;
}
//call to parent class function example to remove loaded swf
private function UnloadThisSWF():void
{
refDocument.RemoveChildMC();
}
}
}
As you can see communication can be achieved by passing the parent class to the child class through the init(refDoc:*) function. 如您所见,可以通过init(refDoc:*)函数将父类传递给子类来实现通信。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.