Проще говоря, у меня есть это маленькое приложение, и оно ломается, когда я вызываю функцию applyDamage в объекте player.Когда я попробовал отладку и получил тип, он говорит, что это число.Тем не менее, когда я запускаю, он не работает должным образом после первой итерации, потому что он выдает ошибку 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();