How To Pass Variables from External SWF to Main Page?

  • BENBOB
  • Novice
  • Novice
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 17
  • Status: Offline

Post January 31st, 2008, 9:34 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post January 31st, 2008, 9:34 am

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Joined: Jul 11, 2005
  • Posts: 1828
  • Loc: In the Great White North
  • Status: Offline

Post January 31st, 2008, 7:20 pm

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?
If at first you don't succeed F1... If that doesn't work try Google!
//// Designer, Developer & Teacher - Interactive, Motion and 3D \\\\
Portfolio at WhenImNotSleeping.com
  • BENBOB
  • Novice
  • Novice
  • No Avatar
  • Joined: Jun 03, 2005
  • Posts: 17
  • Status: Offline

Post February 1st, 2008, 9:07 am

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
  • No Avatar
  • Joined: Feb 06, 2008
  • Posts: 5
  • Loc: http://elearningking.blogspot.com
  • Status: Offline

Post February 6th, 2008, 5:39 am

Thanks all for these great info.
  • ATNO/TW
  • Super Moderator
  • Super Moderator
  • User avatar
  • Joined: May 28, 2003
  • Posts: 23404
  • Loc: Woodbridge VA
  • Status: Offline

Post February 6th, 2008, 6:24 am

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.
"There's no place like 127.0.0.1 except for ::1."
Alexandria Networks. Leader in IT consulting for associations/non-profits, and small to medium sized businesses around the northern Virginia and Washington D.C. metro area.
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Joined: Jul 11, 2005
  • Posts: 1828
  • Loc: In the Great White North
  • Status: Offline

Post February 6th, 2008, 11:52 am

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


Heh I guess you just have to ask :-)
If at first you don't succeed F1... If that doesn't work try Google!
//// Designer, Developer & Teacher - Interactive, Motion and 3D \\\\
Portfolio at WhenImNotSleeping.com

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 38 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.