Skip to content

Commit 01fd3df

Browse files
committed
AudioSprite support is now built into the Loader and SoundManager. AudioSprites are like sprite sheets, only they consist of a selection of audio files and markers in a json configuration. You can find more details at https://github.com/tonistiigi/audiosprite (thanks @codevinsky phaserjs#1205)
Fixed AudioSprite jsdoc, casing and formatting issues.
1 parent be07fb3 commit 01fd3df

6 files changed

Lines changed: 110 additions & 35 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Version 2.1.2 - "Whitebridge" - in development
8181
* Loader can now natively load XML files via `load.xml`. Once the XML file has loaded it is parsed via either DOMParser or ActiveXObject and then added to the Cache, where it can be retrieved via `cache.getXML(key)`.
8282
* Cache now has support for XML files stored in their own container. You can add them with `cache.addXML` (typically this is done from the Loader automatically for you) and get them with `cache.getXML(key)`. There is also `cache.checkXMLKey(key)`, `cache.checkKeys` and `cache.removeXML(key)`.
8383
* Rectangle.aabb is a new method that will take an array of Points and return a Rectangle that matches the AABB (bounding area) of the Points (thanks @codevinsky #1199)
84+
* AudioSprite support is now built into the Loader and SoundManager. AudioSprites are like sprite sheets, only they consist of a selection of audio files and markers in a json configuration. You can find more details at https://github.com/tonistiigi/audiosprite (thanks @codevinsky #1205)
8485

8586

8687
### Updates

src/gameobjects/GameObjectCreator.js

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

126126
},
127+
127128
/**
128129
* Creates a new AudioSprite object.
129130
*
130-
* @method Phaser.GameObjectCreator#audioSrpite
131+
* @method Phaser.GameObjectCreator#audioSprite
131132
* @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+
* @return {Phaser.AudioSprite} The newly created AudioSprite object.
133134
*/
134-
audiosprite: function(key) {
135+
audioSprite: function (key) {
136+
135137
return this.game.sound.addSprite(key);
138+
136139
},
137140

138141
/**

src/gameobjects/GameObjectFactory.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,14 @@ Phaser.GameObjectFactory.prototype = {
182182
/**
183183
* Creates a new AudioSprite object.
184184
*
185-
* @method Phaser.GameObjectCreator#audioSrpite
185+
* @method Phaser.GameObjectFactory#audioSprite
186186
* @param {string} key - The Game.cache key of the sound that this object will use.
187-
* @return {Phaser.AudioSprite} The newly created audioSprite object.
187+
* @return {Phaser.AudioSprite} The newly created AudioSprite object.
188188
*/
189-
audiosprite: function(key) {
189+
audioSprite: function (key) {
190+
190191
return this.game.sound.addSprite(key);
192+
191193
},
192194

193195
/**

src/loader/Loader.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,18 +615,23 @@ Phaser.Loader.prototype = {
615615
},
616616

617617
/**
618-
* Add a new audiosprite file to the loader.
618+
* Add a new audiosprite file to the loader. Audio Sprites are a combination of audio files and a JSON configuration.
619+
* The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
619620
*
620621
* @method Phaser.Loader#audiosprite
621622
* @param {string} key - Unique asset key of the audio file.
622623
* @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+
* @param {string} atlasURL - The URL of the audiosprite configuration json.
624625
* @return {Phaser.Loader} This Loader instance.
625626
*/
626-
audiosprite: function(key, urls, atlasurl) {
627+
audiosprite: function(key, urls, atlasURL) {
628+
627629
this.audio(key, urls);
628-
this.json(key + '-audioatlas', atlasurl);
630+
631+
this.json(key + '-audioatlas', atlasURL);
632+
629633
return this;
634+
630635
},
631636

632637
/**

src/sound/AudioSprite.js

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,137 @@
11
/**
22
* @author Jeremy Dowell <jeremy@codevinsky.com>
3+
* @author Richard Davey <rich@photonstorm.com>
34
* @copyright 2014 Photon Storm Ltd.
45
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
56
*/
67

78
/**
8-
* The AudioSprite class constructor.
9+
* Audio Sprites are a combination of audio files and a JSON configuration.
10+
* The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
911
*
1012
* @class Phaser.AudioSprite
1113
* @constructor
1214
* @param {Phaser.Game} game - Reference to the current game instance.
1315
* @param {string} key - Asset key for the sound.
1416
*/
17+
Phaser.AudioSprite = function (game, key) {
1518

16-
Phaser.AudioSprite = function(game, key) {
19+
/**
20+
* A reference to the currently running Game.
21+
* @property {Phaser.Game} game
22+
*/
1723
this.game = game;
24+
25+
/**
26+
* Asset key for the Audio Sprite.
27+
* @property {string} key
28+
*/
1829
this.key = key;
30+
31+
/**
32+
* JSON audio atlas object.
33+
* @property {object} config
34+
*/
1935
this.config = this.game.cache.getJSON(key + '-audioatlas');
36+
37+
/**
38+
* If a sound is set to auto play, this holds the marker key of it.
39+
* @property {string} autoplayKey
40+
*/
2041
this.autoplayKey = null;
21-
this.autoplay = null;
42+
43+
/**
44+
* Is a sound set to autoplay or not?
45+
* @property {boolean} autoplay
46+
* @default
47+
*/
48+
this.autoplay = false;
49+
50+
/**
51+
* An object containing the Phaser.Sound objects for the Audio Sprite.
52+
* @property {object} sounds
53+
*/
2254
this.sounds = {};
23-
for (var k in this.config.spritemap) {
55+
56+
for (var k in this.config.spritemap)
57+
{
2458
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);
59+
var sound = this.game.add.sound(this.key);
60+
61+
if (marker.loop)
62+
{
63+
sound.addMarker(k, marker.start, (marker.end - marker.start), null, true);
64+
}
65+
else
66+
{
67+
sound.addMarker(k, marker.start, (marker.end - marker.start), null, false);
3068
}
69+
3170
this.sounds[k] = s;
3271
}
33-
if (this.config.autoplay) {
72+
73+
if (this.config.autoplay)
74+
{
3475
this.autoplayKey = this.config.autoplay;
3576
this.play(this.autoplayKey);
3677
this.autoplay = this.sounds[this.autoplayKey];
3778
}
79+
3880
};
3981

4082
Phaser.AudioSprite.prototype = {
83+
4184
/**
42-
* Play a sound with the given name
85+
* Play a sound with the given name.
86+
*
4387
* @method Phaser.AudioSprite#play
4488
* @param {string} [marker] - The name of sound to play
4589
* @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).
4690
* @return {Phaser.Sound} This sound instance.
4791
*/
48-
play: function(marker, volume) {
49-
volume = typeof volume === 'undefined' ? 1 : volume;
92+
play: function (marker, volume) {
93+
94+
if (typeof volume === 'undefined') { volume = 1; }
95+
5096
return this.sounds[marker].play(marker, null, volume);
97+
5198
},
99+
52100
/**
53-
* Play a sound with the given name
101+
* Stop a sound with the given name.
102+
*
54103
* @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.
104+
* @param {string} [marker=''] - The name of sound to stop. If none is given it will stop all sounds in the audio sprite.
57105
*/
58-
stop: function(marker) {
59-
if (!marker) {
60-
for (var key in this.sounds) {
106+
stop: function (marker) {
107+
108+
if (!marker)
109+
{
110+
for (var key in this.sounds)
111+
{
61112
this.sounds[key].stop();
62113
}
63-
} else {
114+
}
115+
else
116+
{
64117
this.sounds[marker].stop();
65118
}
119+
66120
},
121+
67122
/**
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.
123+
* Get a sound with the given name.
124+
*
125+
* @method Phaser.AudioSprite#get
126+
* @param {string} marker - The name of sound to get.
127+
* @return {Phaser.Sound} The sound instance.
72128
*/
73129
get: function(marker) {
130+
74131
return this.sounds[marker];
132+
75133
}
134+
76135
};
136+
137+
Phaser.AudioSprite.prototype.constructor = Phaser.AudioSprite;

src/sound/SoundManager.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,13 +369,16 @@ Phaser.SoundManager.prototype = {
369369
/**
370370
* Adds a new AudioSprite into the SoundManager.
371371
*
372-
* @method Phaser.SoundManager#add
372+
* @method Phaser.SoundManager#addSprite
373373
* @param {string} key - Asset key for the sound.
374374
* @return {Phaser.AudioSprite} The new AudioSprite instance.
375375
*/
376376
addSprite: function(key) {
377+
377378
var audioSprite = new Phaser.AudioSprite(this.game, key);
379+
378380
return audioSprite;
381+
379382
},
380383

381384
/**

0 commit comments

Comments
 (0)