There is an error in the following code:
generate: function() {
var generated = [];
for (var j = 0; j < objectDefinitions.length; j++) {
var randomNumber;
if (!objectDefinitions[j].restrictGeneration) {
continue;
}
randomNumber = Math.random();
if (randomNumber < objectDefinitions[j].probability) {
generated.push(objectDefinitions[j].createObject());
objectDefinitions[j].restrictGeneration = true;
if (j > 5) {
}
setTimeout(function() {
//console.log(j);
objectDefinitions[j].restrictGeneration = false;
}, objectDefinitions[j].cooldown);
}
}
return generated;
}
When the setTimeout “fires” the variable j is 6, which causes an array index out of bounds exception because there are only 6 items in the array. I don’t really understand what’s going on, I have checked j outside setTimeout and it is never 6, but it seems to change after it’s declaration. Thanks for any help.
The variables
jit reads is the one you used in the loop so it didn’t get “copied”.This way you
jis copied in akvariable. But there is onekvariable per iteration of the loop.