Skip to content

Commit be07fb3

Browse files
committed
Merge pull request phaserjs#1205 from codevinsky/feature/audiosprite
Phaser.AudioSprite
2 parents ba7f33d + 7cce136 commit be07fb3

5 files changed

Lines changed: 135 additions & 11 deletions

File tree

src/gameobjects/GameObjectCreator.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ Phaser.GameObjectCreator.prototype = {
124124
return this.game.sound.add(key, volume, loop, connect);
125125

126126
},
127+
/**
128+
* Creates a new AudioSprite object.
129+
*
130+
* @method Phaser.GameObjectCreator#audioSrpite
131+
* @param {string} key - The Game.cache key of the sound that this object will use.
132+
* @return {Phaser.AudioSprite} The newly created audioSprite object.
133+
*/
134+
audiosprite: function(key) {
135+
return this.game.sound.addSprite(key);
136+
},
127137

128138
/**
129139
* Creates a new Sound object.

src/gameobjects/GameObjectFactory.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ Phaser.GameObjectFactory.prototype = {
179179

180180
},
181181

182+
/**
183+
* Creates a new AudioSprite object.
184+
*
185+
* @method Phaser.GameObjectCreator#audioSrpite
186+
* @param {string} key - The Game.cache key of the sound that this object will use.
187+
* @return {Phaser.AudioSprite} The newly created audioSprite object.
188+
*/
189+
audiosprite: function(key) {
190+
return this.game.sound.addSprite(key);
191+
},
192+
182193
/**
183194
* Creates a new TileSprite object.
184195
*

src/loader/Loader.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* The Loader handles loading all external content such as Images, Sounds, Texture Atlases and data files.
1010
* It uses a combination of Image() loading and xhr and provides progress and completion callbacks.
11-
*
11+
*
1212
* @class Phaser.Loader
1313
* @constructor
1414
* @param {Phaser.Game} game - A reference to the currently running game.
@@ -48,7 +48,7 @@ Phaser.Loader = function (game) {
4848
* You can optionally link a sprite to the preloader.
4949
* If you do so the Sprites width or height will be cropped based on the percentage loaded.
5050
* This property is an object containing: sprite, rect, direction, width and height
51-
*
51+
*
5252
* @property {object} preloadSprite
5353
*/
5454
this.preloadSprite = null;
@@ -94,10 +94,10 @@ Phaser.Loader = function (game) {
9494
this.onLoadComplete = new Phaser.Signal();
9595

9696
/**
97-
* @property {Phaser.Signal} onPackComplete - This event is dispatched when an asset pack has either loaded or failed.
97+
* @property {Phaser.Signal} onPackComplete - This event is dispatched when an asset pack has either loaded or failed.
9898
*/
9999
this.onPackComplete = new Phaser.Signal();
100-
100+
101101
/**
102102
* @property {boolean} useXDomainRequest - If true and if the browser supports XDomainRequest, it will be used in preference for xhr when loading json files. It is enabled automatically if the browser is IE9, but you can disable it as required.
103103
*/
@@ -215,7 +215,7 @@ Phaser.Loader.prototype = {
215215
/**
216216
* Check whether asset exists with a specific key.
217217
* Use Phaser.Cache to access loaded assets, e.g. Phaser.Cache#checkImageKey
218-
*
218+
*
219219
* @method Phaser.Loader#checkKeyExists
220220
* @param {string} type - The type asset you want to check.
221221
* @param {string} key - Key of the asset you want to check.
@@ -614,6 +614,21 @@ Phaser.Loader.prototype = {
614614

615615
},
616616

617+
/**
618+
* Add a new audiosprite file to the loader.
619+
*
620+
* @method Phaser.Loader#audiosprite
621+
* @param {string} key - Unique asset key of the audio file.
622+
* @param {Array|string} urls - An array containing the URLs of the audio files, i.e.: [ 'audiosprite.mp3', 'audiosprite.ogg', 'audiosprite.m4a' ] or a single string containing just one URL.
623+
* @param {string} atlasurl - the url containing the audiosprite configuration json
624+
* @return {Phaser.Loader} This Loader instance.
625+
*/
626+
audiosprite: function(key, urls, atlasurl) {
627+
this.audio(key, urls);
628+
this.json(key + '-audioatlas', atlasurl);
629+
return this;
630+
},
631+
617632
/**
618633
* Add a new tilemap loading request.
619634
*
@@ -1000,7 +1015,7 @@ Phaser.Loader.prototype = {
10001015
console.warn('Phaser.Loader loadPackList invalid index ' + this._packIndex);
10011016
return;
10021017
}
1003-
1018+
10041019
var pack = this._packList[this._packIndex];
10051020

10061021
if (pack.data !== null)
@@ -1179,7 +1194,7 @@ Phaser.Loader.prototype = {
11791194
console.warn('Phaser.Loader loadFile invalid index ' + this._fileIndex);
11801195
return;
11811196
}
1182-
1197+
11831198
var file = this._fileList[this._fileIndex];
11841199
var _this = this;
11851200

@@ -1263,7 +1278,7 @@ Phaser.Loader.prototype = {
12631278
this._ajax.onerror = function () {
12641279
return _this.dataLoadError(_this._fileIndex);
12651280
};
1266-
1281+
12671282
this._ajax.ontimeout = function () {
12681283
return _this.dataLoadError(_this._fileIndex);
12691284
};
@@ -1276,7 +1291,7 @@ Phaser.Loader.prototype = {
12761291

12771292
this._ajax.open('GET', this.baseURL + file.url, true);
12781293

1279-
// Note: The xdr.send() call is wrapped in a timeout to prevent an issue with the interface where some requests are lost
1294+
// Note: The xdr.send() call is wrapped in a timeout to prevent an issue with the interface where some requests are lost
12801295
// if multiple XDomainRequests are being sent at the same time.
12811296
setTimeout(function () {
12821297
this._ajax.send();
@@ -1325,7 +1340,7 @@ Phaser.Loader.prototype = {
13251340

13261341
/**
13271342
* Starts the xhr loader.
1328-
*
1343+
*
13291344
* @method Phaser.Loader#xhrLoad
13301345
* @private
13311346
* @param {number} index - The index of the file to load from the file list.
@@ -1355,7 +1370,7 @@ Phaser.Loader.prototype = {
13551370

13561371
/**
13571372
* Private method ONLY used by loader.
1358-
*
1373+
*
13591374
* @method Phaser.Loader#getAudioURL
13601375
* @private
13611376
* @param {array|string} urls - Either an array of audio file URLs or a string containing a single URL path.

src/sound/AudioSprite.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* @author Jeremy Dowell <jeremy@codevinsky.com>
3+
* @copyright 2014 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* The AudioSprite class constructor.
9+
*
10+
* @class Phaser.AudioSprite
11+
* @constructor
12+
* @param {Phaser.Game} game - Reference to the current game instance.
13+
* @param {string} key - Asset key for the sound.
14+
*/
15+
16+
Phaser.AudioSprite = function(game, key) {
17+
this.game = game;
18+
this.key = key;
19+
this.config = this.game.cache.getJSON(key + '-audioatlas');
20+
this.autoplayKey = null;
21+
this.autoplay = null;
22+
this.sounds = {};
23+
for (var k in this.config.spritemap) {
24+
var marker = this.config.spritemap[k];
25+
var s = this.game.add.sound(this.key);
26+
if (marker.loop) {
27+
s.addMarker(k, marker.start, (marker.end - marker.start), null, true);
28+
} else {
29+
s.addMarker(k, marker.start, (marker.end - marker.start), null, false);
30+
}
31+
this.sounds[k] = s;
32+
}
33+
if (this.config.autoplay) {
34+
this.autoplayKey = this.config.autoplay;
35+
this.play(this.autoplayKey);
36+
this.autoplay = this.sounds[this.autoplayKey];
37+
}
38+
};
39+
40+
Phaser.AudioSprite.prototype = {
41+
/**
42+
* Play a sound with the given name
43+
* @method Phaser.AudioSprite#play
44+
* @param {string} [marker] - The name of sound to play
45+
* @param {number} [volume=1] - Volume of the sound you want to play. If none is given it will use the volume given to the Sound when it was created (which defaults to 1 if none was specified).
46+
* @return {Phaser.Sound} This sound instance.
47+
*/
48+
play: function(marker, volume) {
49+
volume = typeof volume === 'undefined' ? 1 : volume;
50+
return this.sounds[marker].play(marker, null, volume);
51+
},
52+
/**
53+
* Play a sound with the given name
54+
* @method Phaser.AudioSprite#stop
55+
* @param {string} [marker=''] - The name of sound to stop. If none is given, stop all sounds in the audiosprite
56+
* @return {Phaser.Sound} This sound instance.
57+
*/
58+
stop: function(marker) {
59+
if (!marker) {
60+
for (var key in this.sounds) {
61+
this.sounds[key].stop();
62+
}
63+
} else {
64+
this.sounds[marker].stop();
65+
}
66+
},
67+
/**
68+
* Get a sound with the given name
69+
* @method Phaser.AudioSprite#play
70+
* @param {string} [marker] - The name of sound to get
71+
* @return {Phaser.Sound} This sound instance.
72+
*/
73+
get: function(marker) {
74+
return this.sounds[marker];
75+
}
76+
};

src/sound/SoundManager.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ Phaser.SoundManager.prototype = {
366366

367367
},
368368

369+
/**
370+
* Adds a new AudioSprite into the SoundManager.
371+
*
372+
* @method Phaser.SoundManager#add
373+
* @param {string} key - Asset key for the sound.
374+
* @return {Phaser.AudioSprite} The new AudioSprite instance.
375+
*/
376+
addSprite: function(key) {
377+
var audioSprite = new Phaser.AudioSprite(this.game, key);
378+
return audioSprite;
379+
},
380+
369381
/**
370382
* Removes a Sound from the SoundManager. The removed Sound is destroyed before removal.
371383
*

0 commit comments

Comments
 (0)