我主要使用jquery制作了一个onclick RPG。随机选择被敌人机器人攻击的英雄,我遇到了一个重大问题

时间:2016-06-03 14:48:39

标签: javascript jquery function conditional

这是我的第一个堆栈溢出问题,但我会尝试尽可能简洁。我以旧学校RPG的风格制作了一款游戏。你有三个英雄可供选择,基于奇迹宇宙,你选择三个敌人中的一个来攻击。在您发起攻击后,敌方会发起随机攻击。我的主要问题是,如果其中一个英雄角色死亡并从游戏中移除,敌人仍有可能随机攻击所述角色。以下是该游戏的一些来源:http://marvel-jquery-rpg.herokuapp.com/或我的github:https://github.com/jdestiny92/jquery_rpg或者这里是主要的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();
        };
    });

};

};

这是我尝试修复敌人的攻击选择,但是一旦我将其插入到上一个函数中它就无效了:

if(capHeatlh<0 && ironmanHeatlh<0){
    heroes = ['deadpool', 'deadpool', 'deadpool'];
};

if(capHeatlh<0 && deadpoolHeatlh<0){
    heroes = ['ironman', 'ironman', 'ironman'];
};

if(deadpoolHeatlh<0 && ironmanHeatlh<0){
    heroes = ['cap', 'cap', 'cap'];
};

if(capHeatlh<0){
    var option1 = ['ironman', 'ironman', 'deadpool'];
    var option2 = ['deadpool', 'ironman', 'deadpool'];

    var coinflip1 = Math.floor(Math.random()*2);

    if(coinflip1==0){
        heroes = option1;
    }
    else{
        heroes = option2;
    };
};

if(ironmanHeatlh<0){
    var option1 = ['cap', 'cap', 'deadpool'];
    var option2 = ['cap', 'deadpool', 'deadpool'];

    var coinflip2 = Math.floor(Math.random()*2);

    if(coinflip2==0){
        heroes = option1;
    }
    else{
        heroes = option2;
    };
};

if(deadpoolHeatlh<0){
    var option1 = ['cap', 'ironman', 'cap'];
    var option2 = ['cap', 'ironman', 'ironman'];

    var coinflip3 = Math.floor(Math.random()*2);

    if(coinflip3==0){
        heroes = option1;
    }
    else{
        heroes = option2;
    };
};

非常感谢任何和所有帮助/反馈!谢谢!

1 个答案:

答案 0 :(得分:1)

您可以将英雄阵列的assignemnt移出该功能的范围。然后由你的英雄阵列中的任何人控制你的选择:

heroes = ["cap", "ironman", "deadpool"]; //move this outside of the function scope, then

你选择随机索引应该由英雄数组的长度决定:

您可以使用.length:

,而不是在那里硬编码3

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

当英雄死亡时,你可以使用.splice(index,1)将他从数组中删除;

所以heroes.splice(0,1)将删除&#34; cap&#34;,heroes.splice(1,1)将删除&#34; ironman&#34;,heroes.splice(2,1)将删除&#34; deadpool&#34;

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