How to pass a String from a SWF into another SWF

风流意气都作罢 提交于 2019-12-01 12:50:23

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.

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.)

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.

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

you can access variables and code from the loaded swf. First, make sure that both the loader and the loaded content use the same AVM, this means that both project use the same version of language (both as3 or both as2)

Thank in the loader complete handler:

loaderComplete(evt:Event):void{
    var mc:MovieClip = loader.content as MovieClip;
    if(!mc){
        trace("Error loading");
        return;
    }
    trace("Your string: " + mc.theString);
}

I haven't tested this code but it should works

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