I'm struggling with the code below. I have tried many different ways of doing this but I end up with one of two incorrect results.
for(i = 0; i < result.length; i++) {
var tmpBlockInfo = {
size: worldTest.data[0].size,
xStartPixel : result[i].x * worldTest.data[0].size,
yStartPixel : result[i].y * worldTest.data[0].size,
blockType : (Math.random() * 100 > 10) ? 'path' : 'wall'
}
var tmpFunc = function(){
worldTest.fillBlock(tmpBlockInfo, 157, 152, 124, 255)
};
var t = setTimeout(function(){
tmpFunc()
} , 500 * i);
}
The problem with the above code is tmpBlockInfo always gets the last result[i].x / result[i].y. So i'm asuming when the timeout runs the function its seeing what result[i].x / result[i].y was left on after the loop (rather than passing it as a "new" variable)
I thought putting it into a function would fix the closure problem, but no luck.
Also tried:
for(i = 0; i < result.length; i++) {
var tmpBlockInfo = {
size: worldTest.data[0].size,
xStartPixel : result[i].x * worldTest.data[0].size,
yStartPixel : result[i].y * worldTest.data[0].size,
blockType : (Math.random() * 100 > 10) ? 'path' : 'wall'
}
var t = setTimeout(function(){
worldTest.fillBlock(tmpBlockInfo, 157, 152, 124, 255)
} , 10000 * i);
}
With the same results as the first code.
If I do:
for(i=0; i < result.length; i++) {
var tmpBlockInfo = {
size: worldTest.data[0].size,
xStartPixel : result[i].x * worldTest.data[0].size,
yStartPixel : result[i].y * worldTest.data[0].size,
blockType : (Math.random() * 100 > 10) ? 'path' : 'wall'
}
setTimeout(function(passBlockInfo) {
worldTest.fillBlock(tmpBlockInfo, 157, 152, 124, 255)
} (tmpBlockInfo), 1000 * i);
}
It does process all the fillBlock functions correctly. BUT it does them all at the same time (e.g. it's not firing them one at a time. It's just doing them after each other but causing blocking (no screen update) and no delay between each one.
Any help with this would be great!