我正在尝试将不同对象的多个实例添加到另一个对象的数组中。然而,我遇到了麻烦。
*Creates the player*
function Player(){
this.name = "";
this.status = "Alive";
this.primaryClass = null;
this.secondaryClass = null;
this.strength = 0;
this.stamina = 0;
this.mystica = 0;
this.health = 0;
this.primaryWeapon = null;
this.offHand = null;
this.accuracy = 0;
this.block = 0;
this.baseDamage = 0;
this.maxDamage = 0;
this.attackSpeed = 0;
this.shield = null;
this.armor = null;
this.armorRating = 0;
this.exp = 0;
}
*Creates the sword weapon*
function LongSword(){
this.name = "";
this.attackSpeed = 1;
this.baseDamage = 10;
this.maxDamage = 15;
this.durability = 100;
this.block = 5;
this.exp = 0;
}
*Creates the Long Sword skills*
function Stab(){
this.name = "Stab";
this.status = "Unlocked";
this.damage = 0;
this.damageModifier = 0.75;
this.cooldown = 5;
this.exp = 0;
this.desc = "Stabs your opponent for an additional " +parseInt(this.damageModifier * 100) +"% of your base damage.";
}
*Equips the Player weapon(s)*
Player.prototype.equipWeapon = function equipWeapon(main, offHand){
if(this.primaryClass.dualWield){
this.primaryWeapon = main;
if(offHand){
this.offHand = offHand;
this.baseDamage += (this.strength + (main.baseDamage + (offHand.baseDamage / 2))) / 10;
this.maxDamage += (this.strength + (main.maxDamage + (offHand.maxDamage / 2))) / 5;
this.attackSpeed += main.attackSpeed + (offHand.attackSpeed / 2);
this.block += main.block + offHand.block;
}
}
else{
this.primaryWeapon = main;
this.offHand = null;
this.baseDamage += (this.strength + main.baseDamage) / 10;
this.maxDamage += (this.strength + main.maxDamage) / 5;
this.attackSpeed += main.attackSpeed;
this.block += main.block;
}
if(!this.primaryClass.dualWield && offHand){
console.log("Your class can not wield dual weapons.");
}
}
*Equips the Weapon skills*
LongSword.prototype.skills = function skills(skill){
this.skills = [];
skill.damage = parseFloat((this.baseDamage / skill.damageModifier).toFixed(1));
this.skills.push(skill);
}
这些对象构成了我尝试做的事情的基本要素。所以,当我去实例化每一个时,
var Robert = new Player();
Robert.equipWeapon(new LongSword());
Robert.primaryWeapon.skills(new Stab());
我得到了我想要的结果。但是,如果我尝试添加Stab()
的另一个实例,使其看起来像这样
var Robert = new Player();
Robert.equipWeapon(new LongSword());
Robert.primaryWeapon.skills(new Stab());
Robert.primaryWeapon.skills(new Stab());
我得到TypeError:Robert.primaryWeapon.skills is not a function
。为什么它会正常工作一次,但不是第二次。我试图实现的最终结果是,如果安慰Robert.primaryWeapon.skills
,我应该看到Stab
对象的两个实例。
答案 0 :(得分:1)
Longsword
原型中存在两个问题。
首先,您将使用存储skill
数组替换您的函数,该数组具有相同的名称:
LongSword.prototype.skills = function skills(skill){
this.skills = []; //Overrides your function
...
导致您的错误Robert.primaryWeapon.skills is not a function
,导致一旦您调用它,它确实是一个数组。
要修复它,只需更改其中一个函数或数组的名称。
其次,每次调用函数时,都会将skills
数组初始化为空数组,每次都重置它。你应该用Longsword
的protoype初始化它。
以下是这些修补程序(fiddle with it if you want)的示例:
function LongSword(){
this.skills = [];
...
LongSword.prototype.addSkill = function skills(skill){
...
然后,您将能够添加多种技能:
var Robert = new Player();
Robert.equipWeapon(new LongSword());
Robert.primaryWeapon.addSkill(new Stab());
Robert.primaryWeapon.addSkill(new Stab());