我想将一个变量与名为 items.json 的文件中的表连接起来。我的文件内的属性称为“pos1_name”、“pos2_name”等...所以我想使用属性名称中的数字作为变量。我尝试过: item.pos..i.._name
但它不起作用。
items = VarLoad.loadTable("items.json")
for i=1,7 do
--var ="1"
--print(table.concat(items,"",2))
if items.pos1=="1" then
item[i] = display.newImageRect("items/"..items.pos1_name..".png", 80 , 80)
item[i].x = holder[i].x+10
item[i].anchorX=0
item[i].y=holder[i].y
itemGroup:insert (item[i])
item[i].destination="item1"
item[i]:addEventListener( "touch", onSwitchPress )
item_bigsize[i] = display.newImageRect("items/"..items.pos1_name..".png", 240 , 240)
item_bigsize[i].x = 950
item_bigsize[i].anchorX=0
item_bigsize[i].y=display.contentCenterY-130
group:insert (item_bigsize[i])
item_bigsize[i].isVisible=true
end
i=i+1
end
最佳答案
My attributes inside of the files are called "pos1_name", "pos2_name", etc... So I would like to use the number in the attribute name, as a variable. I've try : item.pos..i.._name but it doesn't work.
您需要使用全表表示法:items.pos1_name
是 items['pos1_name']
的快捷方式,因此如果您需要动态生成 key ,您可以可以使用这样的东西:
print(items['pos'..i..'name'])
关于json - 将 json 文件与变量连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26427894/