Skip to content

Commit

Permalink
hopefully fixed loading
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackScorp committed Mar 4, 2012
1 parent aa6c0c2 commit fb94ef0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
/assets/img/.fr-5KubhJ/
/nbproject
.*.sw?
*~
46 changes: 27 additions & 19 deletions assets/js/crafty.js
Expand Up @@ -7206,21 +7206,27 @@ Crafty.c("Text", {
*/
load: function (data, oncomplete, onprogress, onerror) {

var i = 0, l = data.length, current, obj, total = l, j = 0, ext = "",event;
var i = 0, l = data.length, current, obj, total = l, j = 0, ext = "",event,canplay;

for (; i < l; ++i) {
obj = new Object();


current = data[i];
console.log(current);
event = '';
obj ={};
ext = current.substr(current.lastIndexOf('.') + 1).toLowerCase();

if (Crafty.support.audio && (ext === "mp3" || ext === "wav" || ext === "ogg" || ext === "mp4")) {
obj = new Audio(current);

event = 'canplaythrough';
//event = 'play';
this.assets[current] = obj;
event = 'loadstart';
canplay = obj.canPlayType(Crafty.audio.type[ext]);
if(canplay !== "" && canplay !== "no"){
this.assets[current] = obj;
}else{
j--;
delete obj;
}
//Chrome doesn't trigger onload on audio, see http://code.google.com/p/chromium/issues/detail?id=77794
if (navigator.userAgent.indexOf('Chrome') != -1) j++;
} else if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png") {
Expand All @@ -7232,36 +7238,38 @@ Crafty.c("Text", {
total--;
continue; //skip if not applicable
}

function progress(){
//Progress function
function pro(){

++j;
//if progress callback, give information of assets loaded, total and percent
if (onprogress)
onprogress.call(this, {loaded: j, total: total, percent: (j / total * 100),obj:this});
console.log(this);

onprogress({loaded: j, total: total, percent: (j / total * 100),obj:this});

if(j === total && oncomplete) oncomplete();
};

function error(){
//Error function
function err(){
if (onerror)
onerror.call(this, {loaded: j, total: total, percent: (j / total * 100),obj:this});
onerror({loaded: j, total: total, percent: (j / total * 100),obj:this});

j++;
if(j === total && oncomplete) oncomplete();
};

if (obj.attachEvent) { //IE
obj.attachEvent('on' + event, progress);
obj.attachEvent('onerror', error);

obj.attachEvent('on' + event, pro);
obj.attachEvent('onerror', err);
} else { //Everyone else
obj.addEventListener(event, progress, false);
obj.addEventListener('error', error, false);
obj.addEventListener(event, pro, false);
obj.addEventListener('error', err, false);
}


}



},
/**@
* #Crafty.modules
Expand Down
5 changes: 3 additions & 2 deletions assets/js/levels.js
Expand Up @@ -7,7 +7,7 @@ Crafty.scene("Loading",function(){
for(var i in Crafty.assets){
toLoad.push(i);
}


//Setup background image
Crafty.background("url("+game_path+"assets/img/loading.jpg) black");
Expand Down Expand Up @@ -51,8 +51,9 @@ Crafty.scene("Loading",function(){
},

function(e) {

//uh oh, error loading
console.log("Error on loading: "+e.src);
console.log("Error on loading: "+e.obj.src);
}
);

Expand Down

0 comments on commit fb94ef0

Please sign in to comment.