PDA

View Full Version : Declaring an array outside of a function?


ryando
04-18-2006, 09:30 PM
Works:
function nature_images() {
_root.nature_images = ["sila", "silb", "silc"];
i = _root.nature_images.length;
k = Math.floor(Math.random()*i);
trace(_root.nature_images[k]);
}
nature_images();

Does not work:
_root.nature_images = ["sila", "silb", "silc"];
function nature_images() {
i = _root.nature_images.length;
k = Math.floor(Math.random()*i);
trace(_root.nature_images[k]);
}
nature_images();

Why?!

Xeef
04-18-2006, 11:46 PM
the function and the variable have the same name

in the first you overwrite the funtion when it's already in the procec of execution (no good in C++ but i coud imagine that this woud cause there a fatal crash)
try to execute the first version a second time :


function nature_images() {
_root.nature_images = ["sila", "silb", "silc"];
i = _root.nature_images.length;
k = Math.floor(Math.random()*i);
trace(_root.nature_images[k]);
}
nature_images();
nature_images();
2 calls 1trace !

in the second version you overwrite the function before you try to execute it there is nothing more to execute

i one sentens :

change your function or variable name !