我试图弄清楚如何通过替换背景音频来添加开始按钮来玩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/