how do I pass variables to loaded swf

I have a movie A.swf with nothing but a preloader, that loads a movie B.swf.
How do I pass variables to movie B.swf from movie A.swf.
I want to pass some flashvars, but for a start I would just like to see an example of passing some simple info to B.swf.
Lets say I have a variable called:
var myVar:String="xmlfile";
that I want movie B.swf to use for loading a xmlfile.

Below is the loading movie A.swf-snippet with most of the code for the preloader erased.


var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest("B.swf"));

function loop(e:ProgressEvent):void
{
  var perc:Number = e.bytesLoaded / e.bytesTotal;
  percent.text = Math.ceil(perc*100).toString();
 }
function done(e:Event):void
{
  percent = null;
  addChild(l);
}

Open in new window

petersegoAsked:
Who is Participating?
 
tomaugerdotcomConnect With a Mentor Commented:
Peter, sorry to step in here, but a really important question is: do you have access to the source code for both movie B and movie A? If you do, and if movie A has an external document class file, then you should be able to put a public variable in movie A's class, and set that from movie B.

ie:

public class MovieA extends MovieClip {
  public var varToSet:String = "";
}

then, in movie B's class:
package {
  import MovieA; // give movieB access to MovieA's prototype
  public class MovieB extends MovieClip {
     // do the loader stuff and then...
    var movieA:MovieA = MovieA(loader.loaderInfo.content);
    movieA.varToSet = "hello world";
  }
}

Again, this only works if you have access to the source code...

T
0
 
GarrettChristophersonCommented:
I'd look into the localConnection Class...
0
 
petersegoAuthor Commented:
I think that is taking it to extreme.
There must be an easier method.
This was fairly easy in as2.
Im sure its just a question of the right syntax.
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
GarrettChristophersonCommented:
well, in order to increase the possibility of another way, please provide the type of environment that your .swf will be compiled to.  Or else I'm just guessing?

0
 
petersegoAuthor Commented:
Well, Im just gonna publish it on the same domain and the same folder.
In the meantime I found this thread trying to solve the same problem, but even if it seems that he made it work, I cant.
The idea is to pass urlvariables with the urlrequest.

http://stackoverflow.com/questions/2302722/as3-pass-flashvars-to-loaded-swf

0
 
GarrettChristophersonCommented:
OK, you know how to get the .xml into the an .swf, why not just load it into B.swf?
0
 
petersegoAuthor Commented:
Its because theres going to be a large numbers of B.swf, that will be defined by different xml-files. I want to avoid uploading a large number of variations of B.swf, so entering the xmlname and the swf-name through flashvars would be ideal. I have no problem entering the swf-name into the loader-movie, but passing the xmlname on to the B.swf is the problem.
B.swf is so large - 200+ kb - because of a lot of UI-components, that it needs a preloader.
And the best way to preload is to load the movie B.swf into another movie - A.swf - with a preloader.
0
 
GarrettChristophersonCommented:
Its because theres going to be a large numbers of B.swf, that will be defined by different xml-files. I want to avoid uploading a large number of variations of B.swf
This is vague.

I am working on a solution...

<non-related question='do you have any 720p video of a top view of an Eagle, Falcon,  or Hawk?'>
0
 
GarrettChristophersonCommented:
Here's a related question, do you think that using Javascript to re-instantiate  a html tag value is faster because it is not being process by another swf movie that could serve the same purpose?
0
 
GarrettChristophersonCommented:
Plus, I'm trying to determine part of if/how/when  the child.swf inherits the LoaderInfo object
0
 
petersegoAuthor Commented:
Im not sure what you mean by the last question.

The part you find vague:
with flashvars I do not have to upload 50 versions of B.swf each with its own xml-file inside.
0
 
GarrettChristophersonCommented:
Yeah, that way you can change what ever it to what ever xml file you want, I just don't get why it has to be done with flashvars?
0
 
GarrettChristophersonCommented:
If you are sending the same information, just with two seemingly different command configurations, the question is why choose one over the other?
0
 
petersegoAuthor Commented:
What do you mean by command configurations.
What other options are there than entering the info through flashvars?
0
 
GarrettChristophersonCommented:
ID: 34168918

You can use LocalConnection, From where I'm reading, it is much simpler, plus my preliminary tests on updating the flashvars parameter have failed, seemingly from the child.swf not being able to access the parent.swf LoaderInfo.parameters which loads the flashvars in.

second of all, if you update the parameter with Javascript, what is going to put that object's value into the .swf? is it automatic?  are you going to reload the preloader object to pass the value in again with a local-like AJAX call?  I just haven't seen a javascript event or html tag/event geared toward this.  There are less lines of script using LocalConnection, less webpage downloads, changing reading styles, a more streamlined testing environment.  The only reason I can see continuing the pursuit of modifiying this parameter that is passed into flash on the initial load, is the possibility of exposing a bit of the XMLFileName to a SearchEngineBot, if the bot will even wait long enough for the parameter to be rewritten??  Lots of unknowns, For all I know, you would have to create code outside of Web/internet/interweb/W3C Standards, which I have chosen to stay away from, no trogan knowledge here besides writing.  
0
 
GarrettChristophersonCommented:
commands - instruction that tell the computer to do something
configuration - the manner in which organizing information will achieve a perceived effect.
0
 
GarrettChristophersonCommented:
What version of flash are you using? I'm going to provide my files for this post, maybe someone will build on them..??
0
 
petersegoAuthor Commented:
Im using CS4
0
 
GarrettChristophersonCommented:
0
 
tomaugerdotcomCommented:
Here's an example I did for someone else on EE a while ago that addresses this same issue. In the attached code I do it one of two ways (my apologies to Garrett if he's already addressed this in his attachment - I have to admit I haven't looked at it).

The first way is similar to what I describe above, though more generic, by providing a simple interface to the loaded movie and then giving access to that interface to the loader movie.

The second way is reasonably complex an uses a technique called run-time introspection that looks inside the loaded movie clip and figures out what elements are available or accessible on it.

And yes, this is way more complicated than it used to be in AS2, because of AS3's strong typing and strict adherence to OO principles. loaderAndDocumentClass.zip
0
 
tomaugerdotcomCommented:
And finally, we can probably (I haven't tested this because it turns my stomach) just type the loader.contentLoaderInfo.content to Object. Once something is typed as an object, the compiler doesn't complain when you do funky stuff to it, like adding or accessing spurious properties which may or may not be accessible.

To be slightly more safe, you can wrap it in a try-catch block...

var movieA:Object = Object(loader.contentLoaderInfo.content);
try {
  movieA.myVarToSet = "hello ghetto";
} catch (e:Error){
  trace("dang, didn't work.");
}

PS: I made an error in post ID:34173100 - it's loader.contentLoaderInfo, not loader.loaderInfo. I've always found the Loader implementation to be messed up and bizarre.
0
 
petersegoAuthor Commented:
Thanks - that works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.