Classes
Sound
Games don't necessarily always need audio; there are many types of simple board and card games, for example, which have no audio at all. On mobile devices it's actually preferable to keep audio to a minimum in order to reduce load times, not to mention the fact that audio support still isn't exactly standardized on all devices. Further still, many users prefer to mute game audio in favor of the their own.
That said, JackyJS leverages the HowlerJS library to bring you cross platform support for your most basic audio needs. You can play multiple sound effects, loop background music, increase/decrease volume, mute/unmute, and save user settings to localStorage. Just remember to keep it simple. ;-)
JackyJS audio needs to be preloaded and assets initialized prior to use. The following example shows you how:
GAME.initPreload(function(){
GAME.Preloader.sounds.push('jackyjs/assets/sounds/gling.wav');
GAME.Preloader.sounds.push('jackyjs/assets/sounds/notify.wav');
GAME.Preloader.sounds.push('jackyjs/assets/sounds/music.mp3');
});
GAME.initAssets(function(){
GAME.Sound.asset(['gling.wav'], {name:'gling'});
GAME.Sound.asset(['notify.wav'], {name:'notify'});
GAME.Sound.asset(['music.mp3','music.ogg'], {
name: 'music',
type: 'music',
loop: true
});
});
GAME.initRooms(function(){
var Level1 = GAME.Room.create({name:'Level1});
Level1.onCreate(function(obj){
...
});
Level1.onStart(function(obj){
GAME.Sound.play('music');
});
});
Properties (static)
Name | Type | Description |
---|---|---|
volumeSound | Number | the global volume of sound assets. **read-only** (default: 50) |
volumeMusic | Number | the global volume of music assets. **read-only** (default: 50) |
mutedSound | Boolean | the global muting of sound assets. **read-only** (default: FALSE) |
mutedMusic | Boolean | the global muting of music assets. **read-only** (default: FALSE) |
Methods (static)
Name | Parameter | Type | Description |
---|---|---|---|
asset | initializes a resource asset to be used as a reference in the creation of entities. You can add fallback audio files to cover cases where a browser does not support a particular audio format. To do this, simply append to the src array different audio files. According to the HowlerJS documentation, the webm format is a very compatible format to use. | ||
* src | Array | source filename of sound resource to initialize. Path not required. (default path: jackyjs/assets/sounds) | |
* options.name | String | name of asset. | |
options.type | String | type of asset; 'sound' or 'music'. (default: 'sound') | |
options.loop | Boolean | if set to TRUE, will loop forever. (default: FALSE) | |
options.pausable | Boolean | if set to TRUE, sound can be paused. (default: FALSE) | |
play | assetName | String | plays the sound referenced by assetName. |
stop | assetName | String | stops the sound file referenced by assetName, and resets its playhead to beginning. |
pause | assetName | String | pauses the sound file referenced by assetName. |
pauseToggle | * assetName | String | toggles pause/play state referenced by assetName. |
override | Boolean | if set to TRUE, toggles pause/play state even if NOT pausable. | |
mute | * assetName | String | mutes/unmutes the sound file referenced by assetName. |
muted | Boolean | TRUE is muted; FALSE is unmuted | |
muteToggle | type | String | the audio type to toggle mute/unmute. Options: 'sound', 'music', 'all'. (default: 'all') |
stopAll | stops ALL sound files, and resets their playhead to beginning. | ||
pauseAll | pauses ALL sound files. | ||
muteAll | muted | Boolean | TRUE is muted; FALSE is unmuted. |
type | String | the audio type to mute/unmute. Options: 'sound', 'music', 'all'. (default: 'all') | |
muteToggleAll | toggles mute/unmute of ALL audio files. | ||
volumeUp | * type | String | increases volume of specific audio type by 1 (max = 100). Options: 'sound', 'music'. (default: 'sound') |
volumeDown | * type | String | decreases volume of specific audio type by 1 (min = 1). Options: 'sound', 'music'. (default: 'sound') |
volumeUpAll | increases volume of ALL audio types. | ||
volumeDownAll | decreases volume of ALL audio types. |