Skip to content

Instantly share code, notes, and snippets.

@bartread
Created February 11, 2017 15:13
Show Gist options
  • Save bartread/964c015009b2ec46b6254e55f0d90840 to your computer and use it in GitHub Desktop.
Save bartread/964c015009b2ec46b6254e55f0d90840 to your computer and use it in GitHub Desktop.
Playing a sound effect in arcade.ly games
function _play(effect) {
if (!areSfxEnabledFlag || !effect) {
return;
}
var buffer = effect.buffer;
if (!buffer) {
return;
}
var source = context.createBufferSource();
source.buffer = buffer;
effect.mediaSource = source;
source.connect(effect.gain);
if (!source.start) {
source.start = source.noteOn;
}
source.start(0);
}
/*
This code is licensed under the terms of the MIT License as described
in LICENSE.txt.
Copyright (c) 2015 - 2017 Bart Read, arcade.ly (https://arcade.ly),
and bartread.com Ltd (http://www.bartread.com/)
The soundConfiguration service is a per-game service, implemented by
games at https://arcade.ly, that provides configuration for sound
effects and music to the sfx service, thus allowing them to be played.
*/
function _playByName(name) {
_play(service[name]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment