Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more tests

  • Loading branch information...
commit aeb6f4869f5f9db61ef9deb5f76802e38b28cab5 1 parent 89715a4
@BlackScorp authored
View
90 assets/js/audio.js
@@ -14,76 +14,96 @@ Crafty.extend({
'mp4': 'audio/mp4'
},
volume:1, //Global Volume
+ muted:false,
add:function(id,url){
if (!Crafty.support.audio) return;
- var audio,source,ext,path,canplay;
+ var audio,source,ext,path;
if(arguments.length === 1 && typeof id === "object"){
for(var i in id){
audio = document.createElement('audio');
audio.id = i;
- //old attribute
- audio.autobuffer = true;
audio.preload = "auto";
audio.volume = Crafty.audio.volume;
-
for(var src in id[i]){
path = id[i][src];
ext = path.substr(path.lastIndexOf('.') + 1).toLowerCase();
- canplay = audio.canPlayType(this.type[ext]);
- if(canplay !== "" && canplay !== "no" ){
- source = document.createElement('source');
- source.src = path;
- source.type=this.srcType[ext];
- audio.appendChild(source);
-
- if (!Crafty.assets[path]) Crafty.assets[path] = audio;
- }
-
+ source = document.createElement('source');
+ source.src = path;
+ source.type=this.srcType[ext];
+ audio.appendChild(source);
}
this.sounds[i] = {
obj:audio,
played:0
}
audio.load();
-
-
- }
-
-
+
+ }
}
if(typeof id === "string"){
-
-
+ audio = document.createElement('audio');
+ audio.id = id;
+ audio.preload = "auto";
+ audio.volume = Crafty.audio.volume;
+ if(typeof url === "string"){
+ audio.src = url;
+ if (!Crafty.assets[url]) Crafty.assets[url] = audio;
+ }
+
+ if(typeof url === "object"){
+ for(src in url){
+ path = url[src];
+ ext = path.substr(path.lastIndexOf('.') + 1).toLowerCase();
+ source = document.createElement('source');
+ source.src = path;
+ source.type=this.srcType[ext];
+ audio.appendChild(source);
+ if (!Crafty.assets[path]) Crafty.assets[path] = audio;
+
+ }
+ }
+ this.sounds[id] = {
+ obj:audio,
+ played:0
+ }
+ audio.load();
}
-
+
},
play:function(id,repeat,volume){
if(repeat == 0 || !Crafty.support.audio || !this.sounds[id]) return;
-
var s = this.sounds[id];
-
s.obj.volume = Crafty.audio.volume || volume;
- if(s.obj.currentTime) s.obj.currentTime = 0;
-
+ if(s.obj.currentTime) s.obj.currentTime = 0;
s.obj.play();
s.played ++;
-
- function r(){
+ s.obj.onended = function(){
if(s.played < repeat || repeat == -1){
if(this.currentTime) this.currentTime = 0;
this.play();
s.played ++;
}
-
- }
- if (s.attachEvent) { //IE
- s.obj.attachEvent('onended', r);
- } else { //Everyone else
- s.obj.addEventListener('ended', r, false);
+ };
+ },
+ mute:function(){
+ var s;
+ if(!this.muted){
+ for(var i in this.sounds){
+ s = this.sounds[i];
+ s.obj.pause();
+ }
+ this.muted = true;
+ }else{
+ for(var i in this.sounds){
+ s = this.sounds[i];
+ if(s.obj.currentTime && s.obj.currentTime > 0)
+ this.sounds[i].obj.play();
+ }
+ this.muted = false;
}
- return s;
+
}
}
});
View
4 assets/js/crafty.js
@@ -1070,7 +1070,9 @@
}
return id;
}
-
+ if(window.opera){
+ console = {log:opera.postError}
+ }
/**
* Clone an Object
*/
View
1  assets/js/levels.js
@@ -55,6 +55,7 @@ Crafty.scene("Loading",function(){
//uh oh, error loading
console.log("Error on loading: ");
console.log(e.obj);
+
}
);
View
109 assets/js/loader.js
@@ -51,11 +51,40 @@ Crafty.extend({
*/
load: function (data, oncomplete, onprogress, onerror) {
- var i = 0, l = data.length, current, obj, total = l, j = 0, ext = "",event,canplay;
-
+ var i = 0, l = data.length, current, obj, total = l, j = 0, ext = "" ,audio,canplay;
+
+ //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
+ });
+
+ if(j === total && oncomplete) oncomplete();
+ };
+ //Error function
+ function err(){
+
+ if (onerror)
+ onerror.call(this,{
+ loaded: j,
+ total: total,
+ percent: (j / total * 100),
+ obj:this
+ });
+
+ j++;
+ if(j === total && oncomplete) oncomplete();
+ };
+
for (; i < l; ++i) {
current = data[i];
- event = '';
ext = current.substr(current.lastIndexOf('.') + 1).toLowerCase();
if(this.assets[current]){
obj = this.assets[current];
@@ -63,62 +92,34 @@ Crafty.extend({
}else{
obj =null;
}
-
-
if (Crafty.support.audio && (ext === "mp3" || ext === "wav" || ext === "ogg" || ext === "mp4")) {
- event = 'loadedmetadata';
-
- //Chrome has problems with mp3 canplaystate is maybe
- if (navigator.userAgent.indexOf('Chrome') != -1 && ext === "mp3") j++;
-
-
- } else if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png") {
- event = 'load';
+ if(!obj){
+ obj = document.createElement('audio');
+ obj.preload = "auto";
+ obj.volume = Crafty.audio.volume;
+ canplay = obj.canPlayType(Crafty.audio.types[ext]);
+ if(canplay !== "" && canplay !== "no"){
+ obj.src =current;
+ }
+ obj.load();
+ if (!Crafty.assets[current]) Crafty.assets[current] = obj;
+ }
+ //Chrome does not support autoload
+ if (navigator.userAgent.indexOf('Chrome') != -1) pro();
+ obj.onloadeddata=pro;
+
+ } else if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png") {
+ if(!obj){
+ obj = new Image();
+ obj.src = current;
+ if (!Crafty.assets[current]) Crafty.assets[current] = obj;
+ }
+ obj.onload = pro;
} else {
total--;
continue; //skip if not applicable
}
-
- //Progress function
- function pro(){
-
- ++j;
- //if progress callback, give information of assets loaded, total and percent
- if (onprogress)
- onprogress({
- loaded: j,
- total: total,
- percent: (j / total * 100),
- obj:this
- });
-
- if(j === total && oncomplete) oncomplete();
- };
- //Error function
- function err(e){
-
- if (onerror)
- 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, pro);
- obj.attachEvent('onerror', err);
- } else { //Everyone else
- obj.addEventListener(event, pro, false);
- obj.addEventListener('error', err, false);
- }
-
-
+ obj.onerror = err;
}
View
7 assets/js/sounds.js
@@ -82,3 +82,10 @@ Crafty.audio.add({
]
});
+Crafty.audio.add("space2", game_path + "media/music/through-space.mp3");
+
+Crafty.audio.add("space3",[
+ game_path + "media/music/through-space.mp3",
+ game_path + "media/music/through-space.ogg"
+ ]);
+Crafty.audio.add("space4");
Please sign in to comment.
Something went wrong with that request. Please try again.