Solved

how do I pass variables to loaded swf

Posted on 2010-11-18
23
1,580 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:petersego
  • 12
  • 8
  • 3
23 Comments
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
I'd look into the localConnection Class...
0
 

Author Comment

by:petersego
Comment Utility
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
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
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
 

Author Comment

by:petersego
Comment Utility
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
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
OK, you know how to get the .xml into the an .swf, why not just load it into B.swf?
0
 

Author Comment

by:petersego
Comment Utility
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
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
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
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
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
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
Plus, I'm trying to determine part of if/how/when  the child.swf inherits the LoaderInfo object
0
 

Author Comment

by:petersego
Comment Utility
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
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
If you are sending the same information, just with two seemingly different command configurations, the question is why choose one over the other?
0
 

Author Comment

by:petersego
Comment Utility
0
 

Author Comment

by:petersego
Comment Utility
What do you mean by command configurations.
What other options are there than entering the info through flashvars?
0
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
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
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
commands - instruction that tell the computer to do something
configuration - the manner in which organizing information will achieve a perceived effect.
0
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
What version of flash are you using? I'm going to provide my files for this post, maybe someone will build on them..??
0
 

Author Comment

by:petersego
Comment Utility
Im using CS4
0
 
LVL 5

Expert Comment

by:GarrettChristopherson
Comment Utility
0
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 500 total points
Comment Utility
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
 
LVL 14

Expert Comment

by:tomaugerdotcom
Comment Utility
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
 
LVL 14

Expert Comment

by:tomaugerdotcom
Comment Utility
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
 

Author Closing Comment

by:petersego
Comment Utility
Thanks - that works.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now