Table doubt

Hi everyone :slight_smile:

i am working with tables and need some help to remove the objects that collides with a spawner

  
function spawner:collision (event)  
 if event.phase == "began" then  
 for i = 1, #block1, 1 do  
 if block1[i].id == event.other.id then  
 block1[i]:removeSelf()  
 table.remove(block1,i)  
  
 return true  
 end  
 end   
 end   
 end  
  

it is my spawner collision function.
Some time ago i use a tables code like it:

local block1 {}  
  
local function myspawner (event)  
 block1[#block1+1] = display.newImage("my image")  
 block1[#block1].x = 100  
 block1[#block1].y=100  
  
local function my\_function\_to\_do\_everything (event)  
 block1[#block1]. some code etc etc  
end  
block1[#block1]:addEventListener("collision", my\_function\_to\_do\_everything)  
  
end  
  

and now i have some code like
P.S : attention inside the function “my_function_to_do_everything”

local block1 {}  
  
local function myspawner (event)  
 block1[#block1+1] = display.newImage("my image")  
 block1[#block1].x = 100  
 block1[#block1].y=100  
  
local function my\_function\_to\_do\_everything (event)  
 block1[1]. some code etc etc  
 block1[2]. some code etc etc  
etc  
end  
block1[#block1]:addEventListener("collision", my\_function\_to\_do\_everything)  
  
end  

now inside the function when i need, i use the blocks separete, like, block1[1], block1[2] etc dont use block1[#block1] and the spawner collision function dont work with this new code used on tables, only work with the old code when i use block1[#block1], anyone can give me some help?

Thanks
[import]uid: 26056 topic_id: 17889 reply_id: 317889[/import]

Hey there - how many blocks do you have? :slight_smile: [import]uid: 52491 topic_id: 17889 reply_id: 68330[/import]

hi peach, i have 3 blocks, some time ago, when i use the code of drag function equal to all of block of the block table the drag event listener works great to add the event and remove, now the eventlisteners have a lot of errors and works bad.

and this collision function works great when i use block1[#block1], but with the new code “block1[1]” it dont work

[code]

function spawner:collision (event)
if event.phase == “began” then
for i = 1, #block1, 1 do
if block1[i].id == event.other.id then
block1[i]:removeSelf()
table.remove(block1,i)

return true
end
end
end
end
[import]uid: 26056 topic_id: 17889 reply_id: 68347[/import]

Hey again,

Take a look at this code, it might help get you back on track - it’s just a simple stand alone demo showing spawning of three blocks and adding an event listener;

[lua]block1 = {}

local left = 50
local top = 50

local function drag ()
–do stuff here
end

for i = 1, 3 do
block1[i] = display.newRect( left, top, 50, 50 )
block1[i]:addEventListener(“touch”, drag)
left, top = left+50, top+50
end[/lua] [import]uid: 52491 topic_id: 17889 reply_id: 68350[/import]

Updated [import]uid: 26056 topic_id: 17889 reply_id: 68650[/import]

Thanks peach i will try it later [import]uid: 26056 topic_id: 17889 reply_id: 68353[/import]