Can't figure out how to remove an object

I’ve been playing around with Corna SDK for the last few hours, but ran into a problem.

I can’t figure out how to remove objects from screen.

Here’s how the objects get created

--this gets called every few second function createBlock() --cw is display.ContentWidth, and ch is display.ContentHeight local b = display.newRect(cw, math.random(60 + ch / 6, ch - 60 - ch / 6 ), 40, ch / 3) b.name = 'block' -- Block physics physics.addBody(b, 'kinematic') b.isSensor = true blocks:insert(b) end

I want to remove all the objects before starting a new game, and here’s my attempt

for key, item in pairs (blocks) do item:removeSelf( ) end

I am getting the following error attempt to index local ‘item’ (a userdata value)

What am I missing?
 

Hi @OtterPressure,

In this case, you’re adding “b” (the block) to the “blocks” table (I assume that is instantiated somewhere above), but you’re adding it merely as an index inside the table, not as a key-value pair. Thus, you can’t loop through the table using the pairs() function because Lua won’t know what you’re trying to locate. Instead, loop through it via a simple reverse-loop as follows:

[lua]

for i = #blocks, 1, -1 do

   blocks[i]:removeSelf()

   blocks[i] = nil

end

[/lua]

Hope this helps,

Brent

Hi @OtterPressure,

In this case, you’re adding “b” (the block) to the “blocks” table (I assume that is instantiated somewhere above), but you’re adding it merely as an index inside the table, not as a key-value pair. Thus, you can’t loop through the table using the pairs() function because Lua won’t know what you’re trying to locate. Instead, loop through it via a simple reverse-loop as follows:

[lua]

for i = #blocks, 1, -1 do

   blocks[i]:removeSelf()

   blocks[i] = nil

end

[/lua]

Hope this helps,

Brent