How To Pass Variables from External SWF to Main Page?

  • BENBOB
  • Novice
  • Novice
  • BENBOB
  • Posts: 17

Post 3+ Months Ago

Hi,

Ive got a main flash page into which I load external swf files using the "Loader" component.

My question is how to get the external swf to communicate variables to the main page?

For instance, say I have defined x = 1 in the external swf, how do I get it to trace out this value for x in the main page? Heres what I tried initially, and it just says "undefined":

ActionScript in External SWF: (called "blue.swf")
x = 1;

ActionScriot in Main SWF:
myLoader.contentPath = "blue.swf";
trace(x);

Ive looked on the net and other posts on this forum and tried various combinations of "_parent, _root, _level0" etc but wasnt sure exactly where I put them!?

Please help! :D
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

Hey Ben. I've been kinda MIA lately but I think I can solve this one for you :-)

A little background. When you load an external movie it replaces the "target" clip that you assign in the loader. So if I had a main stage with a child clips called "mc1" and a movie called "mc2" inside of the "mc1", I could do something like this (assuming this code is on your root timeline)
Code: [ Select ]
var mcLoader:MovieClipLoader = new MovieClipLoader();
mcLoader.loadClip("blue.swf", this.mc1.mc2);
  1. var mcLoader:MovieClipLoader = new MovieClipLoader();
  2. mcLoader.loadClip("blue.swf", this.mc1.mc2);


Now I can access all the objects of my loaded clip simply by using this target:
Code: [ Select ]
trace(this.mc1.mc2._alpha); // example of a property or
trace(this.mc1.mc2.myVariableName); // variable located in the loaded clip
  1. trace(this.mc1.mc2._alpha); // example of a property or
  2. trace(this.mc1.mc2.myVariableName); // variable located in the loaded clip


So you can get the variable content with its path from where ever your code is located or when you define the variable you could specify that its global. For example.
Code: [ Select ]
_global.x = 1;

and then access it with
Code: [ Select ]
trace(_global.x);

This solution is generally considered bad practice because you get a bunch of variables that exist everywhere and pollute the global scope. Best bet is to go with the first version.

Theres likely a second issue here though, and thats that you may be calling the variable before the external clip has actually loaded and written the variable. Actionscript processes code very quickly and so two lines of code may fire only a fraction of a second behind each other. This is usually much too quick for the http request thats actually retrieving and loading the external file. The result is that Flash says "I checked for the variable you want but it doesn't exist at this moment in time so I won't look again and I'll say its undefined"

The way around this is to wait to call the variable until you know the the clip has loaded and the variable exists. This is where callbacks come in. You basically need to tell flash to do something when another process completes (loading a file, playing a video, etc)
Code: [ Select ]
var mcLoader:MovieClipLoader = new MovieClipLoader();
mcLoader.addListener(this);
mcLoader.loadClip("blue.swf", this.mc1.mc2);

// this is the callback function and it runs when the first frame of AS in the external file is loaded
function onLoadInit(mc:MovieClip) {
  // flash knows that mc = the path to your target clip
  trace(mc.x); // show the value of x after the clip is loaded
}
  1. var mcLoader:MovieClipLoader = new MovieClipLoader();
  2. mcLoader.addListener(this);
  3. mcLoader.loadClip("blue.swf", this.mc1.mc2);
  4. // this is the callback function and it runs when the first frame of AS in the external file is loaded
  5. function onLoadInit(mc:MovieClip) {
  6.   // flash knows that mc = the path to your target clip
  7.   trace(mc.x); // show the value of x after the clip is loaded
  8. }


Make sense?
  • BENBOB
  • Novice
  • Novice
  • BENBOB
  • Posts: 17

Post 3+ Months Ago

Hey Bryce,

Made a few modifications and it works perfectly in my file :) Cheers your a legend!

Im so close to finally getting my website done! After weeks messing around inserting cue points and trying to get .FLV to work properly, I realised I could just import the video using the "Embed video in SWF and play in timeline" option - since they are only short clips with no sound. Should save a few headaches! Ill email you the link once its done...

Thanks again, Ben.
  • goodamr
  • Newbie
  • Newbie
  • goodamr
  • Posts: 5
  • Loc: http://elearningking.blogspot.com

Post 3+ Months Ago

Thanks all for these great info.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Posts: 23456
  • Loc: Woodbridge VA

Post 3+ Months Ago

Excellent explanation. Had to learn how to do exactly this a few months ago. I remember searching for like an hour for exactly what graphixboy explained here. Wish this post would have been around back then *lol.
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

ATNO/TW wrote:
Wish this post would have been around back then *lol.


Heh I guess you just have to ask :-)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 6 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.