私は音をしばしば演奏するゲームを作っています。私はそれが演奏されている間、サウンドが再び演奏されないことに気づいた。たとえば、プレイヤーが壁に衝突した場合、「太鼓」の音が鳴ります。しかし、プレイヤーが1つの壁に衝突してすぐに別の壁に衝突すると、ただ1つの「強打」音が鳴り、最初の音が終わらないために起こると私は信じています。本当?これを避けるにはどうすればいいですか?解決しようHTML5では、同じ音を複数回同時に再生できますか?
...私はいつもその音の別のコピーを演奏、音3回をプリロードと思ったが、これはむしろ愚かなようだ:
あなたがプリロードする必要があります...私は正しかったが判明します複数のバージョンのサウンドを再生し、それらを循環再生します。
CODE:
var ns = 3; //The number of sounds to preload. This depends on how often the sounds need to be played, but if too big it will probably cause lond loading times.
var sounds = []; //This will be a matrix of all the sounds
for (i = 0; i < ns; i ++) //We need to have ns different copies of each sound, hence:
sounds.push([]);
for (i = 0; i < soundSources.length; i ++)
for (j = 0; j < ns; j ++)
sounds[j].push(new Audio(sources[i])); //Assuming that you hold your sound sauces in a "sources" array, for example ["bla.wav", "smile.dog" "scream.wav"]
var playing = []; //This will be our play index, so we know which version has been played the last.
for (i = 0; i < soundSources.length; i ++)
playing[i] = 0;
playSound = function(id, vol) //id in the sounds[i] array., vol is a real number in the [0, 1] interval
{
if (vol <= 1 && vol >= 0)
sounds[playing[id]][id].volume = vol;
else
sounds[playing[id]][id].volume = 1;
sounds[playing[id]][id].play();
++ playing[id]; //Each time a sound is played, increment this so the next time that sound needs to be played, we play a different version of it,
if (playing[id] >= ns)
playing[id] = 0;
}
あなたはオーディオを再生するために何を使用していますか?私はそれが '
'sound = new Audio(" souce.wav "); sound.play(); ' – corazza