如何索引Lua中多数据类型数组中的值?

时间:2013-04-09 22:38:43

标签: arrays lua coordinates null

这是一个奇怪的问题,但令我感到困惑。我希望能够在基于id的系统上存储x和y co-ords,例如:id.1.x = 10,id.1.y = 15:id.2.x = 50,id.2.y = 42我试图为我做一个功能,我有问题。这是我的代码

a = { p = {x,y}}
function box(xpos,ypos,id)
a[id].x = xpos
a[id].y = ypos
end
box(25,30,1)
box(45,10,2)
print(a[1].x.." "..a[1].y)
print(a[2].x.." "..a[2].y)

我想要打印:

25 30
45 10

但我收到错误:

attempt to index global '?' (a nil value)

我真的很精疲力竭,并希望让它休息,所以如果有人能提供帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:3)

function box(xpos,ypos,id)
  a[id] = {x = xpos, y = ypos}
end