Reputation: 73
Im trying to load some image before using them in the canvas. I created a function that wait the 'onload' and then run the mainloop, but sometimes it doesn't work...
here's the code :
var imagesSrc = ['paris.jpg','Paris','shanghai5.jpg','Shangai','beijing.jpg','Beijing','brslondon.jpg','London','madrid.jpg','Madrid','luxembourg.jpg','Luxembourg','dubai.jpg','Dubai','geneve.jpg','Geneva','sino.jpg','Shanghai','swaar.jpg','Mumbai','singapore2.jpg','Singapore','hongkong.jpg','Hong Kong'];
for (var i = imagesSrc.length - 1; i >= 0; i-=2) {
var image = new Image();
image.name = imagesSrc[i];
image.onload = function()
{
load++;
if(load >= imagesSrc.length*0.5){
ImageGenerator(game , 6000);
}
}
image.src = 'assets/images/illus/'+imagesSrc[i-1];
game.images.push(image);
}
It looks fine to me...
Can someone help me?
thx!
Upvotes: 0
Views: 58
Reputation: 5123
Try to do this like:
function imgTest(){
var imagesSrc = ['paris.jpg','Paris','shanghai5.jpg','Shangai','beijing.jpg','Beijing','brslondon.jpg','London','madrid.jpg','Madrid','luxembourg.jpg','Luxembourg','dubai.jpg','Dubai','geneve.jpg','Geneva','sino.jpg','Shanghai','swaar.jpg','Mumbai','singapore2.jpg','Singapore','hongkong.jpg','Hong Kong'];
for (var i = imagesSrc.length - 1; i >= 0; i-=2) {
var image = new Image();
image.name = imagesSrc[i];
image.onload = function()
{
load++;
if(load >= imagesSrc.length*0.5){
ImageGenerator(game , 6000);
}
}
image.src = 'assets/images/illus/'+imagesSrc[i-1];
game.images.push(image);
}
}
And call this like:
window.onload=imgTest;
Upvotes: 1