执行算数运算后,对象属性不返回值

时间:2018-10-03 18:58:35

标签: javascript javascript-objects

简单的说,我有这个小应用程序,当我在播放器对象中调用applyDamage函数时,它中断了。当我尝试调试并得到调试类型时,它说是一个数字。但是,当我运行该程序时,它在第一次迭代后无法正确计算,因为它抛出了NaN错误。如果有的话,我真的很感谢您的帮助。我尝试了许多不同的简单技巧,因此,如果您有时间查看我的代码,请仅提供高级答复。

尽管如此,

提前谢谢!

var gameOver = function(){
    console.log("Game Over");
  return 0;
};

var youWin = function(arrPlayersAlive){
  console.log("\nCongratulations, you have defeated Scarlet Byte.\n");
  console.log("Players Alive:");
  console.log("==============");
  for(var x = 0; x < arrPlayersAlive.length; x++){
    console.log(arrPlayersAlive[x].name);
  }
};

var rndNumbGen = function(min, max){
  return Math.round(Math.random() * (max - min) + min);
};

var fight = function(arrActivePlayers, arrActiveMinion){
    var playersAlive = arrActivePlayers.length;
  var minionsAlive = arrActiveMinion.length;
  for(var i = 0; i < arrActivePlayers.length; i++){
    var weapon = new Weapon(arrActivePlayers[i].myWeapon);
    while(arrActivePlayers[i].isAlive() === true){
      for(var x = 0; x < arrActiveMinion.length; x++){
        while(arrActiveMinion[x].isAlive() === true){
          weapon.attack(arrActivePlayers[i], arrActiveMinion[x]);
        }
        if(arrActiveMinion[x].isAlive() === false){
            minionsAlive = minionsAlive - 1;
          if(minionsAlive === 0){ youWin(arrActivePlayers); }
        }
      }
    }
    if(arrActivePlayers[i].isAlive() === false){
        playersAlive = playersAlive - 1;
      if(playersAlive === 0){ gameOver(); }
    }
  }
};

function Player(strName, arrWeapons){
    this.name = strName;
  this.health = parseInt(10);
  this.strength = parseInt(2);
  this.weapons = arrWeapons;
  this.myWeapon = "unarmed";
  this.applyDamage = function(intDamage){
    this.health -= parseInt(intDamage);
    console.log(this.name + " has sustained " + intDamage + " amount of damage. Remainig health " + this.health);
  };
  this.isAlive = function(){
    if(this.health <= 0){
        return false;
    }
    else{
        return true;
    }
  };
  this.attackWith = function(){
    this.myWeapon = this.weapons[rndNumbGen(0,7)];
  };
}

function Minion(){
  this.name = "Minion";
  this.health = parseInt(5);
  this.strength = parseInt(2);
  this.applyDamage = function(intDamage){
    this.health = this.health - parseInt(intDamage);
    console.log(this.name + " has sustained " + intDamage + " amount of damage.");
  };
  this.isAlive = function(){
    if(this.health <= 0){
      return false;
    }
    else{
      return true;
    }
  };
  this.attack = function(objPlayer){
    objPlayer.applyDamage(this.strength);
  };
}

function Weapon(strName){
    this.name = strName;
  this.damage = rndNumbGen(1,5);
  this.attack = function(objPlayer, objMinion){
    objMinion.applyDamage(objPlayer.strength);
    if(objMinion.isAlive() === true){
        objPlayer.applyDamage(objMinion.attack(objPlayer));
    }
  };
}

function Game(){
    this.players = [];
  this.minion = [];
  this.createMinions = function(){
    for(var i = 0; i < 20; i++){
        this.minion[i] = new Minion();
    }
  };
  this.createPlayers = function(){
    var weaponsCache = ["Over Gold", "Kunai", "Flame Thrower", "BuzzBomb", "God_Scythe", "Ankle Biting Chihuahua Launcher", "Mini Black Hole", "Small Pox Blanket"];
      var playerNames = ["Loki_Blacksteel", "Naruto", "CERN", "Harambe", "Kung Fu Joe"];
    for(var n = 0; n < playerNames.length; n++){
        this.players[n] = new Player(playerNames[n], weaponsCache);
    }
  };
  this.play = function(){
    console.log("Simulating Battle\n");
    this.createMinions();
    this.createPlayers();
    fight(this.players, this.minion);
  };
}

var myGame = new Game();
myGame.play();

2 个答案:

答案 0 :(得分:0)

我看到的一件事是,您在武器课中拥有

objPlayer.applyDamage(objMinion.attack(objPlayer));

但是applyDamage()需要一个整数,而您的attack()函数不会返回任何内容

您正在尝试在parseInt方法内调用applyDamage,但是您向其传递了未定义的值,这说明了NaN错误。

希望有帮助。

console.log(parseInt(undefined))

答案 1 :(得分:0)

您两次调用applyDamage函数 首先:

objPlayer.applyDamage(objMinion.attack(objPlayer));

第二: objMinion.attack(objPlayer)个电话对其身体造成伤害

 this.attack = function(objPlayer){
    objPlayer.applyDamage(this.strength);
 };

第一个以未定义的值调用applyDamage(如果函数没有返回状态,则返回未定义),第二个以2调用。您可以尝试写objMinion.attack(objPlayer));而不是objPlayer.applyDamage(objMinion.attack(objPlayer)); < / p>

相关问题