1
votes

hoping this is an easy enough question :)

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.

Basically inside the SWF I am working on contains just a simple string:

var theString = "theString";
trace("theString = "+theString);

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

This is my output window:

theString = theString
... in loaderComplete
loader.content = [object MainTimeline]
... getString  = [object MainTimeline]

I've searched on Stack and found similar questions, but none are exactly what I need:

tracking video files - embedding flv to swf

^ Basically what I'm trying to do as well, no answers yet

to pass variable from one swf to another swf in as3

^ sounded just like my problem, but answer was a Flex application example

pass var values from one swf to another swf who is loaded inside the firts one in AS3

^ This was close, but am not sure how to implement the chosen answer, also seems a bit more intricate then I need

Please help!

2

2 Answers

2
votes

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

0
votes

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