関数を再利用できます:
// declare your sound dictionary
var sounds = {
'laser': new buzz.sound( "laser-01", { formats: [ "ogg", "mp3", "acc" ]}),
'alien-noise': new buzz.sound( "alien-noise-01", {formats: [ "ogg", "mp3", "acc" ]})
};
// this is the helper function
var playSoundFn = function() {
this.play().fadeIn().loop();
};
// assign the helper function to all your sounds
for (var i=0, len=sounds.length; i<len; i++){
sounds[i].playSound = playSoundFn;
}
// then play your sounds from any of them in your dictionary :
sounds['laser'].playSound();
sounds['alien-noise'].playSound();
**編集** ( TheSmoseに感謝)
sounds
配列内の各項目がbuzz.sound.prototype
プロトタイプで作成されている場合は、カスタム関数を単純に追加して使用できます。
// this is the helper function
buzz.sound.prototype.playSound = function() {
this.play().fadeIn().loop();
};
// declare your sound dictionary
var sounds = {
'laser': new buzz.sound("laser-01", { formats: ["ogg", "mp3", "acc"]}),
'alien-noise': new buzz.sound("alien-noise-01", {formats: ["ogg", "mp3", "acc"]})
};
// then play your sounds from any of them in your dictionary :
sounds['laser'].playSound();
sounds['alien-noise'].playSound();