javascript - 如何为该功能添加开始按钮?

标签 javascript jquery button audio

我试图弄清楚如何通过替换背景音频来添加开始按钮来玩JavaScript游戏。我碰到过这款游戏,它的工作方式是,一旦您加载页面,音乐就会在后台播放并且游戏已经开始。当我删除音频链接时,游戏在玩家3个生命结束后暂停,如果我保留音频,那么当3个生命结束时,您可以看到您的积分和弹出消息,请有人帮我理解

这是游戏的链接,因此您可以看一下代码,了解我想说的:https://jsfiddle.net/74nbrdak/embedded/result/

<div>
<canvas id="canvas" width="1000" height="500"></canvas>
</div>

<audio id="background-music" preload="auto" autoplay loop>
<source 
src="https://dl.dropbox.com/s/5r3iu7kjsl0mx81/Wildfire%20Cut%20Loopable.wav"       type="audio/wav">


 function ShowGamesFinished() {
var message = gamesfinished[Math.floor(Math.random() *     gamesfinished.length)];

 document.getElementById("background-music").pause();

最佳答案

When I removed the audio link, the game paused after the players 3 lives are up, if I leave the audio in, then when the 3 lives are up you can see your points and a pop up message, can someone please help me understand this



因此,在第二种情况下,当音频元素在页面上时,游戏将按照创建者的预期运行。
在第一种情况下,当音频元素不在页面上时,游戏运行良好,直到调用了处理游戏的功能。导致该函数出现问题的是此行document.getElementById("background-music").pause();。由于音频元素不存在,因此会引发错误,并且不会绘制屏幕游戏。希望这会有所帮助

关于javascript - 如何为该功能添加开始按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47816896/

相关文章:

javascript - 下拉让元素移动

javascript - 更改相对于旧行高值的行高属性

javascript - FullCalendar "cannot read property ' getDay' 未定义”

javascript - Angular : Conditionally changing FormControl

javascript - jquery默认禁用复选框

java - 当我使用这个线程代码时,我的应用程序崩溃了

javascript - 当鼠标恰好位于范围 slider 上时滚动会更改值

javascript - 即使其他 JS 工作正常,Twitter 的 Bootstrap Popover : Won't work,

javascript - 如何使用按钮激活 For 循环以显示每次按钮单击的每个单独结果?

python - 多个按钮 Raspberry Pi