HTML5 multiplayer game

I am programming a Javascript game and I want to use one sound several times. I can do this with loading one sound more time into the array. But I want to load one sound once and “copy” it into an array, so I can play it several times. This is the method I have now:

this.list = [
        "LaserShot", //http://www.freesound.org/samplesViewSingle.php?id=39459
        "Ding", //http://www.freesound.org/samplesViewSingle.php?id=5212
        "Rocket" //http://www.freesound.org/samplesViewSingle.php?id=47252 -> http://creativecommons.org/licenses/sampling+/1.0/
    ];

...

for (i in this.list) {
        this.sounds[this.list[i]] = new Array();

        for (var j = 0; j < this.channels; j++) {
            this.sounds[this.list[i]][j] = new Audio("./sounds/"+ this.list[i] + type);
        }
    }

I just want to do this:

for (i in this.list) {
        this.sounds[this.list[i]] = new Array();

var tempAudio = new Audio("./sounds/"+ this.list[i] + type);

        for (var j = 0; j < this.channels; j++) {
            this.sounds[this.list[i]][j] = realCopyOfTempAudio;
        }
    }

Thank you very much.

+2
source share
1 answer

My experience:

it’s better to create more audio html tags with the same source. I am a fan of js, but this time it is better to have html audio tags in html form.

-, . , .

( EXE_JUST_ONE_TIME , ):

<audio controls id="LaserShot" >
 <source src="LaserShot.mp3" type="audio/mpeg">
 <source src="LaserShot.ogg" type="audio/ogg">
</audio>
 <audio controls id="LaserShot_CLONE" >
 <source src="LaserShot.mp3" type="audio/mpeg">
 <source src="LaserShot.ogg" type="audio/ogg">
</audio>

var EXE_JUST_ONE_TIME = false;

document.addEventListener("click", function (e) {

if (EXE_JUST_ONE_TIME == false){

  EXE_JUST_ONE_TIME = true;

  document.getElementById("LaserShot").play();
  document.getElementById("LaserShot").pause();

  document.getElementById("LaserShot_CLONE").play();
  document.getElementById("LaserShot_CLONE").pause();

  // Buffering in progress 
  // now you can play programmability from code
  // One click or touch can prepare max 6 audios 

}

}

( ), :

var play_shoot = function(){

 if (document.getElementById('LaserShot').duration > 0 &&
   !document.getElementById('LaserShot').paused) {

  document.getElementById('LaserShot_CLONE').play();

 } else {

  document.getElementById('LaserShot').play();

 }

}
0

Source: https://habr.com/ru/post/1679479/


All Articles