phaser/src/sound/AudioSprite.js

138 lines
3.5 KiB
JavaScript
Raw Normal View History

/**
2014-09-23 15:25:49 +00:00
* @author Jeremy Dowell <jeremy@codevinsky.com>
* @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}
*/
/**
* 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.
*/
Phaser.AudioSprite = function (game, key) {
/**
* A reference to the currently running Game.
* @property {Phaser.Game} game
*/
2014-09-23 15:25:49 +00:00
this.game = game;
/**
* Asset key for the Audio Sprite.
* @property {string} key
*/
2014-09-23 15:25:49 +00:00
this.key = key;
/**
* JSON audio atlas object.
* @property {object} config
*/
2014-09-23 15:25:49 +00:00
this.config = this.game.cache.getJSON(key + '-audioatlas');
/**
* 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;
/**
* 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 = {};
for (var k in this.config.spritemap)
{
2014-09-23 15:25:49 +00:00
var marker = this.config.spritemap[k];
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:28:15 +00:00
this.sounds[k] = sound;
2014-09-23 15:25:49 +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];
}
};
Phaser.AudioSprite.prototype = {
2014-09-23 15:25:49 +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.
*/
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 15:25:49 +00:00
},
2014-09-23 15:25:49 +00:00
/**
* Stop a sound with the given name.
*
2014-09-23 15:25:49 +00:00
* @method Phaser.AudioSprite#stop
* @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
*/
stop: function (marker) {
if (!marker)
{
for (var key in this.sounds)
{
2014-09-23 15:25:49 +00:00
this.sounds[key].stop();
}
}
else
{
2014-09-23 15:25:49 +00:00
this.sounds[marker].stop();
}
2014-09-23 15:25:49 +00:00
},
2014-09-23 15:25:49 +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 15:25:49 +00:00
return this.sounds[marker];
}
};
Phaser.AudioSprite.prototype.constructor = Phaser.AudioSprite;