Skip to content

allow users to use their own sound class#119

Open
aaron13100 wants to merge 1 commit intocrashmax-dev:masterfrom
aaron13100:patch-3
Open

allow users to use their own sound class#119
aaron13100 wants to merge 1 commit intocrashmax-dev:masterfrom
aaron13100:patch-3

Conversation

@aaron13100
Copy link
Copy Markdown

Checklist

This is untested so feel free to reject it. This allows users to override the Sound class, as in the following example.

  class CustomFireworksSound extends Sound {
    play() {
      if (this.isEnabled) {
        var soundFiles = this.options.sound.files;
        var randomIndex = Math.floor(Math.random() * soundFiles.length);
        var gainDifference = Math.abs(this.options.sound.volume.min - this.options.sound.volume.max);

        var gain = Math.random() * gainDifference;
        var fileToPlay = soundFiles[randomIndex];
        var audioElement = document.createElement('audio');
        audioElement.src = fileToPlay;
        audioElement.setAttribute('data-gain', gain);
        audioElement.setAttribute('data-allow-simultaneous', 'true');
        playSoundWithEventTracking(audioElement);
        console.log("custom play function");
      }
    }
  }
  fireworksConfig.soundClass = new CustomFireworksSound(fireworksConfig);

or if we've wrapped all the Fireworks stuff like this..

function fireworksModule() {
   // ... existing code ...
   return {
     Fireworks: Fireworks,
     Sound: Sound
   }
}

Then we can use our custom class like this...

  const fireworksModuleInstance = fireworksModule();
  const FireworksSoundClass = fireworksModuleInstance.Sound;
  class CustomFireworksSound extends FireworksSoundClass {
    play() {
      // ...etc ...
    }
  }
  fireworksConfig.soundClass = new CustomFireworksSound(fireworksConfig);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant