2013-04-09 62 views
1

這是一個奇怪的問題,但它讓我感到困惑。我希望能夠在一個基於id的系統上存儲x和y的合作伙伴,例如:id.1.x = 10,id.1.y = 15:id.2.x = 50,id.2.y = 42,我正試圖爲我做一個功能,我遇到了問題。這裏是我的代碼如何在Lua中爲多個數據類型數組建立索引值?

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) 

我真的很疲憊,很想把這個休息,所以如果有人可以幫助它將不勝感激。

回答

3
function box(xpos,ypos,id) 
    a[id] = {x = xpos, y = ypos} 
end 
相關問題