I’ve scanned through all the threads I can on this and I’m still confused. It seems that you can’t change physics properties of an object during a collision.
If I set isBodyActive to false on my “block” during a collision, I’d expect my ball to pass straight through. However it seems to take a few seconds to respond.
If I do display.remove(block) then it works as expected, but now I have a problem since I’m doing object pooling and can’t get my block back into my scene.
What is the difference between removing an object using sceneGroup:remove vs display.remove?
I can’t seem to get my code to indent here =/ sorry
I create my blocks during scene creation
[lua]
local color = ‘red’
for i = 1, 10 do
local block = display.newRect( 0, 0, 60, 2 )
block.index = i
block.name = ‘block’
– block.isVisible = false
block.isAlive = false
physics.addBody( block, ‘static’ )
block.collision = onCollision
block:addEventListener( ‘collision’, block )
blocks[i] = block
sceneGroup:insert( block )
block.color = color
end
[/lua]
block removal during enter frame
[lua]
for k, block in pairs(blocks) do
if block.isAlive then
block.y = block.y - 1
if block.y < 0 then
removeBlock(block)
end
end
end
[/lua]
add block function
[lua]
function addBlock()
for k, block in pairs(blocks) do
if block.isAlive ~= true then
block.contentWidth = 60
block.contentHeight = 2
block.isAlive = true
block.x = math.random(
block.contentWidth/2 + 20,
display.contentWidth - block.contentWidth/2 - 20
)
block.y = display.contentHeight
break
end
end
end
[/lua]
removeblock function, being called during collision as well
[lua]
function removeBlock(block)
display.remove( block )
block.isAlive = false
block.isBodyActive = false
end
[/lua]
Removing the block using display.remove works instantly, but then I can’t find a way to add it back inside of addBlock… once it’s gone it seems like it’s really gone. I’d figure just setting isBodyActive to false would be enough to turn off the physics object, but it doesnt happen instantly the way I’d like.