-
Notifications
You must be signed in to change notification settings - Fork 3
/
preloader.js
74 lines (65 loc) · 1.66 KB
/
preloader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var Preloader = (function(){
var images = {};
var sounds = {};
var addedCount = 0;
var readyCount = 0;
var failedCount = 0;
var callback = null;
var fired = false;
return {
addImage: function(argImageSrc, skipAddCountFlag) {
// called with an array of images
if(argImageSrc instanceof Array){
addedCount += argImageSrc.length;
for(var i = 0, len = argImageSrc.length; i<len; i++){
Preloader.addImage(argImageSrc[i], true);
}
} else { // called with a single image
if(typeof skipAddCountFlag === 'undefined' || !skipAddCountFlag){
addedCount++;
}
images[argImageSrc] = new Image();
images[argImageSrc].src = argImageSrc;
images[argImageSrc].onload = function(){
readyCount++;
Preloader.checkDone();
};
images[argImageSrc].onerror = function(){
failedCount++;
Preloader.checkDone();
};
}
},
hasImage: function(argImageSrc) {
return images.hasOwnProperty(argImageSrc);
},
getImage: function(argImageSrc) {
return images[argImageSrc];
},
addSound: function(argSoundSrc) {
addedCount++
sounds[argSoundSrc] = new Audio(argSoundSrc);
},
hasSound: function(argSoundSrc) {
return sounds.hasOwnProperty(argSoundSrc);
},
getSound: function(argSoundSrc) {
return sounds[argSoundSrc];
},
checkDone: function(){
if( readyCount === addedCount && failedCount === 0 && addedCount > 0 ){
callback();
} else {
if(failedCount){
console.log("Still loading -> successes: " + readyCount + ", failures: " + failedCount);
}
}
},
ready: function(argCallback){
callback = argCallback;
},
rearm: function(){
fired = false;
}
};
})();