如何将字符串从 SWF 传递到另一个 SWF [英] How to pass a String from a SWF into another SWF
问题描述
希望这是一个足够简单的问题:)
hoping this is an easy enough question :)
一些细节:我使用的是 Flash CS5,从未接触过 Flex.此外,正在加载的 SWF 将是一个客户端 SWF,因此希望有一个可以使用简单几行代码的解决方案.
Some details: I am using Flash CS5, never touched Flex. Also the SWF that is doing the loading will be a client SWF, so hoping for a solution that could work with a simple couple of lines.
基本上在我正在处理的 SWF 中只包含一个简单的字符串:
Basically inside the SWF I am working on contains just a simple string:
var theString = "theString";
trace("theString = "+theString);
现在我一直在研究一个测试加载器 SWF,它将加载我的字符串 SWF 并以最简单的方式获取变量.有什么想法吗?以下是我当前损坏的代码:
Now I've been working on a test loader SWF that will load my String SWF and get the variable in the simplest way. Any thoughts? Below is my current broken code:
function loaderComplete(event:Event)
{
trace("... in loaderComplete");
getString = loader.content.toString();
trace("loader.content = "+loader.content);
trace("... getString = "+getString);
}
这是我的输出窗口:
theString = theString
... in loaderComplete
loader.content = [object MainTimeline]
... getString = [object MainTimeline]
<小时>
我在 Stack 上搜索并发现了类似的问题,但没有一个是我需要的:
I've searched on Stack and found similar questions, but none are exactly what I need:
^ 基本上我也在尝试做的,还没有答案
^ Basically what I'm trying to do as well, no answers yet
^ 听起来就像我的问题,但答案是一个 Flex 应用程序示例
^ sounded just like my problem, but answer was a Flex application example
将 var 值从一个 swf 传递到另一个加载在 AS3 中的第一个 swf 中的 swf
^ 这已经很接近了,但我不确定如何实现所选的答案,而且似乎比我需要的更复杂
^ This was close, but am not sure how to implement the chosen answer, also seems a bit more intricate then I need
请帮忙!
推荐答案
让我们澄清一下:1.加载器swf,我们会调用父.2. 父级加载的swf,我们将调用子级.
Let's clarify a bit: 1. The loader swf, we will call the parent. 2. The swf loaded by the parent we will call the child.
Child 包含一个字符串,并且您希望父级能够读取该字符串>所以...Child 必须为字符串定义一个公共变量.(这意味着您必须为此使用 Class 文件,因为您不能在时间轴上声明属性为 public.)
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.)
最后,父级将尝试获取该属性.您可能希望将其包装在 try/catch 中以处理不存在字符串的情况.
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.
这是一个示例子类.
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");
}
}
}
这是父加载器类:
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
这篇关于如何将字符串从 SWF 传递到另一个 SWF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!