Skip to content

Commit 8983c93

Browse files
committed
Added AudioSprite loader.
1 parent db515d8 commit 8983c93

2 files changed

Lines changed: 61 additions & 14 deletions

File tree

src/loader/filetypes/AudioFile.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,42 +59,45 @@ var AudioFile = new Class({
5959

6060
});
6161

62-
// When registering a factory function 'this' refers to the Loader context.
63-
//
64-
// There are several properties available to use:
65-
//
66-
// this.scene - a reference to the Scene that owns the GameObjectFactory
67-
68-
FileTypesManager.register('audio', function (key, urls, config, xhrSettings)
62+
AudioFile.create = function (loader, key, urls, config, xhrSettings)
6963
{
70-
var game = this.systems.game;
64+
var game = loader.systems.game;
7165
var audioConfig = game.config.audio;
7266
var deviceAudio = game.device.Audio;
7367

7468
if ((audioConfig && audioConfig.noAudio) || (!deviceAudio.webAudio && !deviceAudio.audioData))
7569
{
7670
console.info('Skipping loading audio \'' + key + '\' since sounds are disabled.');
77-
return this;
71+
return null;
7872
}
7973

8074
var url = AudioFile.findAudioURL(game, urls);
8175

8276
if (!url)
8377
{
8478
console.warn('No supported url provided for audio \'' + key + '\'!');
85-
return this;
79+
return null;
8680
}
8781

88-
var audioFile;
89-
9082
if (deviceAudio.webAudio && !(audioConfig && audioConfig.disableWebAudio))
9183
{
92-
audioFile = new AudioFile(key, url, this.path, xhrSettings, game.sound.context);
84+
return new AudioFile(key, url, this.path, xhrSettings, game.sound.context);
9385
}
9486
else
9587
{
96-
audioFile = new HTML5AudioFile(key, url, this.path, config);
88+
return new HTML5AudioFile(key, url, this.path, config);
9789
}
90+
};
91+
92+
// When registering a factory function 'this' refers to the Loader context.
93+
//
94+
// There are several properties available to use:
95+
//
96+
// this.scene - a reference to the Scene that owns the GameObjectFactory
97+
98+
FileTypesManager.register('audio', function (key, urls, config, xhrSettings)
99+
{
100+
var audioFile = AudioFile.create(this, key, urls, config, xhrSettings);
98101

99102
if (audioFile)
100103
{
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var AudioFile = require('./AudioFile.js');
2+
var CONST = require('../const');
3+
var FileTypesManager = require('../FileTypesManager');
4+
var JSONFile = require('./JSONFile.js');
5+
6+
// Phaser.Loader.FileTypes.AudioSprite
7+
8+
FileTypesManager.register('audioSprite', function (key, urls, json, config, audioXhrSettings, jsonXhrSettings)
9+
{
10+
var audioFile = AudioFile.create(this, key, urls, config, audioXhrSettings);
11+
12+
if (audioFile)
13+
{
14+
var jsonFile;
15+
16+
if (typeof json === 'string')
17+
{
18+
jsonFile = new JSONFile(key, json, this.path, jsonXhrSettings);
19+
20+
this.addFile(jsonFile);
21+
}
22+
else
23+
{
24+
jsonFile = {
25+
type: 'json',
26+
key: key,
27+
data: json,
28+
state: CONST.FILE_WAITING_LINKFILE
29+
};
30+
}
31+
32+
// Link them together
33+
audioFile.linkFile = jsonFile;
34+
jsonFile.linkFile = audioFile;
35+
36+
// Set the type
37+
audioFile.linkType = 'audioSprite';
38+
jsonFile.linkType = 'audioSprite';
39+
40+
this.addFile(audioFile);
41+
}
42+
43+
return this;
44+
});

0 commit comments

Comments
 (0)