How do you reference an interaction between two objects? For example if a character and a zombie collide with each other, what would I need to write so that the zombie itself disappears on collision? Thank you for your help.
first thing you need to do is assigning names to the objects and then do the following
[lua]
function onCollision_zombi(e)
if e.phase == “began” then
if (e.other.name == “character”) then
if e.target ~= nil then
e.target:removeSelf()
end
end
end
end
[/lua]
with this code the zombie will disappear only if it collides with the character.
Moha.
Thank you so much for responding…
I wrote this now: function onCollision( event )
–print(“collide!”)
happy:removeSelf()
happy = nil
end
Runtime:addEventListener(“collision”, onCollision)
if happy == nil
then
display.newText( “You lose” )
end
Now when I run it, it gives me an error. error: "Attempt to index upvalue ‘happy’(a nil value).
I’m trying to make it that when the zombie collides with character (happy), happy disappears (which is not that different from what I was trying to do before.) Also the collisions are REALLY buggy, and sometimes the zombie is a good few inches away from the character but the character disappears anyway… How do I solve this? Also I want to make sure when the character disappears, the screen transitions to a new screen that shows a game over button… I know this might be a lot but thanks for responding!!
Try using the timer.performWithDelay().
I think you cant or shouldn’t really cant remove an object at the time of onCollision you may need to set a simple delay to get around the issue.
Try this
timer.performWithDelay( 1, function() timer.performWithDelay( 1, function() happy:removeSelf(); end ) or based on hammod-930 example above if e.target ~= nil then timer.performWithDelay( 1, function() e.target:removeSelf(); end ) end
Good Luck,
Larry
first thing you need to do is assigning names to the objects and then do the following
[lua]
function onCollision_zombi(e)
if e.phase == “began” then
if (e.other.name == “character”) then
if e.target ~= nil then
e.target:removeSelf()
end
end
end
end
[/lua]
with this code the zombie will disappear only if it collides with the character.
Moha.
Thank you so much for responding…
I wrote this now: function onCollision( event )
–print(“collide!”)
happy:removeSelf()
happy = nil
end
Runtime:addEventListener(“collision”, onCollision)
if happy == nil
then
display.newText( “You lose” )
end
Now when I run it, it gives me an error. error: "Attempt to index upvalue ‘happy’(a nil value).
I’m trying to make it that when the zombie collides with character (happy), happy disappears (which is not that different from what I was trying to do before.) Also the collisions are REALLY buggy, and sometimes the zombie is a good few inches away from the character but the character disappears anyway… How do I solve this? Also I want to make sure when the character disappears, the screen transitions to a new screen that shows a game over button… I know this might be a lot but thanks for responding!!
Try using the timer.performWithDelay().
I think you cant or shouldn’t really cant remove an object at the time of onCollision you may need to set a simple delay to get around the issue.
Try this
timer.performWithDelay( 1, function() timer.performWithDelay( 1, function() happy:removeSelf(); end ) or based on hammod-930 example above if e.target ~= nil then timer.performWithDelay( 1, function() e.target:removeSelf(); end ) end
Good Luck,
Larry