[Resolved] Trying to do simple on-collision object removal demo

Hi everyone,

I’ve put some Frankencode together and though it’s not outright crashing, it’s not doing what I want for a global collision listener. I was wondering if someone may be able to help. Physics world setup is in the rest of the code but omitted here.

function newCrate()   
 rand = math.random( 100 )  
  
 if (rand \< 60) then  
 j = display.newImage("crate.png");  
 j.x = 60 + math.random( 160 )  
 j.y = -100  
  
 physics.addBody( j, { density=0.9, friction=0.3, bounce=0.3} )  
 transition.to(self, {time=1000, alpha=0, onComplete=removeBall})  
  
  
 elseif (rand \< 80) then  
 j = display.newImage("crateB.png");  
  
 j.x = 60 + math.random( 160 )  
 j.y = -100  
 physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )  
  
 else  
 j = display.newImage("crateC.png");  
  
 j.x = 60 + math.random( 160 )  
 j.y = -100  
 physics.addBody( j, { density=0.3, friction=0.2, bounce=0.5} )  
  
 end   
end  
  
local function onCollision( self, event )  
 if ( event.phase == "began" ) then  
 event.other:removeSelf()  
end  
end  
Runtime:addEventListener( "collision", onCollision )  
  
local dropCrates = timer.performWithDelay( 500, newCrate, 10 )  

Thanks for your help. [import]uid: 144359 topic_id: 27207 reply_id: 327207[/import]

So what did you need help with? [import]uid: 147322 topic_id: 27207 reply_id: 110520[/import]

All set, but thanks for responding. I just needed the syntax for collision handling for mass-created objects. If any other coding newbies would like a look at it, this example mass-generates crates, but only smashes the tiny ones. :slight_smile: (Just go to the Many Crates sample for the image files.)

[code]
local physics = require(“physics”)
physics.start()
glass = audio.loadSound(“glass.mp3”)
display.setStatusBar( display.HiddenStatusBar )

local bkg = display.newImage( “bkg_cor.png” )

local grass = display.newImage(“grass.png”)
grass.x = 160; grass.y = 430
grass.myname=“grass”

local grass2 = display.newImage(“grass2.png”) – non-physical decorative overlay
grass2.x = 160; grass2.y = 440

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

function newCrate()
rand = math.random( 100 )

if (rand < 60) then
j = display.newImage(“crate.png”);
j.x = 60 + math.random( 160 )
j.y = -100

physics.addBody( j, { density=0.9, friction=0.3, bounce=0.3} )
transition.to(self, {time=1000, alpha=0, onComplete=removeBall})

elseif (rand < 80) then
j = display.newImage(“crateB.png”);

j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )

else
j = display.newImage(“crateC.png”);
j.myname=“tinybox”
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=0.3, friction=0.2, bounce=0.5} )

end

j.collision = function(self, event)
if(event.phase == “ended”) then
if event.other.myname==“tinybox” then
goob=audio.play( glass )
event.other:removeSelf()
event.other=nil
end
end
end

j:addEventListener(“collision”, j)
end

local dropCrates = timer.performWithDelay( 1500, newCrate, 10 )
[/code] [import]uid: 144359 topic_id: 27207 reply_id: 110525[/import]

Glad to hear you got it sorted [import]uid: 84637 topic_id: 27207 reply_id: 110704[/import]