Last active
December 24, 2015 12:49
-
-
Save hex13/6800760 to your computer and use it in GitHub Desktop.
spaceshooter, tutorial, step1, loadImages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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