This should be easy but I'm wracking my brains trying to do it. :-)
I want to put a set of co-ordinates into a table...
coords = {}
table.insert(coords, x1,y1)
table.insert(coords, x2,y2)
table.insert(coords, x3,y3)
What code is it to add the x,y to the table please?
Thank you
Comments
local coords = {
{1,2},
{11,55},
{4,5},
{134,233},
}
print(coords[1][2])
or
local coords = {
{x=1,y=2},
{x=11,y=22},
{x=111,y=222},
{x=1111,y=2222},
}
print(coords[2].x)
Learn Lua for iOS Game Development from Apress at http://www.apress.com/9781430246626
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
poly:set(unpack(coords))
And for it to work like
poly:set(23,11,66,77,88,55,33,44)
I'm trying to feed coords dynamically into a table then read them into the poly:set box2d shape.
Thanks
table.insert(coords, x..","..y)
But of course you get the error:
main.lua:84: bad argument #1 to 'unpack' (table expected, got string)
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
for loop
table.insert(x)
table.insert(y)
end for loop
then...
poly:set(unpack(coords))
So if you are expecting that unpack will unpack and return x=val, y=val, width=val, height=val, etc then you would head for massive disappointment.
But if you insert several values either all at once as
now if you use unpack(coords) it will work.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Thanks OzApps.
The stumbling block was I was thinking of pairs rather than individual numbers.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps