我正在为一个JS班写作业,老师希望我们在开火时有一个武器声音,但当弹药耗尽时不要发出声音。我有声音效果工作时,枪开火,但它继续发出声音时,点击0弹药。
我尝试做一个其他{}函数,但这打破了我的浏览器中的“弹药显示”,而且声音将继续播放。
HTML:
<input type="button" value="Heal" class="shoot" onclick="shoot();">
<audio id="heal" src="sound/heal.mp3">JS:显示弹药从最多6枪开始,其中6枪是后备的,每次发射时都会倒计时。
function shoot() {
if (currentAmmo > 0) {
currentAmmo--;
}
var shoot = document.getElementById("shoot");
shoot.play();
updatescreen();
function updatescreen() {
document.getElementById("total-ammo").innerHTML = "Bullets in Gun:</br>" + totalAmmo;
document.getElementById("current-ammo").innerHTML = "Reload Ammo:</br>" + currentAmmo;
}发布于 2019-04-30 02:56:43
将play调用放在if语句中,因此只有在语句为真时才会播放:
function shoot() {
if (currentAmmo > 0) {
currentAmmo--;
document.getElementById("shoot").play();
}
}https://stackoverflow.com/questions/55913341
复制相似问题