1

我已经成功地为我的点击计数器图像添加了 onclick 音频效果,但我想知道,有没有办法随机选择声音,而不是一个声音?当我单击我的图像时,会出现单击图像的次数,并播放声音,但我希望它在每次单击时播放不同的(随机)音频文件(来自我的选择)

这些是到目前为止我的按钮的脚本

Javascript

function clickCounter(){
     var audio = document.getElementById("audio");
     audio.play();
     if(typeof(Storage)!=="undefined"){
         if (localStorage.clickcount)
         {
             localStorage.clickcount=Number(localStorage.clickcount)+1;
         }
         else
         {
             localStorage.clickcount=1;
         }
         document.getElementById("result").innerHTML="<center><b>You have clicked the poop
         </b>"+ localStorage.clickcount + "<b> times!</b></center>";
     }
     else{
         document.getElementById("result").innerHTML="I'm sorry to inform you that your browser
         does not support this web storage... I guess you could say that your browser is...
         shit! awwww yeaahh!";
     }
     cursor: pointer;
}

HTML

<center>
     <font face="chiller" color="#603913" "font size="300" <align="center"><b>Click the
     poop!</b>
</center>
<center>
    <p><picture onclick="clickCounter()"><picture
     onmouseover="document.getElementById('touch').play()"><img src="poop.png"></button></p>
</center>
<audio id="audio" src="fart-01.wav"></audio>
<audio id="touch" src="sticky goo.wav"></audio>
<div id="result"></div>

我将如何编写这个脚本?我知道我必须将它与我当前的 onclick 函数交织在一起,而且我必须制作一个表格,可能带有 tr 或 td 标签。有人可以帮我吗?我不能让它工作。

提前感谢大家!

4

1 回答 1

1

您可以为此使用一个开关盒。

您生成一个随机数,例如

var number = Math.floor((Math.random() * 3) + 1);

然后您可以使用开关盒从您使用随机数生成的 N 种可能性中选择一种随机声音。

喜欢

函数 setSound() {

    switch (number) {
    case 1:
        setSound1;
        return sound;
        break
    case 2:
         setSound2;
        return sound;
        break
    case 3:
         setSound3;
        return sound;
        break
    }
}

然后将返回值添加到您要播放声音的代码中。

在这种情况下,您需要返回您从 swtich 案例中随机选择的声音文件的名称。

于 2014-10-02T09:01:32.037 回答