1
我试图做一个简单的“选择你的冒险!”游戏,我似乎遇到了问题。我不知道如何定位我制作的这个多维数组的某些值。 我做了一个'经销商/贸易商',并有这样的销售他的物品。JavaScript抓取多维数组的某些值
var dealer = [
[
{type: "weapon", cost: 250, name: "Claymore"},
{type: "weapon", cost: 75, name: "Dagger"},
{type: "weapon", cost: 350, name: "Magic Staff"},
{type: "weapon", cost: 150, name: "Sword"},
{type: "weapon", cost: 125, name: "Bow"},
{type: "weapon", cost: 125, name: "Crossbow"},
{type: "weapon", cost: 5, name: "Arrow"},
{type: "weapon", cost: 15, name: "Bolt"}
],
[
{type: "clothing", slot: "head", name: "Helmet"},
{type: "clothing", slot: "head", name: "Hood"},
{type: "clothing", slot: "chest", name: "Chestplate"},
{type: "clothing", slot: "chest", name: "Tunic"},
{type: "clothing", slot: "chest", name: "Robe"},
{type: "clothing", slot: "leggings", name: "Legplates"},
{type: "clothing", slot: "leggings", name: "Leggings"},
{type: "clothing", slot: "leggings", slot: "Undergarments"},
{type: "clothing", slot: "feet", name: "Boots"},
{type: "clothing", slot: "feet", name: "Armored Boots"}
]
]
而且我有一个操作经销商的功能,比如购买物品,我不知道如何锁定某些值/数组。这就是我想要的。
function merchant() = {
var armor = function(slot, name, material) {
if(dealer[2].slot === "feet" && dealer[2].name = "Boots"}
money -= 10;
}
}
}
这应该是针对第二批服装,并寻找插槽脚和靴子的名称,对吗?
貌似无效的javascript对我来说,有一个等号第一功能decleration,和'armor'是局部的作用,如何你打算打电话了吗? – adeneo 2015-02-06 18:18:42
另外,数组是零索引的,所以第二个数组是'[1]' – adeneo 2015-02-06 18:19:13
您可以将经销商id传递到装甲函数中。而且,索引从零开始(0)。 – 2015-02-06 18:21:26