views:

11129

answers:

6

I've got an AS3 SWF that I'm going to be loading other SWFs into. These child SWFs all take a single parameter on the URL. I can't seem to get it working when loading an AS2 child, and it needs to be able to handle both.

so I have

var request:URLRequest = new URLRequest();
var loader:URLLoader = new URLLoader();

request.url = "http://domain/as2.swf?param=foo";
loader.load(request);
// etc on to the eventListeners, addChild, etc

When the as2 SWF gets loaded, it can't see the parameter I've passed to it. It's looking for _root.param. Am I doing this wrong or am I attempting the impossible?

EDIT: I should add that I can load a SWF with those URL params from an AS2 loader and it works just fine.

A: 

You are doing it wrong.

"http://domain/as2.swf?param=foo"

Is a request for the file named as2.swf, on the server named domain. Any ?param=foo parameters that are part of that http request are lost when the request is complete. If the server needed to do something according to these variables, it would, but you are asking a .swf file to detect these variables, that's just silly.

Put a variable in your Global object (Global namespace) for the flash player, then when the as2 .swf is loaded into that flash player it will have access to the variable you set in your Global object.

I am not proficient in as2, but in as3, the Global object can be accessed with the this keyword, at the package level (probly is the same for as2, just dont worry about setting it at a package level).

ForYourOwnGood
If I'm not completely mistaken all parameters are available in the _root in as2.
Simon Groenewolt
Also, as far as I know: 'this' refers to the current object, not to a/the Global object.
Simon Groenewolt
this is the current object when you are in global scope.
ForYourOwnGood
both as2 and as3 SWFs can receive vars passed to them in the URL like this. in AS2 it's done with _root.varName and in AS3 it's with LoaderInfo.
nerdabilly
+3  A: 

It's not trivial to communicate between AS2 and AS3 since they run in different virtual machines. Check this http://www.gskinner.com/blog/archives/2007/07/swfbridge_easie.html for some hints.

Edit: If you cannot change the loaded as2 content your only options is creating a 'wrapper' as2 loader that uses the linked example above to communicate with the as3 and interfaces with the loaded as2 content using _root.varname This is not pretty but it might just work.

Simon Groenewolt
unfortunately, that's not an option since I mostly wont have control over authoring the SWFs that are being loaded (banner ads).
nerdabilly
Hmm, I guess in that case you are out of luck -- Best I can think of is creating an extra 'wrapper' as2 loader that uses the linked example above to communicate with the as3 and interfaces with the loaded as2 content using _root.varname
Simon Groenewolt
thats actually what I ended up doing! thanks.
nerdabilly
A: 

When you're loading a SWF, shouldn't you use the Loader class instead of the URLLoader?

I don't think the SWF actually runs at all when loaded with the URLLoader class.

Kristian J.
Doh. I am using the loader. I typed this question in a hurry.
nerdabilly
A: 

It might be worth trying to assign the variables dynamically after the SWF has loaded but before you add it to the stage. Ie.

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, movieLoaded);

function movieLoadedHandler(event : Event) : void
{
    var loaderInfo : LoaderInfo = event.target as LoaderInfo;
    var clip : DisplayObject = loaderInfo.content;

    for each(var prop in varsToTransfer)
    {
        clip[prop] = varsToTransfer[prop];
    }

    // add to parent
}

Let me know how that goes.

Richard Szalay
tried that and unfortunately it was either "prop does not exist" errors or nothing would happen at all. I ended up making a wrapper SWF.
nerdabilly
A: 

AS3 -> AS3

Movie 1(www.domain1.com): Load the external movie when click a "buy" button...

buy.addEventListener(MouseEvent.CLICK,function(){                   
    var ldr:Loader = new Loader();
    var url:String = "http://www.domain2.com/movie.swf?a=b&c=d";
    var urlReq:URLRequest = new URLRequest(url);
    ldr.load(urlReq);
    addChild(ldr);
    });

Movie 2(http://www.domain2.com/movie.swf):

var mc:MovieClip = this as MovieClip;
var ldi:LoaderInfo = mc.loaderInfo;
var lobj:Object = ldi.parameters as Object;

for (var l in lobj) {
    dumper.htmlText += l+" => "+lobj[l]+"<br />";
}

"dumper" is the name of the Dynamic Textbox field located in Movie2. The output should look like:

a => b
c => d
sitemap
A: 

@nerdabilly.. what is the your approach in solving this problem. I am in a similar situation. What do you mean by an AS2 wrapper?

I would have my AS3 SWF load an AS2 SWF that I created, which would implement the SWFBridge library linked above. That would be the "wrapper" SWF. This AS2 SWF can then communicate with other loaded AS2 SWFs, and also communicate with the main AS3 SWF via SWFBridge. It would serve as a proxy because a loaded AS2 SWF with SWFBridge can communicate with both the main AS3 and the loaded AS2. There are limitations to this and it's not foolproof but so far its the best available solution.
nerdabilly