Re: multidimensional tables/arrays
Posted: Tue Dec 22, 2009 8:34 pm
Thanks for the help, kikito and Robin. This worked. Now that I've gotten around that obstacle I've run into another problem. I'm trying to use FOR to iterate through my four objects but again the compiler tells me that I'm attempting to index field 'object' (a nil value). The object variable should be defined though, so I guess it's the use of . Got any idea how I messed up again? The LUA documentation didn't really solve my problem here.
Code: Select all
objects = {
object1 = {
type = circle,
sprite = images.circle1,
x = math.random(50,350),
y = math.random(50,350),
s = math.random(50,150)/100},
object2 = {
type = circle,
sprite = images.circle2,
x = math.random(50,350),
y = math.random(50,350),
s = math.random(50,150)/100},
object3 = {
type = box,
sprite = images.box3,
x = math.random(50,350),
y = math.random(50,350),
s = math.random(50,150)/100},
object4 = {
type = box,
sprite = images.box4,
x = math.random(50,350),
y = math.random(50,350),
s = math.random(50,150)/100},
}
for i = 1,4 do
if math.random(1,10) <= 5 then
objects.object[i].type = circle
else
objects.object[i].type = box
end
end
for i = 1,4 do
if objects.object[i].type == circle
then
if math.random(1,10) <= 5 then
objects.object[i].sprite = images.circle1
else
objects.object[i].sprite = images.circle2
end
else
if math.random(1,10) <= 5 then
objects.object[i].sprite = images.box3
else
objects.object[i].sprite = images.box4
end
end
end
for i = 1,4 do
if objects.object[i].sprite == images.circle1 then
objects.object[i].r = 78*objects.box[i].s
end
end
for i = 1,4 do
if objects.object[i].sprite == images.circle2 then
objects.object[i].r = 57*objects.box[i].s
end
end
for i = 1,4 do
if objects.object[i].sprite == images.box3 then
objects.object[i].w = 100*objects.object[i].s
objects.object[i].h = 100*objects.object[i].s
end
end
for i = 1,4 do
if objects.object[i].sprite == images.box4 then
objects.object[i].w = 140*objects.object[i].s
objects.object[i].h = 70*objects.object[i].s
end
end