I'm developing a game using JavaScript and canvas
. As the game loads, all images that will be used are being cached.
Observing the resource timeline, I see that the following code triggers an asynchronous request:
var sprite = new Image();
sprite.src = "sprites/sheet1.png";
The engine will keep executing, eventually beginning to draw and play the level. Images that are loaded after the first frame is painted might never appear due to clipping (i.e. not getting "dirty").
So I tested the following:
console.log("begin");
var sprite = new Image();
sprite.onload = function() { console.log('loaded!'); };
sprite.src = "sprites/sheet1.png";
console.log("end");
The resulting console outputs in the order they occur are:
begin
end
loaded!
I'm looking for a similar way to $.ajax
with async: false
to perform the loading. Can't figure out how... thanks in advance for you help!
J.