0

I have two swf(a.swf anb b.swf) in as3 . Now I want pass variable From a.swf to b.Swf.i follow this link Tutorial i used the Loader Class.Here my Coding.

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

0

5 Answers 5

4

Check out flash.net.LocalConnection

Sign up to request clarification or add additional context in comments.

1 Comment

I up voted, because this is the right way to do it. However, in the future, try to give a little more information such as a simple example or a link to an explanation of how to use it.
1

ExternalInterface is a good approach.

Perhaps better yet would be the LocalConnection class:

http://help.adobe.com/en_US/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:

How to pass a String from a SWF into another SWF

Answered by Plastic Sturgeon:

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

2 Comments

+1 Thanks for ur kind reply, It's very useful information for me.i declare a variable in a.swf var value:String = "This is the child Value"; Now i cal the value in B.swf,Here my Coding function onLoaded(e:Event):void { trace("Child Loaded"); trace(loader.content['value']); } loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded); loader.load(new URLRequest("a.swf"));
i got error message Also TypeError: Error #1009: Cannot access a property or method of a null object reference. at textchat_fla::MainTimeline/frame1()
1
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);
    }

4 Comments

I tried this coding ,i got error like this,Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
Again Same Error Occured sir.Kindly help me.i Connected my Local Server
does the same url open in web browser?
Ya Same url Sir,Is any other Way to pass the String from one Swf to another Swf in as3?Kindly Guide me
0

You can attain better communication between SWFs using ExternalInterface object.

Look at here for adobe reference.

and take a look at following links.

Comments

0

I know this is an old topic but I recently had to reference a smartfox server instance and variables between multiple swfs. I thought i'd share the basics on how i achieved this.

ParentSWF

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.