gpt4 book ai didi

javascript - 将一个对象的多个实例添加到另一个对象的数组中;

转载 作者:行者123 更新时间:2023-11-27 22:42:07 26 4
gpt4 key购买 nike

我正在尝试将不同对象的多个实例添加到另一个对象的数组中。不过我遇到了麻烦。

*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 不是函数。为什么一次可以正常,第二次就不行了?我想要实现的最终结果是,如果对 Robert.primaryWeapon.skills 进行控制台,我应该会看到 Stab 对象的两个实例。

最佳答案

您的 Longsword 原型(prototype)存在两个问题。

首先,您将用存储 skill 数组替换您的函数,它们具有相同的名称:

LongSword.prototype.skills = function skills(skill){
this.skills = []; //Overrides your function
...

这会导致你的错误,Robert.primaryWeapon.skills不是一个函数,因为一旦你调用它,它确实是一个数组。

要修复此问题,只需更改其中一个函数或数组的名称即可。

其次,每次调用该函数时,您都会将 skills 数组初始化为空数组,并每次都会重置它。您应该在 Longsword 的原型(prototype)中初始化它。

<小时/>

以下是包含这些修复的示例 ( 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());

关于javascript - 将一个对象的多个实例添加到另一个对象的数组中;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38675100/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com