Link to home
Start Free TrialLog in
Avatar of petersego
petersego

asked on

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

Avatar of GarrettChristopherson
GarrettChristopherson

I'd look into the localConnection Class...
Avatar of petersego

ASKER

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

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

OK, you know how to get the .xml into the an .swf, why not just load it into B.swf?
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.
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?'>
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?
Plus, I'm trying to determine part of if/how/when  the child.swf inherits the LoaderInfo object
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.
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?
If you are sending the same information, just with two seemingly different command configurations, the question is why choose one over the other?
What do you mean by command configurations.
What other options are there than entering the info through flashvars?
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.  
commands - instruction that tell the computer to do something
configuration - the manner in which organizing information will achieve a perceived effect.
What version of flash are you using? I'm going to provide my files for this post, maybe someone will build on them..??
Im using CS4
ASKER CERTIFIED SOLUTION
Avatar of tomaugerdotcom
tomaugerdotcom
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Thanks - that works.