Permalink
Browse files

Fixed Preloader and Audio (i hope)

  • Loading branch information...
1 parent aeb6f48 commit 7497b5d34583ae6c9f56eacdd71bd71826893b0b @BlackScorp BlackScorp committed Mar 8, 2012
Showing with 99 additions and 130 deletions.
  1. +46 −30 assets/js/audio.js
  2. +10 −14 assets/js/levels.js
  3. +39 −29 assets/js/loader.js
  4. +3 −51 assets/js/sounds.js
  5. +1 −6 assets/js/sprites.js
View
@@ -1,52 +1,68 @@
Crafty.extend({
audio:{
sounds:{},
- type: {
- 'mp3': 'audio/mpeg; codecs="mp3"',
- 'ogg': 'audio/ogg; codecs="vorbis"',
- 'wav': 'audio/wav; codecs="1"',
- 'mp4': 'audio/mp4; codecs="mp4a.40.2"'
- },
- srcType: {
- 'mp3': 'audio/mpeg',
- 'ogg': 'audio/ogg',
- 'wav': 'audio/wav',
- 'mp4': 'audio/mp4'
+ supported:{},
+ codecs :{ // Chart from jPlayer
+ ogg: 'audio/ogg; codecs="vorbis"', //OGG
+ wav: 'audio/wav; codecs="1"', // PCM
+ webma: 'audio/webm; codecs="vorbis"',// WEBM
+ mp3: 'audio/mpeg; codecs="mp3"', //MP3
+ m4a: 'audio/mp4; codecs="mp4a.40.2"'// AAC / MP4
},
volume:1, //Global Volume
muted:false,
+ canPlay:function(){
+ var audio = this.audioElement(),canplay;
+ for(var i in this.codecs){
+ canplay = audio.canPlayType(this.codecs[i]);
+ if(canplay !== "" && canplay !== "no"){
+ this.supported[i] = true;
+ }else{
+ this.supported[i] = false;
+ }
+ }
+
+ },
+ audioElement:function(){
+ //IE does not support Audio Object
+ return typeof Audio !== 'undefined' ? new Audio() : document.createElement('audio');
+ },
add:function(id,url){
+ Crafty.support.audio = !!this.audioElement().canPlayType; //Setup audio support
if (!Crafty.support.audio) return;
+ this.canPlay(); //Setup supported Extensions
+
var audio,source,ext,path;
if(arguments.length === 1 && typeof id === "object"){
for(var i in id){
- audio = document.createElement('audio');
- audio.id = i;
- audio.preload = "auto";
- audio.volume = Crafty.audio.volume;
for(var src in id[i]){
+ audio = this.audioElement();
+ audio.id = i;
+ audio.preload = "auto";
+ audio.volume = Crafty.audio.volume;
path = id[i][src];
- ext = path.substr(path.lastIndexOf('.') + 1).toLowerCase();
- source = document.createElement('source');
- source.src = path;
- source.type=this.srcType[ext];
- audio.appendChild(source);
+ ext = path.substr(path.lastIndexOf('.') + 1).toLowerCase();
+ if(this.supported[ext]){
+ audio.src = path;
+ if (!Crafty.assets[path]) Crafty.assets[path] = audio;
+ this.sounds[i] = {
+ obj:audio,
+ played:0
+ }
+ }
+
}
- this.sounds[i] = {
- obj:audio,
- played:0
- }
- audio.load();
-
}
}
if(typeof id === "string"){
- audio = document.createElement('audio');
+
+ audio = this.audioElement();
audio.id = id;
audio.preload = "auto";
audio.volume = Crafty.audio.volume;
- if(typeof url === "string"){
+ ext = url.substr(url.lastIndexOf('.') + 1).toLowerCase();
+ if(typeof url === "string" && this.supported[ext]){
audio.src = url;
if (!Crafty.assets[url]) Crafty.assets[url] = audio;
}
@@ -67,10 +83,10 @@ Crafty.extend({
obj:audio,
played:0
}
- audio.load();
+
}
-
+
},
play:function(id,repeat,volume){
if(repeat == 0 || !Crafty.support.audio || !this.sounds[id]) return;
View
@@ -3,13 +3,13 @@
*/
//Loading Scene
Crafty.scene("Loading",function(){
- var toLoad = [];
+ var toLoad = [];
+ toLoad.push(game_path + "assets/img/loading.jpg", game_path + "assets/img/bg.png");
for(var i in Crafty.assets){
toLoad.push(i);
}
-
-
- //Setup background image
+
+ //Setup background image
Crafty.background("url("+game_path+"assets/img/loading.jpg) black");
//Select DOM elements
@@ -31,34 +31,30 @@ Crafty.scene("Loading",function(){
//Start scene level 1
Crafty.scene("Level1");
});
-
+
Crafty.load(toLoad,
function() {
//Everything is loaded
bar.fadeOut(2000,function(){
button.show();
});
},
-
function(e) {
-
//update progress
- text.text("Loading... "+~~e.percent+"%");
+ text.text("Loading "+e.src.substr(e.src.lastIndexOf('/') + 1).toLowerCase()+" Loaded: "+~~e.percent+"%");
bar.progressbar({
value:~~e.percent
});
-
+ // console.log("Loaded: ");
+ // console.log(e.src);
},
-
function(e) {
-
//uh oh, error loading
console.log("Error on loading: ");
- console.log(e.obj);
-
+ console.log(e.src);
}
);
-
+
});
//Level 1 Scene
Crafty.scene("Level1",function(){
View
@@ -52,74 +52,84 @@ Crafty.extend({
load: function (data, oncomplete, onprogress, onerror) {
var i = 0, l = data.length, current, obj, total = l, j = 0, ext = "" ,audio,canplay;
-
+
//Progress function
function pro(){
-
+
+
+ var src = this.currentSrc || this.src;
+ if (this.removeEventListener) {
+ this.removeEventListener('canplaythrough', pro, true);
+ } else if (this.detachEvent) {
+ this.detachEvent('oncanplaythrough', pro);
+ }
+ this.onload=null;
++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
+ src:src
});
if(j === total && oncomplete) oncomplete();
};
//Error function
function err(){
-
+ var src = this.src;
if (onerror)
onerror.call(this,{
loaded: j,
total: total,
percent: (j / total * 100),
- obj:this
+ src:src
});
-
+
j++;
if(j === total && oncomplete) oncomplete();
};
for (; i < l; ++i) {
current = data[i];
ext = current.substr(current.lastIndexOf('.') + 1).toLowerCase();
- if(this.assets[current]){
- obj = this.assets[current];
+
+ obj = this.assets[current] || null;
+
+ if (Crafty.support.audio && Crafty.audio.supported[ext]) {
- }else{
- obj =null;
- }
- if (Crafty.support.audio && (ext === "mp3" || ext === "wav" || ext === "ogg" || ext === "mp4")) {
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;
+ var name = current.substr(current.lastIndexOf('/') + 1).toLowerCase();
+ obj = Crafty.audio.audioElement();
+ obj.id = name;
+ obj.src = current;
+ if (!Crafty.assets[current]) Crafty.assets[current] = obj;
+ Crafty.audio.sounds[name] = {
+ obj:obj,
+ played:0
+ }
}
- //Chrome does not support autoload
- if (navigator.userAgent.indexOf('Chrome') != -1) pro();
- obj.onloadeddata=pro;
-
+ if (obj.addEventListener) {
+ obj.addEventListener('canplaythrough', pro, true);
+ } else if (obj.attachEvent) {
+ obj.attachEvent('oncanplaythrough', pro);
+ }
+ obj.load();
} else if (ext === "jpg" || ext === "jpeg" || ext === "gif" || ext === "png") {
- if(!obj){
+ if(!obj) {
obj = new Image();
- obj.src = current;
if (!Crafty.assets[current]) Crafty.assets[current] = obj;
}
- obj.onload = pro;
+ obj.onload=pro;
+ obj.src = current; //setup src after onload function Opera/IE Bug
} else {
total--;
continue; //skip if not applicable
}
- obj.onerror = err;
+ obj.onerror = err;
+
+
}
View
@@ -1,52 +1,4 @@
-/*Crafty.audio.add({
- space: [
- game_path + "media/music/through-space.mp3",
- game_path + "media/music/through-space.ogg"
- ]
-});
-Crafty.audio.add({
- spaceship: [
- game_path + "media/music/spaceship.mp3",
- game_path + "media/music/spaceship.ogg"
- ]
-});
-Crafty.audio.add({
- explosion0: [
- game_path + "media/sounds/explodemini.mp3",
- game_path + "media/sounds/explodemini.ogg"
- ]
-});
-Crafty.audio.add({
- explosion1: [
- game_path + "media/sounds/explode.mp3",
- game_path + "media/sounds/explode.ogg"
- ]
-});
-Crafty.audio.add({
- laser1: [
- game_path + "media/sounds/laser1.mp3",
- game_path + "media/sounds/laser1.ogg"
- ]
-});
-Crafty.audio.add({
- laser2: [
- game_path + "media/sounds/laser2.mp3",
- game_path + "media/sounds/laser2.ogg"
- ]
-});
-Crafty.audio.add({
- laser3: [
- game_path + "media/sounds/laser3.mp3",
- game_path + "media/sounds/laser3.ogg"
- ]
-});
-Crafty.audio.add({
- laser4: [
- game_path + "media/sounds/laser4.mp3",
- game_path + "media/sounds/laser4.ogg"
- ]
-});
-*/
+
Crafty.audio.add({
space: [
game_path + "media/music/through-space.mp3",
@@ -81,11 +33,11 @@ Crafty.audio.add({
game_path + "media/sounds/laser4.ogg"
]
});
-
+/*
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");
+Crafty.audio.add("space4");*/
View
@@ -1,9 +1,4 @@
-Crafty.sprite(game_path + "assets/img/loading.jpg",{
- loadingbg:[0,0]
-})
-Crafty.sprite(game_path + "assets/img/bg.png",{
- spacebg1:[0,0]
-})
+
Crafty.sprite(game_path + "assets/img/ships.png",{
//Gold
ship1:[0,0,44,47],

0 comments on commit 7497b5d

Please sign in to comment.