javascript:我的声音只播放一次?

时间:2013-10-26 23:35:36

标签: javascript html css audio

我为我的游戏Blitz做了一项技巧。它的功能是你在6秒内击中你的对手3次。

我将声音文件添加到技能中,这样您就可以听到有关您所做的事情的听力线索,因此您不必阅读文本日志。我遇到的问题是当我击中对手2次或更多时,声音文件只播放一次,而不是两次。

例如:这有效 - 击中,失败,击中。 例如:这不起作用 - Hit,Hit(没有声音),Miss(声音)

为什么跳过或不玩?

// BLITZ SKILL 

document.getElementById("blitz").addEventListener('click', function() {
    atbReset();
    DB();
    RET();
    window.setTimeout(function() { blitzskill() }, 1000);
    window.setTimeout(function() { blitzskill() }, 2000);
    window.setTimeout(function() { blitzskill() }, 3000);
});

function blitzskill(){
    var criticalRoll = Math.floor(Math.random() * 100 + 1);
    var precisionRoll = Math.floor(Math.random() * cs.precision + 1);
    var npcParryRoll = Math.floor(Math.random() * ds.parry + 1);
    var damage = Math.floor(Math.random() * cs.strength * 1);

        if (character.energy <= 4) {
            addMessage("Not enough energy!")    
            return;
            }
        if (precisionRoll < npcParryRoll) {
            addMessage("The Dragon evaded your attack!");
            character.energy -= 5;
            miss.play(); // PLAY MISS SOUND
            }
        else if (damage - ds.armor <= 0) {
            character.energy -= 5;
            addMessage("Your opponents armor withstood your attack!");
            armor.play(); // PLAY MISS/ARMOR SOUND
            }   
        else if (cs.critical >= criticalRoll) {
            damage *= 2;
            damage -= ds.armor;
            dragon.hp -= damage;
            character.energy -= 5;
            document.getElementById("npchp").innerHTML = dragon.hp;
            addMessage("Critical Strike! Dragon suffers " + damage + " hp!")
            swoosh.play(); // PLAY HIT SOUND
            }   
        else {  
            dragon.hp -= damage;
            damage -= ds.armor;
            document.getElementById("npchp").innerHTML = dragon.hp;
            addMessage("You hit the dragon for " + damage + " hp!");
            character.energy -= 5;
            swoosh.play(); // PLAY HIT SOUND
            }
            document.getElementById("energy").innerHTML = character.energy;
};

1 个答案:

答案 0 :(得分:1)

试试这个:

swoosh.pause();// ensure sound is not already playing 
swoosh.currentTime = 0; //reset time
swoosh.play(); // PLAY HIT SOUND

我正在做的就是添加逻辑以确保在声音开始时调用播放