PDA

View Full Version : Scope trouble with loadVars !



flypix
February 23rd, 2004, 02:16 PM
[AS1][loadVars] Scope trouble with loadVars !

Hi everybody,

I'm new on this server !
You will find enclosed a ".zip" file in wich there's a ".fla" file, a ".txt" file and 6 ".jpg" images.
Frame 1 of the FLA only has a "Preload" class/constructor found on another site and works well.
Frame 2 launches the graphic interface construction.

The file "externalVar.txt" has one variable "maxVin" with a value of 6.
I would like to know WHY i can't acces this variable from outside of the loadVars object's scope (i would better say from outside of its ".onLoad" methode scope).
In fact, when i trace my variable "maxVinNum" from outside of this function, flash player return "undefined". Even if i declare "maxVinNum" on the _root (_root.maxVinNum).

For information, at the start of frame 2, i have let an instruction in comment :
//var maxVinNum = 6;
When we activate this instruction, then the variable is directely declared and everything works well. This test allow us to verify that scripts are not bugged.

Here is the script of frame 2, some of you may understand quickly what's wrong :
//var maxVinNum = 6;
var vignetteVars = new LoadVars();
vignetteVars.onLoad = function(ok) {
if (ok) {
trace(this.loaded);
maxVinNum = this.maxVin;
trace("maxVinNum = " + maxVinNum);
}
};
vignetteVars.load("externalVar.txt");
//
function buildArray (arrayLength) {
FileArray = new Array();
for (var i=0; i<maxVinNum; i++){
FileArray.push ("v_"+(i+1)+".jpg");
}
trace("FileArray : " + FileArray);
};
//
function buildSlide(){
vinKern = 1;
preloadSlide = new Preloader(load_indicator);
for (var i=0;i<FileArray.length;i++){
//Construction du slide
slide.vignette.duplicateMovieClip("vignette"+i,i);
slide["vignette"+i]._x += i*(slide.vignette._width + vinKern);
// ajoute à la file du preloader
preloadSlide.insert(FileArray[i], this.slide["vignette"+i].conteneur, i,"","","okload");
}
slide.vignette._visible=0;
};

buildArray(maxVinNum);
buildSlide()
stop();
I hope my english is not too bad, and my explaination not too long and understable.
thanks a lot for your help !

claudio
February 23rd, 2004, 10:59 PM
Run the buildArray function after the external variable is loaded, then run the buildSlide function after the FileArray has been populated with all the values:
var vignetteVars = new LoadVars();
vignetteVars.onLoad = function(success) {
if (success) {
trace("external variable loaded");
maxVinNum = this.maxVin;
trace("maxVinNum = "+maxVinNum);
buildArray(maxVinNum);
}
};
vignetteVars.load("externalVar.txt");
function buildArray(arrayLength) {
FileArray = new Array();
for (var i = 0; i<maxVinNum; i++) {
FileArray.push("v_"+(i+1)+".jpg");
}
trace("FileArray : "+FileArray);
buildSlide();
}
function buildSlide() {
vinKern = 1;
preloadSlide = new Preloader(load_indicator);
for (var i = 0; i<FileArray.length; i++) {
//Construction du slide
slide.vignette.duplicateMovieClip("vignette"+i, i);
slide["vignette"+i]._x += i*(slide.vignette._width+vinKern);
// ajoute à la file du preloader
preloadSlide.insert(FileArray[i], this.slide["vignette"+i].conteneur, i, "", "", "okload");
}
slide.vignette._visible = 0;
}
stop();

flypix
March 4th, 2004, 02:04 PM
hi,

as you show in your response, the solution is to trigger functions that need to acces that variable from within the onLoad function.

thanks for your help

claudio
March 4th, 2004, 02:06 PM
Welcome :)