I have a function sitting in the 1st frame on level 0 of my movie. This function loads an xml file and processes the extracted data. Works fine. Within the function I’ve created a variable myLogo from an xml array and I can see it fine in the debugger:-
myLogo = logoPath; // logoPath is from xml array
My problem is that I have a movie clip that hangs off the root that needs one variable from within this function to load an external file:-
var logo = _level0.root.myLogo; // logo is path to the file to be imported
this.createEmptyMovieClip(“holder_mc”, 1);
var mcl:MovieClipLoader = new MovieClipLoader();
mcl.addListener(this);
mcl.loadClip(“logo”, holder_mc); // grab logo
I’ve tried setting the root variable to _global but I still can’t see it in the above mc script in the debugger.
Does anyone know how to extract a variable in a function from another mc?
Many thanks.
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 100 and 499 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 6-7 years
- Item was Featured
set up the var outside the function, then you can use it anywhere 
so instead of
function myFunction() {
var myVar:String = "blah";
}
use
var myVar:String = new String;
function myFunction() {
myVar = "blah";
}
Or if it’s a variable of a type that you can only set up when you have certain parameters, like a custom class that needs xml data for example, then put the variable you create within the function into an array or object that you create outside of the function… like so
var myObj:Object = new Object(); function myFunction() { var myVar:someCustomType = new someCustomType(param); myObj.myVar = myVar; }
then you can access myObj.myVar from outside the function too.
Ok so I’m probably complicating matters but this is something that had me tearing my hair out a while back…
Thanks for getting back guys.
flashjunkie I would set up the variable outside of the function if I could but it’s a custom class that needs xml data.
mbudm I think you’re on the right track. I actually need to send about 10 variables out of this function so an array seems the most efficient method. For testing I set up an array outside of the function and it works fine. However I can’t read the array when I put it in the function. Is the scope of an array the same as variables when used in functions? Any idea how I can read the array outside of the function?
many thanks again for your time
I setup an empty array outside of the function:-
configImport = new Array();
and within the function pushed some strings to populate the array:- pushed = configImport.push(“logoPath”, “soundAuto”,”soundMiniPlayer”, “soundShowPlayer”, “soundInitVol”); trace(pushed);
Doesnt work either
you are forgeting this is event driven
since there is load time involved with loading the xml and then parsing it the data will not exist untill that happens
since you have stated that it is a custom xml class. what you need to do in the method in that class that parses the xml you need to dispatch an event.COMPLETE or dispatch a custom event
then when you instantiate your custom xml class you need to add an event listener to it like.
var myCustomXmlClass:myCustomXmlClass = new myCustomXmlClass( )
myCustomXmlClass.addEventListener( event.COMPLETE, myCallBackFunc );
function myCallBackFucntion( e:Event ){
// do the work here
}
AS3 is very good in handling events fired from class objects. Dont over complicate things. oh and almost forgot in your custom class you need to have a public variable that the data is assigned to IE: public var data:Array; so in your call back function you can grab it with e.target.data
disclaimer: I wrote this off the top of my head and am sure i forgot something but it is a start for you. email me if you still dont get it
