PDA

View Full Version : (CS3) Accessing an Array Dynamically


JethroGillgren
09-14-2008, 02:34 PM
Hi, Thanks ahead for any help. I'm learning so please put up with my stupid questions.


Basically I've got a load of Arrays, and i do a function called LoadImageArray, passing in the array, and it's name as a string. a function will run, and inside there i want to update the array. I don't know what the array name will be, so how do i change the array using the "thisName" argument? (which has the array name inside)
Just saying thisName[i] etc... is referencing the argument (I think they're called arguments?) and I don't know how to make it reference the Array of the same name.

var imageArray0:Array = [];

LoadImageArray(imageArray0, "imageArray0")


function LoadImageArray(thisArray:Array, thisName:String) :void {

//Unimportant Stuff Cut Out
//Below function will run...

function onAllLoaded(done:Event) :void {

//Unimportant Stuff Cut Out
//Here I want to access the Array (for this example it is imageArray0) that has been passed in under the argument "thisName".


}
}
}



Thanks for any help

xxneon
09-14-2008, 02:50 PM
ok first, your functions should be seperate .. you dont want to nest a function inside another.. you lose your scope that way.. amoung other possible issues.. but to do what your asking i would think it would be a matter of just passing the array itself.. variables that point to the same object will automatically change the object reference.. it does not make a copy of the object when you pass it to a function .. so thisArray is the same as imageArray0 if you pass that to the function directly .. you dont want to use a string representation of the array name..

if your functions are on the timeline with your code then this will work to show you that both the variable and the argument are pointing to the same thing..
var lastLoadedArray:Array; //added for explanation below..
function LoadImageArray(thisArray:Array, thisName:String) :void {
trace(thisArray == imageArray0);
//... notice i also showed the onAllLoaded function as being declared outside this function for preservation of scope.
//editted for below explanation..
lastLoadedArray = thisArray;
}
function onAllLoaded(done:Event) :void {
//code..
//added for explanation below..
trace(lastLoadedArray == imageArray0);
}
ofcourse doing it this way i just realised your onAllLoaded event listener wont know what array was passed to the LoadImageArray function though .. unless you have a variable in place that accually stores the reference so other functions can use it.. or use a custom event..

JethroGillgren
09-14-2008, 03:07 PM
Thanks A lot for the explination, the array's changing fine now. N i understand properly now.

I have been placing my functions seperatedly throughout most of the rest of the file, but when i've been running a function from an EventListener i've had to nest it... In this the onAllLoaded function needs to know which array was passed into LoadImageArray, how would I pass that value through? I've tried adding the argument into eventListener thing but can't get it to compile right.

oh and thanks again for the help

JethroGillgren
09-14-2008, 03:27 PM
NVM I've worked it out, couple stupid mistakes really