作为较大形式的一部分,我有一个二维数组的数据。目前它是硬编码的(为了便于开发),但最终它将存储在本地存储中,并且任何更改都将被保存。我正在使用输入来显示数据,并允许进行更改。
上下文是我儿子正在设计并希望加快计算速度的角色扮演游戏。一个角色拥有各种盔甲,这也赋予了他们额外的技能。下面是一段摘录,其中给出了需要显示的部分内容:
Stats: [{ Component: "Head", Armor: 10, Evasion: 0, HP: 20, MP: 10 },
{ Component: "Chest", Armor: 20, Evasion: 10, HP: 0, MP: 0 },
{ Component: "Arms", Armor: 20, Evasion: 5, HP: 0, MP: 0 },
{ Component: "Hands", Armor: 10, Evasion: 20, HP: 5, MP: 0},
{ Component: "Legs", Armor: 15, Evasion: 0, HP: 0, MP: 0},
{ Component: "Feet", Armor: 10, Evasion: 0, HP: 0, MP: 0 }]
它在表单上显示为:
Head Chest Arms Hands Legs Feet
Armor 10 20 20 10 15 10
Evasion 0 10 5 20 0 0
HP 20 0 0 5 0 0
MP 10 0 0 0 0 0
为了显示像力量和敏捷这样的简单评级,我使用以下 HTML 完成了它:
<label class="lab" for="strengthID">Strength:</label>
<input class="inp" type="number" id="strengthID" step="any">
<label class="lab" for="agilityID">Agility:</label>
<input class="inp" type="number" id="agilityID" step="any">
注意:“lab”和“inp”类设置标签和输入标签的宽度。
以下是我用来显示所需字符数据的 javascript 的摘录:
var strength;
var agility;
window.onload = init;
function init() {
people = [];
people[0] = { // here is where I provide the data during development
Name: "Strider", Hp: 300, Strength: 30, Agility: 20, ... ,
Stats: [{Component: "Head", Armor: 10, ... ]
people[1] = { ...
strength = document.getElementById("strengthID");
agility = document.getElementById("agilityID");
...
assignPeople(0); // to initialize the form with the first person's data.
}
function assignPeople(indx) {
str.value = people[indx].Strength;
agility.value = people[indx].Agility;
...
}
我不知道该怎么做是很好地显示统计数据数组。当然,我可以费力地对矩阵的每一对进行编码,例如:
<input class="inp" type="number" id="Armor_Head_ID" step="any">
<input class="inp" type="number" id="Armor_Chest_ID" step="any">
...
但这会导致大量变量。我用谷歌搜索了答案,但可能没有使用正确的搜索词。
任何帮助将不胜感激。