Skip to content

Instantly share code, notes, and snippets.

@hex13
Last active December 24, 2015 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hex13/6800760 to your computer and use it in GitHub Desktop.
Save hex13/6800760 to your computer and use it in GitHub Desktop.
spaceshooter, tutorial, step1, loadImages
function loadImages(directory, fileNames, onComplete) {
var imagesLeft = fileNames.length;
fileNames.forEach(function(fileName) {
var img = new Image();
img.onload = function() {
imagesLeft--; // tu zliczamy ile obrazkow się zaladowalo
if (imagesLeft <= 0) // jesli wszystkie obrazki sie zaladowaly...
onComplete(); // uruchamiamy funkcje onComplete (podana jako argument funkcji)
};
img.src = directory + '/' + fileName;
// z "spaceship.png" uzyskamy "spaceship", za pomoca funkcji substring
var unitName = fileName.substring(0, fileName.lastIndexOf('.'));
images[unitName] = img; // przypisujemy obiekt obrazka (tj. obiekt typu Image) do zmiennej images.
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment