需要一些帮助与一个脚本完全对数组,我正在做
skill = [
//[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
//Schoolgirl, Fighter
[0, "Steel Punch", 0, 0, null, null],
[1, "Shockwave", 1, 1, 2, null],
[2, "Bull's Eye", 10, 2, 2, null],
[3, "Burning Rave", 20, 2, 2, null],
[4, "Shockvibe", 20, 1, 2, null],
[5, "Sense Breaker", 20, 1, 2, null],
[6, "Luck Breaker", 20, 1, 2, null],
[7, "Pumping Heart", 25, 3, 3, skill[3], 1],
[8, "Armor Breaker", 30, 2, 2, skill[1], 10],
[9, "Upper Smash", 40, 2, 2, skill[2], 10],
[10, "Hyper Beat", 45, 4, 3, [skill[2],skill[3]], [10,10]],
[11, "Tornado Bomb", 50, 3, 3, skill[8], 1]
];
我需要在数组内部,在某些点,再次调用数组把数组的值放进去,就像这里一样。理论上这工作得很好,没有任何错误,但是当我调用它里面的数组时,它说它是"未定义的"。
有谁知道我怎么能做到这一点,而不重写上面的一切吗?(因为我在+- 300代码行中使用这个)。
经过漫长的过程,我已经提出了一个解决方案,将取代所有先决条件,即使他们是几个层次的深度(例如,skill_3需要skill_2,需要skill_1…)。
这将要求你的skill
变量被正确声明(在你的问题中,不是所有的技能都有7个变量)。
下面是这个变量的示例:
var skill = [
//[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
[0, "Steel Punch", 0, 0, 0, null, null],
[1, "Shockwave", 1, 1, 2, null, null],
[2, "Bull's Eye", 10, 2, 2, 7, null],
[3, "Burning Rave", 20, 2, 2, null, null],
[4, "Shockvibe", 20, 1, 2, null, null],
[5, "Sense Breaker",20, 1, 2, null, null],
[6, "Luck Breaker", 20, 1, 2, null, null],
[7, "Pumping Heart",25, 3, 3, 3, 1],
[8, "Armor Breaker",30, 2, 2, 7, 10],
[9, "Upper Smash", 40, 2, 2, 2, 10],
[10,"Hyper Beat", 45, 4, 3, [2,3], [10,10]],
[11,"Tornado Bomb", 50, 3, 3, 8, 1]
];
现在,我想到了一个函数setPrerequisites()
,它将为一个技能递归地设置它的先决条件:
Array.prototype.setPrerequisites = function(){
if (typeof this[5] === "number")
{
this[5]=skill[getPosOfSkill(this[5])];
this[5].setPrerequisites();
}
else if (this[5] instanceof Array)
{
if (this[5].isSkill()) this[5].setPrerequisites();
else
{
for(var i = 0; i < this[5].length; i++)
{
this[5][i] = skill[getPosOfSkill(this[5][i])];
this[5][i].setPrerequisites();
}
}
}
}
该函数使用isSkill()
来判断一个数组是技能还是技能id数组:
Array.prototype.isSkill = function(){
return this.length==7 && typeof this[1]==="string";
}
它还使用getPosOfSkill(id)
来寻找正确的技能,以防您的技能在没有特定顺序列出,或者如果ID缺失:
function getPosOfSkill(id){
for(var i=0; i<skill.length; i++) if (skill[i][0]==id) return i;
return false;
}
你所要做的就是声明你的skill
变量,然后填充它:
for (var i = 0; i < skill.length; i++) skill[i].setPrerequisites();
// if you want to see the results
console.log(skill);
这是一个jsFiddle Demo
您必须重新考虑这里的整个方法(建议),或者首先将这些项设置为null,然后重新运行声明:
skill = [
//[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
//Schoolgirl, Fighter
[0, "Steel Punch", 0, 0, null, null],
[1, "Shockwave", 1, 1, 2, null],
[2, "Bull's Eye", 10, 2, 2, null],
[3, "Burning Rave", 20, 2, 2, null],
[4, "Shockvibe", 20, 1, 2, null],
[5, "Sense Breaker", 20, 1, 2, null],
[6, "Luck Breaker", 20, 1, 2, null],
[7, "Pumping Heart", 25, 3, 3, null, 1],
[8, "Armor Breaker", 30, 2, 2, null, 10],
[9, "Upper Smash", 40, 2, 2, null, 10],
[10, "Hyper Beat", 45, 4, 3, null, null],
[11, "Tornado Bomb", 50, 3, 3, null, 1]
];
skill = [
//[ID, "NAME", TMLEVEL, Learn, Mastery, Prerequisite, PrerequisiteLvl],
//Schoolgirl, Fighter
[0, "Steel Punch", 0, 0, null, null],
[1, "Shockwave", 1, 1, 2, null],
[2, "Bull's Eye", 10, 2, 2, null],
[3, "Burning Rave", 20, 2, 2, null],
[4, "Shockvibe", 20, 1, 2, null],
[5, "Sense Breaker", 20, 1, 2, null],
[6, "Luck Breaker", 20, 1, 2, null],
[7, "Pumping Heart", 25, 3, 3, skill[3], 1],
[8, "Armor Breaker", 30, 2, 2, skill[1], 10],
[9, "Upper Smash", 40, 2, 2, skill[2], 10],
[10, "Hyper Beat", 45, 4, 3, [skill[2],skill[3]], [10,10]],
[11, "Tornado Bomb", 50, 3, 3, skill[8], 1]
];
这样,你要访问的数组元素已经存在,现在你只是覆盖它们。
我想我找到了一个简单的方法,但仍然有问题,如果它被定义后,但因为我只调用之前定义的值,现在没有问题。
但如果有人知道什么更好的方法,请告诉我。
下面是我只使用数组所做的:
var skill = [];
skill[0] = [0, "Steel Punch", 0, 0, null, null];
skill[1] = [1, "Shockwave", 1, 1, 2, null];
skill[2] = [2, "Bull's Eye", 10, 2, 2, null];
skill[3] = [3, "Burning Rave", 20, 2, 2, null];
skill[4] = [4, "Shockvibe", 20, 1, 2, null];
skill[5] = [5, "Sense Breaker", 20, 1, 2, null];
skill[6] = [6, "Luck Breaker", 20, 1, 2, null];
skill[7] = [7, "Pumping Heart", 25, 3, 3, skill[3], 1];
skill[8] = [8, "Armor Breaker", 30, 2, 2, skill[1], 10];
skill[9] = [9, "Upper Smash", 40, 2, 2, skill[2], 10];
skill[10] = [10, "Hyper Beat", 45, 4, 3, [skill[2],skill[3]], [10,10]];
skill[11] = [11, "Tornado Bomb", 50, 3, 3, skill[8], 1];
这样我定义它们,我仍然使用数组我想要的(1个晚上的睡眠让我想得更好:p)