Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 功能赢得';不要停止跑步_Javascript_Jquery_Function - Fatal编程技术网

Javascript 功能赢得';不要停止跑步

Javascript 功能赢得';不要停止跑步,javascript,jquery,function,Javascript,Jquery,Function,我的代码似乎有一个非常严重的问题。我第一次调用battle函数时,它工作正常,但第二次调用时,它只使用第一次调用时传递的参数。我认为从第一次调用开始,该函数仍在运行。以下是我的代码,我只尝试将关键部分放入其中: var player = { // Name // Position totalHealth: 20, // total Health currentHealth : 20, // current Health Level: 1, // Level

我的代码似乎有一个非常严重的问题。我第一次调用battle函数时,它工作正常,但第二次调用时,它只使用第一次调用时传递的参数。我认为从第一次调用开始,该函数仍在运行。以下是我的代码,我只尝试将关键部分放入其中:

var player = {
    // Name
    // Position
    totalHealth: 20, // total Health
    currentHealth : 20, // current Health
    Level: 1, // Level
    // Defense
    // Attack
    // Luck
    Win: false,
    EXP: 0,
    EXPNeededCalculation: function (Level) {
        return (Level * 100) - player.EXP;
    },
    EXPGainedCalculation: function (opponentLevel, playerCurrentHealth, playerTotalHealth) {
        return player.EXP += Math.ceil(opponentLevel * 10 - ((playerTotalHealth - playerCurrentHealth) / 2));
    },
    // Items are in an object {Description:  powers, used : false, use() : yadayadayada}
    DefenseDoubled: false,
    attack: function (Attack, Luck, opponentDefense) {
        var possibleAttacks = [];
        var possibleDefense = [999]; // arbitrary number to fill one space
        for (var i = 0 ; i < Luck ; i++) {
            possibleAttacks.push(Math.ceil(Math.random() * Attack));
        };
        possibleAttacks.sort(); 
        var a = Math.ceil(Math.random() * 10);
        if (a > 7) {
            for (var x = 0 ; x < opponentDefense ; x++) {
                possibleDefense.push(Math.floor(Math.random() * opponentDefense) - 1);
            };
        }
        possibleDefense.sort(); 
        if (possibleDefense[0] <= 0) {
            var b = 0;
        } else {
            var b = 998; // arbitrary number
        }

        if (b === 0) {
            player["attackDamage"] = 0;
        } else {
            player["attackDamage"] = possibleAttacks[possibleAttacks.length - 1];
        }


    },
    defend: function() {
        player.Defense *= 2;
        player.DefenseDoubled = true;
    },
    undefend: function() {
        player.Defense /= 2;
        player.DefenseDoubled = false;
    }
};

function opponent(Level, Type, Name) {
    this.Name = Name;
    this.Level = Level;
    this.Type = Type;
    this.Health = calcHealth(Level);
    this.currentHealth = calcHealth(Level);
    this.Defense = calcStats(Level, Type);
    this.Attack = calcStats(Level, Type);
    this.Luck = calcStats(Level, Type);
    this.attackDamage =  0;
    this.DefenseDoubled = false;
    this.Poisoned = false;
    this.Burned = false;
    this.Frozen = false;

    function calcHealth(Level) {
        this.Health = Level * 5;
        this.currentHealth = this.Health;
        return this.Health; return this.currentHealth;
    };

    function calcStats(Level, Type) {
        this.Defense = Level * 2;
        this.Attack = Math.ceil(Level * 1.5);
        this.Luck = Math.ceil(Level * 1.25);
        return this.Defense ; return this.Attack ; return this.Luck; 
        if (Type === "snake" || Type === "Snake") {
            this.Luck += 1;
        }
    };
    this.attack = function(Attack, Luck, playerDefense) {
        var possibleAttacks = [];
        var possibleDefense = [999]; // arbitrary number to fill one space
        for (var i = 0 ; i < Luck ; i++) {
            possibleAttacks.push(Math.ceil(Math.random() * Attack));
        };
        possibleAttacks.sort(); 
        var a = Math.ceil(Math.random() * 10);
        if (a > 7) {
            for (var x = 0 ; x < playerDefense ; x++) {
                possibleDefense.push(Math.floor(Math.random() * playerDefense) - 1);
            };
        } 
        possibleDefense.sort(); 
        if (possibleDefense[0] <= 0) {
            var b = 0;
        } else {
            var b = 998; // arbitrary number
        }
        if (b === 0) {
            this.attackDamage = 0;
        } else {
            this.attackDamage = possibleAttacks[possibleAttacks.length - 1];
        }
    };

    this.defend = function() {
        this.Defense *= 2;
        this.DefenseDoubled = true;
    };
    this.undefend = function() {
        this.Defense /= 2;
        this.DefenseDoubled = false;
    }
}
$yesButton.click(function() {
yPressed = true;
if (value === 4 && yPressed === true) {
        addToValue(1);
        yPressed = false;
        $showBattleSummaryBox();
        $statsBox.append("<p class='center'>Health: " + player.totalHealth + "</p>");
        $battleSummaryBox.append("<p id='playersHealth' class='center'>Your Health: " + player.currentHealth + "/" + player.totalHealth + "</p>");
        $outputBox.append("<p>On the right is your Battle Summary Box. Your health has also shown up. If your health reaches zero, then you will faint, but if your enemy's health reaches zero, then you win.</p>");
        $outputBox.append("<p>Battles are turn-based. You will make an action and then your opponent will. During your turn, you can either attack, defend, or use an item.</p>");
        $outputBox.append("<p>I happen to have a level 1 python for you to practice with. </p>");
        scroll($outputBox);
        var e1 = new opponent(1, "snake", "Python"); 
        $battleSummaryBox.append("<p id='opponentsHealth' class='center'>Enemy's Health: " + e1.currentHealth + "/" + e1.Health + "</p>");
        $showBattleCommandBox();
        battle(e1);
    } 
});

function battle(opponent) {
    $battleOutputBox.append("<p>You make the first move!</p>");
    $("#opponentsHealth").text("Enemy's Health: " + opponent.currentHealth + "/" + opponent.Health);
    battleInProgress = true;
    $attackButton.click(function () {
        if (player.Win === false && battleInProgress === true) {
            player.attack(player.Attack, player.Luck, opponent.Defense);

            // additional Item effects
            if (player.Items.hasOwnProperty("Poison Arrows")) {
                var e = Math.ceil(Math.random() * 10); 
                if (e > 7) {
                    opponent.Poisoned = true;
                }
            } else if (player.Items.hasOwnProperty("Fire Blade")) {
                var e = Math.ceil(Math.random() * 10); 
                if (e > 7) {
                    opponent.Burned = true;
                    player.attackDamage += 3;
                }
            } else if (player.Items.hasOwnProperty("Blade of Luck")) {
                var e = Math.ceil(Math.random() * 10); 
                if (e > 7) {
                    player.attackDamage += 1;
                } 
            }

            opponent.currentHealth -= player.attackDamage;
            $("#opponentsHealth").text("Enemy's Health: " + opponent.currentHealth + "/" + opponent.Health);
            if (player.attackDamage === 0) {
                $battleOutputBox.append("<p>Your attack has been blocked</p>");
            } else {
                $battleOutputBox.append("<p>You inflicted " + player.attackDamage + " damage.</p>");
                scroll($battleOutputBox);
            }
            if (opponent.DefenseDoubled === true) {
                opponent.undefend();
            }
            if (opponent.Poisoned === true) {
                Poison();
            } else if (opponent.Burned === true) {
                Burn();
            } else if (opponent.Frozen === true) {
                Freeze();
            } 
            if (opponent.currentHealth <= 0) {
                player.Win = true;
                return postBattle(opponent);
            } else {opponentTurn();}
        }
    });

    $defendButton.click(function () {
        if (player.Win === false && battleInProgress === true) {
            player.defend();
            $battleOutputBox.append("<p>Your defense has been doubled for one turn.</p>");
            if (opponent.DefenseDoubled === true) {
                opponent.undefend();
            }
            if (opponent.Poisoned === true) {
                Poison();
            } else if (opponent.Burned === true) {
                Burn();
            } else if (opponent.Frozen === true) {
                Freeze();
            } 
            if (opponent.currentHealth <= 0) {
                player.Win = true;
                return postBattle(opponent);
            } else {opponentTurn();}
        }
    }); 

    $useItemButton.click(function () {
        if (player.Win === false && battleInProgress === true) {
            if ($useItemSelection.html().length > 15) {
                var itemBeingUsed = $("select[name='useItemSelection'] option:selected").text();
                if (itemBeingUsed === "Book of Spells" && player.Items.bookOfSpells.used === false) {
                    player.Items.bookOfSpells.use();
                    console.log(player.Items.bookOfSpells.effect);
                    if (player.Items.bookOfSpells.effect === "burn" || player.Items.bookOfSpells.effect === "poison") {
                        $battleOutputBox.append("<p>You have " + player.Items.bookOfSpells.effect + "ed " + opponent.Name + ".</p>");
                        if (player.Items.bookOfSpells.effect === "burn") {
                            opponent.Burned = true;
                        } else {
                            opponent.Poisoned = true;
                        }
                    } else {
                        $battleOutputBox.append("<p>You have froze " + opponent.Name + ".</p>");
                        opponent.Frozen = true;
                    }
                    player.Items.bookOfSpells.used = true;
                } else if (itemBeingUsed === "Shield Charm" && player.Items.shieldCharm.used === false) {
                    player.Items.shieldCharm.use();
                    $battleOutputBox.append("<p>You will block the next attack with your shield charm.</p>");
                    player.Items.shieldCharm.used = true;
                }
                if (opponent.Poisoned === true) {
                    Poison();
                } else if (opponent.Burned === true) {
                    Burn();
                } else if (opponent.Frozen === true) {
                    Freeze();
                } 
                if (opponent.currentHealth <= 0) {
                    player.Win = true;
                    return postBattle(opponent);
                } else {opponentTurn();}
            } else {
                var noUsableItemP = $("<p id='noUsableItemP'>You have no usable items. Select another command.</p>");
                $battleCommandPromptDiv.empty();
                $battleCommandPromptDiv.append(noUsableItemP);
            }
        }
    });

    function opponentTurn() {

        var c = Math.round(Math.random() * 10);
        if (c > 5) {
            opponent.attack(opponent.Attack, opponent.Luck, player.Defense);
            if (player.Items.hasOwnProperty('shieldCharm')) {
                if (player.Items.shieldCharm.active) {
                    var d = 0;
                    player.Items.shieldCharm.active = false;
                } else {
                    var d = opponent.attackDamage;
                }
            } else {
                var d = opponent.attackDamage;
            }
            player.currentHealth -= d;
            $("#playersHealth").text("Your Health: " + player.currentHealth + "/" + player.totalHealth);
            if (d === 0) {
                $battleOutputBox.append("<p class='right'>You have blocked the attack.</p>");
            } else {
                $battleOutputBox.append("<p class='right'>" + d + " damage has been inflicted upon you.</p>");
                scroll($battleOutputBox);
            }
        } else {
            opponent.defend();
            $battleOutputBox.append("<p class='right'>" + opponent.Name + " has doubled his defense for one turn.</p>");
        }
        if (player.DefenseDoubled === true) {
            player.undefend();
        }
        if (player.currentHealth <= 0) {
            postBattle(opponent);
        }
    }
    function Poison() {
        opponent.currentHealth -= 1;
        $("#opponentsHealth").text("Enemy's Health: " + opponent.currentHealth + "/" + opponent.Health);
        $battleOutputBox.append("<p> " + opponent.Name + " was hurt by poison.</p>");
        scroll($battleOutputBox);
        $("#opponentStatus").empty();
        $("#opponentStatus").append("<p style='color: green;' id='status'>Poisoned</p>");
    }
    function Burn() {
        opponent.currentHealth -= 1;
        $("#opponentsHealth").text("Enemy's Health: " + opponent.currentHealth + "/" + opponent.Health);
        $battleOutputBox.append("<p> " + opponent.Name + " was hurt by burn.</p>");
        scroll($battleOutputBox);
        $("#opponentStatus").empty();
        $("#opponentStatus").append("<p style='color: red;' id='status'>Burned</p>");
    }
    function Freeze() {
        opponent.currentHealth -= 1;
        $("#opponentsHealth").text("Enemy's Health: " + opponent.currentHealth + "/" + opponent.Health);
        $battleOutputBox.append("<p> " + opponent.Name + " was hurt by freeze.</p>");
        scroll($battleOutputBox);
        $("#opponentStatus").empty();
        $("#opponentStatus").append("<p style='color: blue;' id='status'>Frozen</p>");
    }
}

function postBattle(opponentBattled) {
    if (player.Win === true) {
        addToValue(1); 
        inMaze = distance > 5 ? true : false;
        $battleOutputBox.append("<p>You have won! You have gained " + player.EXPGainedCalculation(opponentBattled.Level, player.currentHealth, player.totalHealth) + " EXP. You need " + player.EXPNeededCalculation(player.Level) + " EXP to level up.</p>");
        $battleOutputBox.append("<p>Click Yes to continue.</p>");
        scroll($battleOutputBox);
        player.Win = false;
        battleInProgress = false;
    } else { 
        $battleOutputBox.append('<p>You have fainted. Refresh to start over.</p>');
        // Fix to restart quest
    }
}

var e2 = new opponent(1, "Snake", "Rattler");  
inMaze = false;
battle(e2);
var播放器={
//名字
//位置
totalHealth:20,//总运行状况
currentHealth:20,//当前运行状况
级别:1,//级别
//防卫
//攻击
//运气
温:错,
实验:0,
expneedCalculation:函数(级别){
返回(等级*100)-player.EXP;
},
EXPGainedCalculation:函数(对开级别、playerCurrentHealth、playerTotalHealth){
return player.EXP+=Math.ceil(opponentLevel*10-((playerTotalHealth-playerCurrentHealth)/2));
},
//项位于对象{Description:powers,used:false,use():yadayada}中
答案是:错,
攻击:功能(攻击、运气、对手防御){
var possibleAttacks=[];
var possibleDefense=[999];//填充一个空格的任意数字
for(var i=0;i7){
对于(var x=0;x7){
对手。烧伤=正确;
player.attackDamage+=3;
}
}else if(player.Items.hasOwnProperty(“幸运之刃”)){
var e=Math.ceil(Math.random()*10);
如果(e>7){
player.attackDamage+=1;
} 
}
对手.currentHealth-=玩家.attackDamage;
$(“#对手健康”).text(“敌人的健康:+attacker.currentHealth+”/“+attacker.Health”);
如果(player.attackDamage==0){
$battleOutputBox.append(“您的攻击已被阻止”

”; }否则{ $battleOutputBox.append(你造成“+player.attackDamage+”伤害。

”; 滚动($battleOutputBox); } 如果(对手.防御加倍===真){ 对手。未设防(); } 如果(对手中毒===真){ 毒药(); }else if(对手燃烧===真){ 烧伤(); }else if(对手冻结===真){ 冻结(); } 如果(对手当前健康5){ 对手。攻击(对手。攻击,对手。运气,玩家。防御); if(player.Items.hasOwnProperty('shieldCharm')){ 如果(玩家.物品.防护罩.激活){ var d=0; player.Items.shieldCharm.active=false; }否则{ var d=对手攻击伤害; } }否则{ var d=对手攻击伤害; } player.currentHealth-=d; $(“#玩家健康”).text(“您的健康状况:+player.currentHealth+”/“+player.totalHealth”); 如果(d==0){ $battleOutputBox.append(“

您已经阻止了攻击。

”; }否则{ $battleOutputBox.append(“

”+d+”伤害已对您造成。

”; 滚动($battleOutputBox); } }否则{ 对手。防守(); $battleOutputBox.append(“

”+对手.Name+”在一回合内防御翻倍。

”; } 如果(player.com===true){ player.undefend(); } if(player.currentHealth 5?真:假; $battleOutputBox.append(“你赢了!你获得了”+player.EXPGainedCalculation(opponentBattled.Level,player.currentHealth,player.totalHealth)+“EXP.你需要”+player.expneedCalculation(player.Level)+“EXP”升级。

”; $battleOutputBox.append(单击“是”继续。

”; 滚动($battleOutputBox); player.Win=false; battleInProgress=假; }否则{ $battleOutputBox.append(“您晕倒了。刷新以重新开始。

”); //修复重新启动任务 } } 变量e2=新对手(1,“蛇”、“响尾蛇”); inMaze=假; 战斗(e2);
在您的
battle
函数中,您绑定的是点击处理程序。这些处理程序永远不会解除绑定。如果您再次调用
battle
,您绑定的是额外的处理程序,您不会替换它们。因此,您现在有两组处理程序,一组是旧的
对手
,另一组是新的.Both将开火。

在您的
battle
函数中,您绑定的是点击处理程序。这些处理程序永远不会解除绑定。如果您再次调用
battle
,您绑定的是其他处理程序,您不会替换它们。因此,您现在有两组处理程序,一组是旧的
对手,另一组是新增。两者都将启动。

在您的
battle
函数中,您正在绑定单击处理程序。这些处理程序永远不会解除绑定。如果您调用