我在Javascript中制作一个简单的游戏,当一个物体与墙碰撞时,它会发出"砰"的声音.声音的响度取决于物体的速度(更高的速度=>更响的声音).
播放功能:
playSound = function(id, vol) //ID of the sound in the sounds array, volume/loudness of the sound
{
if (vol) //sometimes, I just want to play the sound without worrying about volume
sounds[id].volume = vol;
else
sounds[id].volume = 1;
sounds[id].play();
}
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它:
playSound(2, Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV); //self.TV stands for terminal velocity. This calculates the actual speed using the basic Pythagora's theorem and then divides it by self.TV, which results in a number from 0 to self.TV. 2 is the …Run Code Online (Sandbox Code Playgroud)