[Solved] Tables and Objects

Im doing a game, so I have to put 9 objects in the screen. So I decided do this (Im using DIrector, this may be relevant):

tabela = {{"","",""},{"","",""},{"","",""}}  
  
init\_btn = function ()  
 local button = display.newImage("image/botao.png")  
 for l = 1, 3 do  
 for c = 1, 3 do  
 tabela[l][c] = button  
 tabela[l][c].x = 100 \* c  
 tabela[l][c].y = 100 \* l  
 localGroup:insert (tabela[l][c])  
 print("tabela: l c:", l, c)  
 end  
 end  
end  

So, What im doing wrong? All 9 buttons appear in the exact same place. This “print” is here just to make sure I did not make anithing wrong in the for statement.

Ideas? I will put an event in each one after this, so I will change the image after a “tap” event.

Thanks! [import]uid: 9133 topic_id: 13311 reply_id: 313311[/import]

Ok, after 1 hour think WHY was happening what I explain, I “just” remember how veriables in lua works. So, was not ALL objects in the same place, but was the SAME object.

The solution was simply “kill” button variable and add display.newImg straight to the table.

I leaving this post, maybe will hel someone.

[import]uid: 9133 topic_id: 13311 reply_id: 48869[/import]

I generally do not run the code when I try to debug, and I cannot sitting at an airport lounge, between flights.

So,

  1. Why do you want to use
    [lua]localGroup:insert(tabela[l][c])[/lua]

when button is already the item.

so try

[lua]tabela = {{"","",""},{"","",""},{"","",""}}

init_btn = function ()
for l = 1, 3 do
for c = 1, 3 do
local button = display.newImage(“image/botao.png”)
tabela[l][c] = button
tabela[l][c].x = 100 * c
tabela[l][c].y = 100 * l
localGroup:insert (button)
print(“tabela: l c:”, l, c)
end
end
end[/lua]
What you were doing was setting every element in the array as button, which is *only* one display object, not 9 objects. So you need to create 9 objects, which are now created in the loop.

I also have something to say about the way you use the tables, but I’ll leave that for a later post.

hope this works for you,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13311 reply_id: 48947[/import]

maybe im doing something wrong, but with this approach, I was receiving error. Error I mean All 9 objects was in the same position.

I will see this appproach again.

Thanks [import]uid: 9133 topic_id: 13311 reply_id: 49105[/import]