It should be easy with “break;”, but there is a slight complication to the code - the for-loop contains a function from within which I want to be able to break the loop.
Here is a codesnippet:
[AS]
for(i = 8; i > 1 ; i–){
loadText = new LoadVars();
loadText.load(“Textfile.txt”);
loadText.onLoad = function(success) {
if (success) {
break for; // HERE I WANT THE BREAK
}
};
}
[/AS]
So, somehow, I want to break the for-loop from within the onLoad-function. Is that possible with some other code (this ofcourse doesnt work)?
Well, I broke it down for clarity, so it doesnt really do anything now.
– RANT ABOUT THE FUNCTION –
But originally, I use the code to check a couple of folders (FOLDER_i) to see if a text-file exists in them.
I count down, from the maximum allowed number of folders (which is 8).
When the first folder with a “Textfile.txt” (which is indicated with onLoad-function being “success”) is encountered, I load a navigationbar with “i”-number of buttons in it (“i” is the counter in the for-loop).
The problem is that I need to stop the for-loop once this happens, or Ill keep loading all other navigationbars with buttons down to 2 (i > 1 is the for-condition).
This way, I dont have to re-write the code when I want to add a folder to my .swf-file. I just dump the folder into the right place, named “FOLDER_i” (where (i < 8) && (unique)), and a navigationbar with an extra button will appear.
Well, I wish there was a better way (probably there is) to check for the existence of folders without having to load a text-file within it to get a success-parameter returned. It takes a bit of time to load text-files. Im using MX 2004 now, so maby there is a good way to do this?
– END RANT –
Anyway, if I get a way to break the for-loop, it will work fine
Well, a [font=courier new]for[/font] or a [font=courier new]while[/font] statement is not an option here. The statement will not wait until the file has been loaded to continue.
…I suggest you to define a function which will load the file, and call the function again if the file has been loaded successfully:
function loadFile() {
if (i>1) {
var my_lv = new LoadVars();
my_lv.load("FOLDER_"+i+"/file.txt");
my_lv.onLoad = function(success) {
if (success) {
loadFile();
};
i--;
}
}
}
var i = 8;
loadFile();
There’s unfortunately no way to check for the existing folders from Flash, though you could use a server-side script (such as PHP) to get them.
Yes, Ive noticed the for-loops doesnt wait (which is the case in other languages, I think), and I have tried every way I could think of to slow it down. But I never thought of using function-overloading. Great idea!
This is how I implemented your code:
[AS]
function fn_Navigation(i){
if (i > 1){
loadText = new LoadVars();
loadText.load(“FOLDER_”+i+"/file.txt");
loadText.onLoad = function(success) {
if (success) {
loadMovieNum(“NAVIGATION_” + i + “.swf”, 15);
}
else {
i --;
fn_Navigation(i);
}
};
}
}
[/AS]
Shame, though, that it is very slow. Seems if-statements slow down the processing considerably. Or maby its the function overloading?
And it’s probably the time it takes to load each file, I really don’t think that the [font=courier new]if[/font] statements are slowing down the proccess.