2014-09-23 15:21:29 +00:00
|
|
|
/**
|
2014-09-23 15:25:49 +00:00
|
|
|
* @author Jeremy Dowell <jeremy@codevinsky.com>
|
2014-09-23 21:15:09 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-09-23 15:25:49 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
2014-09-23 15:21:29 +00:00
|
|
|
|
|
|
|
/**
|
2014-09-23 21:15:09 +00:00
|
|
|
* Audio Sprites are a combination of audio files and a JSON configuration.
|
|
|
|
* The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
|
2014-09-23 15:25:49 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.AudioSprite
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - Reference to the current game instance.
|
|
|
|
* @param {string} key - Asset key for the sound.
|
|
|
|
*/
|
2014-09-23 21:15:09 +00:00
|
|
|
Phaser.AudioSprite = function (game, key) {
|
2014-09-23 15:21:29 +00:00
|
|
|
|
2014-09-23 21:15:09 +00:00
|
|
|
/**
|
|
|
|
* A reference to the currently running Game.
|
|
|
|
* @property {Phaser.Game} game
|
|
|
|
*/
|
2014-09-23 15:25:49 +00:00
|
|
|
this.game = game;
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asset key for the Audio Sprite.
|
|
|
|
* @property {string} key
|
|
|
|
*/
|
2014-09-23 15:25:49 +00:00
|
|
|
this.key = key;
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JSON audio atlas object.
|
|
|
|
* @property {object} config
|
|
|
|
*/
|
2014-09-23 15:25:49 +00:00
|
|
|
this.config = this.game.cache.getJSON(key + '-audioatlas');
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If a sound is set to auto play, this holds the marker key of it.
|
|
|
|
* @property {string} autoplayKey
|
|
|
|
*/
|
2014-09-23 15:25:49 +00:00
|
|
|
this.autoplayKey = null;
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is a sound set to autoplay or not?
|
|
|
|
* @property {boolean} autoplay
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.autoplay = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object containing the Phaser.Sound objects for the Audio Sprite.
|
|
|
|
* @property {object} sounds
|
|
|
|
*/
|
2014-09-23 15:25:49 +00:00
|
|
|
this.sounds = {};
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
for (var k in this.config.spritemap)
|
|
|
|
{
|
2014-09-23 15:25:49 +00:00
|
|
|
var marker = this.config.spritemap[k];
|
2014-09-23 21:15:09 +00:00
|
|
|
var sound = this.game.add.sound(this.key);
|
|
|
|
|
|
|
|
if (marker.loop)
|
|
|
|
{
|
|
|
|
sound.addMarker(k, marker.start, (marker.end - marker.start), null, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sound.addMarker(k, marker.start, (marker.end - marker.start), null, false);
|
2014-09-23 15:25:49 +00:00
|
|
|
}
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 21:28:15 +00:00
|
|
|
this.sounds[k] = sound;
|
2014-09-23 15:25:49 +00:00
|
|
|
}
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
if (this.config.autoplay)
|
|
|
|
{
|
2014-09-23 15:25:49 +00:00
|
|
|
this.autoplayKey = this.config.autoplay;
|
|
|
|
this.play(this.autoplayKey);
|
|
|
|
this.autoplay = this.sounds[this.autoplayKey];
|
2014-09-23 15:21:29 +00:00
|
|
|
}
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:21:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.AudioSprite.prototype = {
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
/**
|
2014-09-23 21:15:09 +00:00
|
|
|
* Play a sound with the given name.
|
|
|
|
*
|
2014-09-23 15:25:49 +00:00
|
|
|
* @method Phaser.AudioSprite#play
|
|
|
|
* @param {string} [marker] - The name of sound to play
|
|
|
|
* @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).
|
|
|
|
* @return {Phaser.Sound} This sound instance.
|
|
|
|
*/
|
2014-09-23 21:15:09 +00:00
|
|
|
play: function (marker, volume) {
|
|
|
|
|
|
|
|
if (typeof volume === 'undefined') { volume = 1; }
|
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
return this.sounds[marker].play(marker, null, volume);
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
},
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
/**
|
2014-09-23 21:15:09 +00:00
|
|
|
* Stop a sound with the given name.
|
|
|
|
*
|
2014-09-23 15:25:49 +00:00
|
|
|
* @method Phaser.AudioSprite#stop
|
2014-09-23 21:15:09 +00:00
|
|
|
* @param {string} [marker=''] - The name of sound to stop. If none is given it will stop all sounds in the audio sprite.
|
2014-09-23 15:25:49 +00:00
|
|
|
*/
|
2014-09-23 21:15:09 +00:00
|
|
|
stop: function (marker) {
|
|
|
|
|
|
|
|
if (!marker)
|
|
|
|
{
|
|
|
|
for (var key in this.sounds)
|
|
|
|
{
|
2014-09-23 15:25:49 +00:00
|
|
|
this.sounds[key].stop();
|
|
|
|
}
|
2014-09-23 21:15:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-23 15:25:49 +00:00
|
|
|
this.sounds[marker].stop();
|
|
|
|
}
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
},
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
/**
|
2014-09-23 21:15:09 +00:00
|
|
|
* Get a sound with the given name.
|
|
|
|
*
|
|
|
|
* @method Phaser.AudioSprite#get
|
|
|
|
* @param {string} marker - The name of sound to get.
|
|
|
|
* @return {Phaser.Sound} The sound instance.
|
2014-09-23 15:25:49 +00:00
|
|
|
*/
|
|
|
|
get: function(marker) {
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
return this.sounds[marker];
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:21:29 +00:00
|
|
|
}
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:21:29 +00:00
|
|
|
};
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
Phaser.AudioSprite.prototype.constructor = Phaser.AudioSprite;
|