removeSelf() with timer.performWithDelay.

Hi, having trouble getting this right,

function onCollision(event)
if(event.object1.myName == “player” and event.object2.myName == “tree”) then
event.object2:removeSelf()
end
end

Runtime:addEventListener( “collision”, onCollision )

I want this collision to perform with a timed delay, have tried placing timer.performWithDelay but with no success.

Collision works perfect with removing the object but I want the object to be removed after a certain time and want the collision to trigger once.

Tree is set to isSensor = true.
[import]uid: 165082 topic_id: 33721 reply_id: 333721[/import]

try:

function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "tree") then target = event.object2 timer.performWithDelay( 1000, function() target:removeSelf() target = nil end, 1 ) end end [import]uid: 77199 topic_id: 33721 reply_id: 134073[/import]

Ok I figured this out, where I don’t have to remove the object after colliding.

Only problem I am having now is that its registering multiple collisions and not just one as intended, when my player collides with the tree my removeLife function is not - 1 life but multiple lives.

local tree = display.newImageRect(“images/tree.png”, 195, 190)
tree.x = 462
tree.y = display.contentHeight - 100
tree.myName = “tree”
tree:addEventListener(“collision”, removeLife)
physics.addBody(tree, “static”, physicsData:get(“tree”))
function tree:preCollision( event )
local allCollisions = event.other
if allCollisions.myName == “player” then
event.contact.isEnabled = false
print( “preCollision”, allCollisions.myName )
end
end
tree:addEventListener( “preCollision” ) [import]uid: 165082 topic_id: 33721 reply_id: 134074[/import]

Hey hatethinkingofnames,

Tried your example but still the same.

Thanks. [import]uid: 165082 topic_id: 33721 reply_id: 134076[/import]

If you are removing the tree after a delay, then it’s removing life constantly because the tree still exists and the collision is still going through. You have to delete the tree instantly for the collision to stop so that you aren’t losing life. Unless you stop the removeLife function from happening multiple times from the same tree [import]uid: 77199 topic_id: 33721 reply_id: 134077[/import]

My suggestion would be to add a variable upon creation of the objects and set it to false. In your collision function change this variable to true on the object to be removed. Then check the variable in whatever function is removing the life. If the variable if false, subtract a life. If true dont subtract a life because the object has collided and is set for removal. This should stop the multiple subtracting.

Hope that makes sense without a code example. [import]uid: 56820 topic_id: 33721 reply_id: 134083[/import]

Will give it a try.

Makes sense, thanks. [import]uid: 165082 topic_id: 33721 reply_id: 134084[/import]

Hmm, I should of explained my idea clearer.

When I say set a variable I was meaning attach it to the object so each object has it’s own defined state.

[code]
local object = someNewPhysicsObject
object.bRemove = false

[code] [import]uid: 56820 topic_id: 33721 reply_id: 134088[/import]

His idea is better:

function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "tree" and event.object2.bRemove == false) then target = event.object2 event.object2.bRemove == true timer.performWithDelay( 1000, function() target:removeSelf() target = nil end, 1 ) -- might want to add the remove life here and whatever else happens when you collide with a tree end end [import]uid: 77199 topic_id: 33721 reply_id: 134087[/import]

try:

function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "tree") then target = event.object2 timer.performWithDelay( 1000, function() target:removeSelf() target = nil end, 1 ) end end [import]uid: 77199 topic_id: 33721 reply_id: 134073[/import]

Ok I figured this out, where I don’t have to remove the object after colliding.

Only problem I am having now is that its registering multiple collisions and not just one as intended, when my player collides with the tree my removeLife function is not - 1 life but multiple lives.

local tree = display.newImageRect(“images/tree.png”, 195, 190)
tree.x = 462
tree.y = display.contentHeight - 100
tree.myName = “tree”
tree:addEventListener(“collision”, removeLife)
physics.addBody(tree, “static”, physicsData:get(“tree”))
function tree:preCollision( event )
local allCollisions = event.other
if allCollisions.myName == “player” then
event.contact.isEnabled = false
print( “preCollision”, allCollisions.myName )
end
end
tree:addEventListener( “preCollision” ) [import]uid: 165082 topic_id: 33721 reply_id: 134074[/import]

Hey hatethinkingofnames,

Tried your example but still the same.

Thanks. [import]uid: 165082 topic_id: 33721 reply_id: 134076[/import]

If you are removing the tree after a delay, then it’s removing life constantly because the tree still exists and the collision is still going through. You have to delete the tree instantly for the collision to stop so that you aren’t losing life. Unless you stop the removeLife function from happening multiple times from the same tree [import]uid: 77199 topic_id: 33721 reply_id: 134077[/import]

My suggestion would be to add a variable upon creation of the objects and set it to false. In your collision function change this variable to true on the object to be removed. Then check the variable in whatever function is removing the life. If the variable if false, subtract a life. If true dont subtract a life because the object has collided and is set for removal. This should stop the multiple subtracting.

Hope that makes sense without a code example. [import]uid: 56820 topic_id: 33721 reply_id: 134083[/import]

Will give it a try.

Makes sense, thanks. [import]uid: 165082 topic_id: 33721 reply_id: 134084[/import]

Hmm, I should of explained my idea clearer.

When I say set a variable I was meaning attach it to the object so each object has it’s own defined state.

[code]
local object = someNewPhysicsObject
object.bRemove = false

[code] [import]uid: 56820 topic_id: 33721 reply_id: 134088[/import]

His idea is better:

function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "tree" and event.object2.bRemove == false) then target = event.object2 event.object2.bRemove == true timer.performWithDelay( 1000, function() target:removeSelf() target = nil end, 1 ) -- might want to add the remove life here and whatever else happens when you collide with a tree end end [import]uid: 77199 topic_id: 33721 reply_id: 134087[/import]