我正在尝试使某些按钮无法与声音一起使用。对于我正在使用的页面,我将其设置如下:
// javascript file for gun tutorial//
window.onload=function()
{
var extraAmmo = 210;
var maxAmmo = 30;
var currentAmmo = maxAmmo;
var extraAmmoHud = document.getElementById("extra-ammo");
var currentAmmoHud = document.getElementById("current-ammo");
var shootButton = document.getElementById("shoot-button");
var unloadButton = document.getElementById("unload-button");
var reloadButton = document.getElementById("reload-button");
refreshScreen();
shootButton.onclick=function()
{
if(currentAmmo > 0)
{
currentAmmo--;
refreshScreen();
}
}
unloadButton.onclick=function()
{
if (currentAmmo > 0)
{
unloadTimer = setTimeout(unloadButton.onclick, 150)
currentAmmo--;
refreshScreen();
}
else unloadTimer = null;
}
reloadButton.onclick=function()
{
var difference = getDifference();
if(extraAmmo >= difference)
{
currentAmmo += difference;
extraAmmo -= difference;
}
else
{
currentAmmo += extraAmmo;
extraAmmo -= extraAmmo;
}
refreshScreen();
function getDifference()
{
return maxAmmo -currentAmmo;
}
}
function refreshScreen()
{
extraAmmoHud.innerHTML="Extra Ammo: " + extraAmmo;
currentAmmoHud.innerHTML="Current Ammo: " + currentAmmo;
}
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Gun Tutorial</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<script src="js/gun.js"></script>
<audio src="sounds/GunShot.mp3"></audio>
<audio src="sounds/GunShotFullAuto.mp3"></audio>
<audio src="sounds/GunCockingFast.wav"></audio>
</head>
<body>
<div id="wrapper">
<header id="header">
<h1>Welcome to the Gun Show</h1>
</header>
<div id="content"></div>
<form>
<div id="boxobuttons">
<input type="button" value="Shoot" id="shoot-button" />
<input type="button" value="Unload" id="unload-button" />
<input type="button" value="Reload" id="reload-button" />
</div>
<div id="ammo-count">
<p id="current-ammo"></p>
<p id="extra-ammo"></p>
</div>
</form>
<footer>
</footer>
</div>
</body>
</html>
我正在尝试查看是否需要将声音用作一个变量,然后可以将每个单独的变量插入不同的变量(shootButton,unloadButton和reloadButton)中,还是我必须做一些完全独立的事情?
您为什么不只在javascript中加载声音?
例如: var gunshot = new Audio('GunShot.mp3');
然后,如果您有声音,可以将它们放在代码之间,如下所示:
shootButton.onclick=function()
{
if(currentAmmo > 0)
{
currentAmmo--;
gunshot.play();
refreshScreen();
}
}
我希望我以此回答了你的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句