在阅读此代码并精疲力尽之前,请先了解其名为p5.js的开源JavaScript库。

https://p5js.org/reference/

我有基本软件包,没有附加组件。看看这个...

var weapons = {
    //[WEAPON NAME]: [accuracy,repeat,recoil,recoilmax,recoil-recovery,damage,firerate,reloadspeed],
    M4A1: [10, 10, 0.2, 5, 1],

    //primary: weapons.M4A1,//ERROR
    //secondary: weapons.M4A1,//ERROR
    equipped: weapons.M4A1,
};

var inventory = {
  accuracy: weapons.M4A1[0],
  accuracyREF: 10,
  recoil: 0.2,
  recoilMAX: 3,
  recoilRecovery: 1,
  damage: 1,
  fireRate: 1,
};


显示空白屏幕,直到我摆脱声明武器的行

我也尝试使用SPLICE,如下所示的p5.JS语法arrayCopy()一样

var weapons = {
  //[WEAPON NAME]: [accuracy,repeat,recoil,recoilmax,recoil-recovery,damage,firerate,reloadspeed],
  M4A1: [10, 10, 0.2, 5, 1],

  //primary: weapons.M4A1,//ERROR
  //secondary: weapons.M4A1,//ERROR
  equipped: [0,0,0,0,0],
};

splice(weapons.M4A1, equipped, 0);

var inventory = {
  accuracy: weapons.M4A1[0],
  accuracyREF: 10,
  recoil: 0.2,
  recoilMAX: 3,
  recoilRecovery: 1,
  damage: 1,
  fireRate: 1,
};


这对我的游戏至关重要,如果无法完成,我将不得不放弃它

最佳答案

var weapons失败,因为您正在尝试为其分配属性之一,而该属性尚不存在。因此var inventory失败,因为由于第一个错误而未定义weapons

要了解发生了什么,请从这里开始:

var weapons = {
  M4A1: [10, 10, 0.2, 5, 1],
  equipped: weapons.M4A1 // <-- weapons.M4A1 must be determined first
};
// weapons === undefined


在实例化var weapons之前,JS需要处理weapons.M4A1的值才能分配给equipped。问题是weapons还不存在。

接下来,您要实例化inventory

// remember, weapons === undefined
var inventory = {
  accuracy: weapons.M4A1[0], // again, weapons.M4A1[0] must be determined
  accuracyREF: 10,
  ...
};
// inventory === undefined


...因此,inventory也无法实例化。



尝试2:splice(weapons.M4A1, equipped, 0);

p5/splice指出splice适用于数组,而不适用于对象(除了实现的其他问题)。



你能做这个吗?

var weapons = {
  M4A1: [10, 10, 0.2, 5, 1],
  equipped: [10, 10, 0.2, 5, 1]
};

var inventory = {
  accuracy: weapons.M4A1[0],
  ...
};


或这个?

var weapons = {
  M4A1: [10, 10, 0.2, 5, 1],
  equipped: [0,0,0,0,0]
};
weapons.equipped = weapons.M4A1;

var inventory = {
  accuracy: weapons.M4A1[0],
  ...
};

09-16 06:37