Here is my sample code in a simple version. Don’t forget to change the image file names at the top. The code works as long as you uncomment the last line.
I know there are maybe unneeded loops in my listener function. I just wanted to prevent some errors.
local physics = require "physics" physics.start() --------------------------------------- -- IMPORTANT: Set your image names here --------------------------------------- local particleImageName = "water-particle-4.png" local crateImageName = "crate.png" -- A table containing functions etc. Makes it easier to call them from anywhere. local t = {} -- Basic particle system setup -- local myParticleSystem = physics.newParticleSystem( { filename = particleImageName, radius = 8, imageRadius = 16 } ) -- Spawning a block of water particles -- local waterblock = myParticleSystem:createGroup( { flags = { "water", "fixtureContactListener" }, x = 300, y = 100, color = { 0, 0.5, 1, 1 }, halfWidth = 200, halfHeight = 400 } ) -- Listener function for the crates -- t.objectParticleCollisionListener = function ( event ) -- Make sure the object is still there. if ( event.object ~= nil ) then -- Store an object reference in a local variable. local target = event.object if ( event.phase == "began" ) then -- Just check if the object has a health pool at all. if target.healthPool then -- Substract 1 health for every collision as long as the health pool is bigger than 0. if target.healthPool \> 0 then target.healthPool = target.healthPool - 1 end -- The health pool is now 0 if target.healthPool == 0 then print( target.name .. " health is 0." ) -- Check if the object is already flagged for removal. if target.garbage ~= true then print( target.name .. " will be removed now." ) -- Remove the event listener from the object first, to prevent future collision events. target:removeEventListener( "particleCollision", t.objectParticleCollisionListener ) -- Flag the object for removal to prevent the function from trying to remove it more than once. target.garbage = true -- Finally remove the object from the scene. display.remove(target) -- Set the object to nil, just to be sure there is nothing left. target = nil end end end end end -- Return true in every case to complete the collision event return true end -- Some params for the crates -- local crateParams = { image = crateImageName, width = 50, height = 50, density = 5.0, friction = 0.3, bounce = 0.2 } -- Spawn crate 1 -- local crate = display.newImageRect( crateParams.image, crateParams.width, crateParams.height ) crate.x, crate.y = 300, 600 physics.addBody( crate, { density = crateParams.density, friction = crateParams.friction, bounce = crateParams.bounce } ) -- Setting up a health pool which can count down in the listener function. crate.healthPool = 500 -- Just a boolean value to see if the crate is already marked for removal. crate.garbage = false crate.bodyType = "static" crate.name = "Crate 01" crate:addEventListener( "particleCollision", t.objectParticleCollisionListener ) -- Spawn crate 2 -- local crate2 = display.newImageRect( crateParams.image, crateParams.width, crateParams.height ) crate2.x, crate2.y = 360, 800 physics.addBody( crate2, { density = crateParams.density, friction = crateParams.friction, bounce = crateParams.bounce } ) crate2.healthPool = 500 crate2.garbage = false crate2.bodyType = "static" crate2.name = "Crate 02" -- IMPORTANT -- If you uncomment the last line, the second crate receives its event listener. -- The app will crash, at least in my tests. --crate2:addEventListener( "particleCollision", t.objectParticleCollisionListener )