声明为现有阵列并不起作用

时间:2018-04-29 22:04:10

标签: javascript processing p5.js

在您阅读此代码并使用完之前,请先了解一个名为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,
};

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

2 个答案:

答案 0 :(得分:1)

使用getter functions的另一种解决方案。有关错误发生的原因,请查看@bloodyKnuckles的答案。



i  rmse.pls rmse.h2o
1   1  8.839967 40.99896
2   2  9.347349 29.94260
3   3  4.240366 14.75890
4   4 17.901563 29.89181
5   5  4.686803 66.04993
6   6 31.717909 10.28799
7   7  2.066342 32.74828
8   8 15.979214 21.05928
9   9 19.454079 10.88551
10 10 27.039400 68.27017




答案 1 :(得分:0)

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],
  ...
};