在 Lua 中,如何引用多个数据类型的数组中的值?

这是一个奇怪的问题,但它困扰着我。我想在基于id的系统上存储x和y坐标,例如: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)

我真的很疲惫,希望能解决这个问题,如果有人能提供帮助,将不胜感激。

点赞
用户1847592
用户1847592
function box(xpos,ypos,id)
  a[id] = {x = xpos, y = ypos}
end
function box(xpos,ypos,id)
  a[id] = {x = xpos, y = ypos}
end
2013-04-09 23:08:40