destroy on collision

Hi all,

I am having a wee bit of trouble with collision, I have read all the docs and gone through loads of samples but I can not get it to work. All I want to do is destroy an object when it collides with another, 2 days now and a huge headache. Please help. If you have a bit of very simple sample code it would be great, I am trying so hard to learn but have got stuck on this function.

Many thanks in advance
Chris [import]uid: 7388 topic_id: 5090 reply_id: 305090[/import]

just combine the 2 examples here:

http://developer.anscamobile.com/content/game-edition-collision-detection#Local_collision_listeners

http://developer.anscamobile.com/content/game-edition-physics-bodies#Destroying_bodies

[import]uid: 6645 topic_id: 5090 reply_id: 16778[/import]

try this:

[lua]local physics = require( “physics” )
physics.start()

local sky = display.newImage( “bkg_clouds.png” )

local ground = display.newImage( “ground.png” )
ground.x = 160; ground.y = 445

physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )

local crate = display.newImage( “crate.png” )
crate.x = 180; crate.y = -50; crate.rotation = 5

physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } )

local function onLocalCollision( self, event )
if ( event.phase == “began” ) then

self:removeSelf()
return true
end
end

crate.collision = onLocalCollision
crate:addEventListener( “collision”, crate )[/lua] [import]uid: 6645 topic_id: 5090 reply_id: 16821[/import]

Thanks jmp909, got it working sort of. The problem turned out to be that I am trying to destroy objects that I am creating in a function, and it does not work.

So I have another question now,

Is there a way to delay the start of physics in a particular object? Or is there a way to destroy an object that has been created in an object?

Thanks in advance [import]uid: 7388 topic_id: 5090 reply_id: 16822[/import]

Hi…just what I was working on too…new to Corona

I did get that to work if it collided with the ground…what would I need to add or replace if I wanted it be destroyed if it hit a specific object? [import]uid: 12894 topic_id: 5090 reply_id: 16823[/import]

chrisal,

sounds like you have a variable scope issue.

[lua]function whatever()
local hello=“hello”
end

print(hello) – nil because hello is local to the “whatever” function[/lua]

however, like i said, the collision function should contain a reference to the object (self)

post your code

j [import]uid: 6645 topic_id: 5090 reply_id: 16827[/import]

ok so here is the code, i am trying to to have things drop down from the top of the screen and am using gravity. i create the objects in the functions, and then destroy them on contact with another object

function cherry()  
 local randomPosition = 100 + math.random(200)  
 cherryPosition = display.newImage("images/fruit/cherries3d.png")  
 cherryPosition.x = randomPosition  
 cherryPosition.y = -10  
 physics.addBody(cherryPosition)  
  
end   
 timer.performWithDelay(18000, cherry, 1)  
-------------------------------------------------------------  
-------------------------------------------------------------  
local function onLocalCollision( self, event )  
 if ( event.phase == "began" ) then  
 cherryPosition:removeSelf()  
 cherryPosition = nil  
 end   
end  
   
cherryPosition.collision = onLocalCollision  
cherryPosition:addEventListener( "collision", cherryPosition )  

Any thoughts would be great or any other ways to achieve this

Thanks
Chris [import]uid: 7388 topic_id: 5090 reply_id: 16832[/import]

does that not work? looking at it i thought it would. what errors/behaviour are you seeing? i would still use self:removeSelf() personally. I think that’s right but i don’t have corona on hand to check.

another thing to note, you’re using a non-local variable for cherryPosition so you’re going to have trouble if you create more than one of them, as it can’t refer to two objects at the same time

for spawning cherries i would do something like:

[lua]local cherries = {}

function newCherry()

local cherry=display.newImage(“cherry.png”)
– etc
– etc
return cherry

end

– add a new cherry to our array
cherries[#cherries+1] = newCherry()[/lua]

that way you’ve got a table (array) of all cherries currently in play

[import]uid: 6645 topic_id: 5090 reply_id: 16839[/import]

oh one issue i’ve noticed

at the point where you’ve defined this…

[lua]cherryPosition.collision = onLocalCollision[/lua]

… cherryPosition doesn’t actually exist yet so that assignment will fail… you’re not creating cherryPosition until you’ve run the timer event after 18 seconds.

try adding [lua]local cherryPosition[/lua] above [lua]function cherry()[/lua] [import]uid: 6645 topic_id: 5090 reply_id: 16840[/import]

How did you get it to work when it collided with the ground?
I have been trying to get that to work for me.

Thanks in advance. [import]uid: 25129 topic_id: 5090 reply_id: 18770[/import]

I’m having a similar issue except mine’s a sprite. How would you remove a sprite after collision?

I’m new to lua and programming so these codes are not perfect or maybe not even correct at all. Please help if anyone can.

Many Thanks in advance.

[lua]spriteObject = sprite.newSpriteSheetFromData ( “spriteObject.png”, require(“spriteObject”).getSpriteSheetData() )
spriteObjectSet = sprite.newSpriteSet( spriteObject, 1, 4 )
sprite.add( spriteObjectSet, “spriteObject”, 1, 4, 1000, 0 )

local createSprite = function ()
spriteObjectInstance = sprite.newSprite(spriteObjectSet)
spriteObjectInstance.x = 550
spriteObjectInstance.y = mRand(50, 120)
spriteObjectInstance.dx = 2
spriteObjectInstance.dy = mRand( -1, 1)
spriteObjectInstance.myName = “spriteObjectInstance”

physics.addBody(spriteObjectInstance, “static”, { density = 0, friction = 0, bounce = 0 })

spriteObjectInstance.collision = onBulletCollison
spriteObjectInstance:addEventListener (“collision”, spriteObjectInstance)

levelGroup:insert(spriteObjectInstance)
gameGroup:insert(levelGroup)

spriteObjectInstance:prepare(“spriteObject”)
spriteObjectInstance:play()

local destroysprite = function ()
spriteObjectInstance.isVisible = false
spriteObjectInstance:removeSelf()
spriteObjectSet:dispose()
levelGroup:remove(spriteObjectInstance)
end

if spriteObjectInstance.x < 50 then
transition.cancel(movesprite)
transition.to(destroysprite)
Runtime:removeEventListener(“enterFrame”, movesprite)
end

local onBulletCollision = function( self, event )
if event.phase == “began” and event.other.myName ==“bullet” then
spriteObjectInstance.isVisible = false

spriteObjectInstance:removeSelf()
spriteObjectSet:dispose()
levelGroup:remove(spriteObjectInstance)

Runtime:removeEventListener(“enterFrame”, movesprite)

end
end
local movesprite = function ()
spriteObjectInstance.x = spriteObjectInstance.x - spriteObjectInstance.dx
end
Runtime:addEventListener(“enterFrame”, movesprite)

end
[import]uid: 12455 topic_id: 5090 reply_id: 18778[/import]

hi yellowbanner,

from my experience (thats not saying lots) you can not destroy an object in a collision if you have created it in a function.

My solution was to create them as you would a normal image or sprite, and them bring them onto the screen with gravity, or transition.to.

If anybody knows how to get it to work with objects created in a function it would be great

Chris [import]uid: 7388 topic_id: 5090 reply_id: 18781[/import]