|
1 | 1 | /** |
2 | 2 | * @author Jeremy Dowell <jeremy@codevinsky.com> |
| 3 | + * @author Richard Davey <rich@photonstorm.com> |
3 | 4 | * @copyright 2014 Photon Storm Ltd. |
4 | 5 | * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} |
5 | 6 | */ |
6 | 7 |
|
7 | 8 | /** |
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 |
9 | 11 | * |
10 | 12 | * @class Phaser.AudioSprite |
11 | 13 | * @constructor |
12 | 14 | * @param {Phaser.Game} game - Reference to the current game instance. |
13 | 15 | * @param {string} key - Asset key for the sound. |
14 | 16 | */ |
| 17 | +Phaser.AudioSprite = function (game, key) { |
15 | 18 |
|
16 | | -Phaser.AudioSprite = function(game, key) { |
| 19 | + /** |
| 20 | + * A reference to the currently running Game. |
| 21 | + * @property {Phaser.Game} game |
| 22 | + */ |
17 | 23 | this.game = game; |
| 24 | + |
| 25 | + /** |
| 26 | + * Asset key for the Audio Sprite. |
| 27 | + * @property {string} key |
| 28 | + */ |
18 | 29 | this.key = key; |
| 30 | + |
| 31 | + /** |
| 32 | + * JSON audio atlas object. |
| 33 | + * @property {object} config |
| 34 | + */ |
19 | 35 | 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 | + */ |
20 | 41 | 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 | + */ |
22 | 54 | this.sounds = {}; |
23 | | - for (var k in this.config.spritemap) { |
| 55 | + |
| 56 | + for (var k in this.config.spritemap) |
| 57 | + { |
24 | 58 | 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); |
30 | 68 | } |
| 69 | + |
31 | 70 | this.sounds[k] = s; |
32 | 71 | } |
33 | | - if (this.config.autoplay) { |
| 72 | + |
| 73 | + if (this.config.autoplay) |
| 74 | + { |
34 | 75 | this.autoplayKey = this.config.autoplay; |
35 | 76 | this.play(this.autoplayKey); |
36 | 77 | this.autoplay = this.sounds[this.autoplayKey]; |
37 | 78 | } |
| 79 | + |
38 | 80 | }; |
39 | 81 |
|
40 | 82 | Phaser.AudioSprite.prototype = { |
| 83 | + |
41 | 84 | /** |
42 | | - * Play a sound with the given name |
| 85 | + * Play a sound with the given name. |
| 86 | + * |
43 | 87 | * @method Phaser.AudioSprite#play |
44 | 88 | * @param {string} [marker] - The name of sound to play |
45 | 89 | * @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 | 90 | * @return {Phaser.Sound} This sound instance. |
47 | 91 | */ |
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 | + |
50 | 96 | return this.sounds[marker].play(marker, null, volume); |
| 97 | + |
51 | 98 | }, |
| 99 | + |
52 | 100 | /** |
53 | | - * Play a sound with the given name |
| 101 | + * Stop a sound with the given name. |
| 102 | + * |
54 | 103 | * @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. |
57 | 105 | */ |
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 | + { |
61 | 112 | this.sounds[key].stop(); |
62 | 113 | } |
63 | | - } else { |
| 114 | + } |
| 115 | + else |
| 116 | + { |
64 | 117 | this.sounds[marker].stop(); |
65 | 118 | } |
| 119 | + |
66 | 120 | }, |
| 121 | + |
67 | 122 | /** |
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. |
72 | 128 | */ |
73 | 129 | get: function(marker) { |
| 130 | + |
74 | 131 | return this.sounds[marker]; |
| 132 | + |
75 | 133 | } |
| 134 | + |
76 | 135 | }; |
| 136 | + |
| 137 | +Phaser.AudioSprite.prototype.constructor = Phaser.AudioSprite; |
0 commit comments