Javascript 我主要使用jquery制作了一个onclick rpg。I';我有一个主要的问题,随机选择一个英雄被敌人的机器人攻击

Javascript 我主要使用jquery制作了一个onclick rpg。I';我有一个主要的问题,随机选择一个英雄被敌人的机器人攻击,javascript,jquery,function,conditional,Javascript,Jquery,Function,Conditional,这是我的第一个堆栈溢出问题,不过我会尽量简洁。我做了一个老式rpg游戏。你有三个英雄可供选择,基于惊奇漫画宇宙,你可以从三个敌人中选择一个进行攻击。发起攻击后,敌方将发起随机攻击。我的主要问题是,如果其中一个英雄角色死亡并从游戏中移除,敌人仍然有可能随机攻击该角色。这里有一些关于上述游戏:或我的github:的来源,或者这里有一个主要的js函数: // Boss Attack Sequence function bossAttackSequence(){ heroes = ["cap"

这是我的第一个堆栈溢出问题,不过我会尽量简洁。我做了一个老式rpg游戏。你有三个英雄可供选择,基于惊奇漫画宇宙,你可以从三个敌人中选择一个进行攻击。发起攻击后,敌方将发起随机攻击。我的主要问题是,如果其中一个英雄角色死亡并从游戏中移除,敌人仍然有可能随机攻击该角色。这里有一些关于上述游戏:或我的github:的来源,或者这里有一个主要的js函数:

    // Boss Attack Sequence
function bossAttackSequence(){

heroes = ["cap", "ironman", "deadpool"];

choice = heroes[Math.floor(Math.random()*3)];

ultronAttack = Math.floor(Math.random()*(300-200+1)+200);

ultronCrit = Math.floor(Math.random()*100);

if(ultronCrit <= 15){
        ultronAttack = Math.floor(ultronAttack + .25*ultronAttack);
    };

if(choice=="cap"){
    $('#attackMessage').show(1000, function(){
    document.getElementById('ultronAttack').play();
    if(ultronCrit <= 15){
        document.getElementById('ultronAttack').pause();
        document.getElementById('ultronCrit').play();
        $('#attackMessage').html('Ultron did a critical hit on Captain America for ' + ultronAttack + ' damage');
    }
    else{
        $('#attackMessage').html('Ultron attacked Captain America for ' + ultronAttack + ' damage');
    };
    reset();
    capHealth = capHealth - ultronAttack;
    $('#capbox').html('Health: ' + capHealth);
    if(capHealth < 0){
        $('#cap').remove();
        $('#capbox').remove();
        $('#capHealth').remove();
        new Audio('letdown.mp3').play();
    };
    if(window.capHealth <=0 && window.ironmanHealth <=0 && window.deadpoolHealth <=0){location.replace('loss.html');};
    });

};

if(choice=="ironman"){
    $('#attackMessage').show(1000, function(){
    document.getElementById('ultronAttack').play();
    if(ultronCrit <= 15){
        document.getElementById('ultronAttack').pause();
        document.getElementById('ultronCrit').play();
        $('#attackMessage').html('Ultron did a critical hit on Ironman for ' + ultronAttack + ' damage');
    }
    else{
        $('#attackMessage').html('Ultron attacked Ironman for ' + ultronAttack + ' damage');
    };
    reset();
    ironmanHealth = ironmanHealth - ultronAttack;
    $('#ironmanbox').html('Health: ' + ironmanHealth);
    if(ironmanHealth < 0){
        $('#ironman').remove();
        $('#ironmanbox').remove();
        $('#ironmanHealth').remove();
        new Audio('impossible2.mp3').play();
    };

});

};

if(choice=="deadpool"){
    $('#attackMessage').show(1000, function(){
    document.getElementById('ultronAttack').play();
    if(ultronCrit <= 15){
        document.getElementById('ultronAttack').pause();
        document.getElementById('ultronCrit').play();
        $('#attackMessage').html('Ultron did a critical hit on Deadpool for ' + ultronAttack + ' damage');
    }
    else{
        $('#attackMessage').html('Ultron attacked Deadpool for ' + ultronAttack + ' damage');
    };
    reset();
    deadpoolHealth = deadpoolHealth - ultronAttack;
    $('#deadpoolbox').html('Health: ' + deadpoolHealth);
    if(deadpoolHealth < 0){
        $('#deadpool').remove();
        $('#deadpoolbox').remove();
        $('#deadpoolHealth').remove();
        new Audio('wrongButton.mp3').play();
        };
    });

};

};
//Boss攻击序列
函数bossAttackSequence(){
英雄=[“帽子”、“铁人”、“死亡池”];
选择=英雄[Math.floor(Math.random()*3)];
ultronAttack=Math.floor(Math.random()*(300-200+1)+200);
ultronCrit=Math.floor(Math.random()*100);

如果(ultronCrit您可以将英雄阵列分配移出函数范围。然后由英雄阵列中剩余的任何人控制您的选择:

heroes = ["cap", "ironman", "deadpool"]; //move this outside of the function scope, then
您选择的随机索引应由英雄数组的长度决定:

heroes = ["cap", "ironman", "deadpool"]; //move this outside of the function scope, then
您可以使用。长度:

choice=heroes[Math.floor(Math.random()*heroes.length)];

当一个英雄死亡时,你可以使用.splice(索引1)将他从阵列中删除

所以英雄。拼接(0,1)将移除“帽子”,英雄。拼接(1,1)将移除“铁人”,英雄。拼接(2,1)将移除“死亡池”

这样,下次运行bossSequence时,您的英雄阵列可能只剩下2个英雄或1个英雄,但您的随机选择仍然只剩下一个活着的角色