Skip to main content
Inspiring
May 24, 2006
Answered

Loaded swf, passing variables

  • May 24, 2006
  • 7 replies
  • 517 views
If i load a swf movieclip into a parent swf i.e. using:
_root.loadMovie("bar_chart.swf");
How can i then pass variables into this swf from the parent using AS?

Is it possible to use _global to declare a global variable in the parent and then acessing this in the loaded swf i.e.:
Parent:
_global.test = new Array();
Loaded:
test=y;
(though this doesnt work as i have tried it, but something along these lines)

Basically i'd appreciate any info on how i could pass variables between a loaded swf and its parent, though try to keep it in laymans terms because i'm not so hot at action script.
Thanks for any help in advance!
This topic has been closed for replies.
Correct answer Deepak_Joshi
put the code in first frame of your movie. its working fine. i dont know why u want to call one perticular function from external swf file.

_global.bars = new Array();
for (i=0; i != 5; i++) {
bars.push(i);
}
var total = 0;
var a = _global.bars.length;
for (i=0; i != a; i++) {
total += _global.bars;
}
for (i=0; i != a; i++) {
this.createEmptyMovieClip(("bar"+i), this.getNextHighestDepth());
var colour = "0x"+random(9)+random(9)+random(9)+random(9)+random(9)+random(9);
this["bar"+i].beginFill(colour);
this["bar"+i].moveTo(((i/a)*100), (100-(_global.bars/total*100)));
this["bar"+i].lineTo(((((i+1)/a)*100)-1), (100-(_global.bars/total*100)));
this["bar"+i].lineTo(((((i+1)/a)*100)-1), 100);
this["bar"+i].lineTo(((i/a)*100), 100);
this["bar"+i].lineTo(((i/a)*100), (100-(_global.bars/total*100)));
this["bar"+i].endFill();
}

7 replies

Inspiring
May 24, 2006
if you want to go your way, attached is the Code for main movie:

_global.bars = new Array();
for (i=0; i != 5; i++) {
bars = i;
}
var mclListenerbject = new Object();
mclListener.onLoadComplete = function(target_mc:MovieClip) {
target_mc._height = 500;
};
var _mcl:MovieClipLoader = new MovieClipLoader();
_mcl.addListener(mclListener);
_mcl.loadClip("bar_chart.swf", _level1);
Deepak_JoshiCorrect answer
Inspiring
May 24, 2006
put the code in first frame of your movie. its working fine. i dont know why u want to call one perticular function from external swf file.

_global.bars = new Array();
for (i=0; i != 5; i++) {
bars.push(i);
}
var total = 0;
var a = _global.bars.length;
for (i=0; i != a; i++) {
total += _global.bars;
}
for (i=0; i != a; i++) {
this.createEmptyMovieClip(("bar"+i), this.getNextHighestDepth());
var colour = "0x"+random(9)+random(9)+random(9)+random(9)+random(9)+random(9);
this["bar"+i].beginFill(colour);
this["bar"+i].moveTo(((i/a)*100), (100-(_global.bars/total*100)));
this["bar"+i].lineTo(((((i+1)/a)*100)-1), (100-(_global.bars/total*100)));
this["bar"+i].lineTo(((((i+1)/a)*100)-1), 100);
this["bar"+i].lineTo(((i/a)*100), 100);
this["bar"+i].lineTo(((i/a)*100), (100-(_global.bars/total*100)));
this["bar"+i].endFill();
}
Inspiring
May 24, 2006
Its because ur movie has still not loaded. if you will trace _level1 at this time, you will get undefined as output. load your movie using following code.

var mclListener bject = new Object();
mclListener.onLoadComplete = function(target_mc:MovieClip) {
target_mc._height = 500;
};
var _mcl:MovieClipLoader = new MovieClipLoader();
_mcl.addListener(mclListener);
_mcl.loadClip("bar_chart.swf", _level1);

TolkAuthor
Inspiring
May 24, 2006
Ok there must be a better way to do what i'm trying to do:
Basically I have my one swf that draws a bar_chart, from the global variable defined (which is an array of values).
I'm trying to make it as seperate from the main movie as possible so that I can re-use the chart drawing at multiple points along the time line of the main movie and in different movies without having to re-hardcode it everytime. Unfortunately my knoledge of AS is not so hot so i jumped upon the first way of doing this, which i thought might work, that i could find.
Here's the code (it isn't neat and no anotation i know :I ):
Chart Drawing swf:

var total = 0;
var a=_global.bars.length;

for (i=0;i!=a;i++){
//_global.bars =i;
total+=_global.bars
;
}
for (i=0;i!=a;i++){
this.createEmptyMovieClip(("bar"+i), this.getNextHighestDepth());
var colour="0x"+random(9)+random(9)+random(9)+random(9)+random(9)+random(9);
this["bar"+i].beginFill(colour);
this["bar"+i].moveTo(((i/a)*100), (100-(_global.bars /total*100)));
this["bar"+i].lineTo(((((i+1)/a)*100)-1), (100-(_global.bars
/total*100)));
this["bar"+i].lineTo(((((i+1)/a)*100)-1), 100);
this["bar"+i].lineTo(((i/a)*100), 100);
this["bar"+i].lineTo(((i/a)*100), (100-(_global.bars /total*100)));
this["bar"+i].endFill();
}



Code for main movie:

_global.bars = new Array();
//testing data
for(i=0;i!=5;i++){
bars
=i;
}
loadMovieNum("bar_chart.swf",1);
stop;


I can copy the contents of the chart drawing swf into a new symbol on the main movie and it works exactly the same except obviously there is no need to load it into the movie as can be dragged on from library. This is prb slightly better but there still must be a better way of doing this?
Anybody suggest a way / point me to a tutorial or something that may be helpful?
Thanks again!
TolkAuthor
Inspiring
May 24, 2006
I tried but i didn't do anything, movieclip is still same height?
TolkAuthor
Inspiring
May 24, 2006
One more question,
after using loadMovieNum(bla bla... how can i reference the movie that's loaded.
Say i wanted to change the height of the movieclip how would i do it?
I tried just using the movieclip name i.e.:

loadMovieNum("bar_chart.swf", 1)
bar_chart._height =500;

But it didn't work like i expected because should be adressing an instance.
So how can i change the position, height etc. of a loaded movieclip?

Also, if i didnt use the _global to define the variable and just used var test=new Array(); (or whatever) would using _level0.test in the loaded movie allow me to access this variable?
Inspiring
May 24, 2006
loadMovieNum("bar_chart.swf", 1)
_level1._height =500;
TolkAuthor
Inspiring
May 24, 2006
It does work using _global and _root.loadMovie, I just realized i forgot to republish one of my swf's so that's why it didnt work when i tested it. As long as the var is declared before loading the swf.
Though like you said it starts a fresh movie (which i didnt know until now as screen size'n background where same on both movies).
I tried using loadMovieNum like you suggested and works great, thanks for help!
Inspiring
May 24, 2006
sry kid.
none of the above methods will work.

when u say
_root.loadMovie("bar_chart.swf");

u actually completely kills all the variables and objects and now a fresh movie "bar_chart.swf" is playing.

i will suggest you to use levels.
loadMovieNum("bar_chart.swf", 1)

now if u got a variable set at any other level, or global variables can be easily accessed like

_level0.test
_global.test