Jump to content

Playing the same sound marker twice at the same time?


CtlAltDel
 Share

Recommended Posts

I am trying to play gunshot sounds from the player and surrounding enemies. I have several gunshot sounds in an audiosprite. Using the addMarker function I create all the different markers. 

 

I now need to play the same marker multiple times at the same time. For instance if someone fires a gun and you're firing at the same time I need the same sound twice. It seems the sound api does not support tthis. It either restarts the sound or does not play an extra sound.

 

Any solution other than creating an object pool of sound objects and using those?

Link to comment
Share on other sites

  • 1 month later...

This might be of use to someone. This is my current solution using the new AudioSprite that is in the dev branch to play multiple of the same sounds.

function MultiSound(game, key, maxSounds) {    this.game = game; // phaser game object    this.maxSounds = maxSounds || 4; // same sounds max at one time    this.key = key; // key of the audiosprite     this.sounds = [];    var self = this;    var i = 0,        n = this.maxSounds;    while( i < n ) {        var sound = this.game.add.audioSprite(this.key);        this.sounds.push(sound);        i++;    }}MultiSound.prototype.play = function(key, volume) {    if (typeof volume === 'undefined') { volume = 1; }    var i = 0,        n = this.sounds.length;    var started = false;    while( i < n ) {        var markerSound = this.sounds[i].get(key);        if (!markerSound.isPlaying) {            markerSound.play(key, null, volume);            started = true;            break;        }        i++;    }    if (!started) { // restart the first available        this.sounds[0].play(key, volume);    }};
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...