I'm having problems uploading images to the server. I would like to know the correct way to preload those images.
// JavaScript Functions
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
function load(){
var colors = ["http://meusite.com/pasta/img/playerRightC.png","http://meusite.com/pasta/img/playerLeftC.png","http://meusite.com/pasta/img/playerRight.png","http://meusite.com/pasta/img/playerLeft.png","http://meusite.com/pasta/img/wallpaper01.png","http://meusite.com/pasta/img/cheese.png","http://meusite.com/pasta/img/toca.png","http://meusite.com/pasta/img/help1.png","http://meusite.com/pasta/img/help2.png","http://meusite.com/pasta/img/help3.png","http://meusite.com/pasta/img/cnImg.png"];
for (color in colors) {
var img = new Image();
img.src = colors[color];
}
}
load();
sleep(6000).then(() => {
StartGame();
});;
window.addEventListener('keydown', KeyDown, true);
I added a delay of 6000ms to start the game, so images could be loaded by the load function.
However, the images are not preloaded, there is still a delay of 0.5ms to 2.0s on slower connections attempting to play the game. Is this correct for loading images? Is it a more effective way?