Clearing particles from the screen

Ok, so i have a game where water is falling down. I am using the particle system of physics. So far everything has been working out for me, except one thing. I am trying to make a method which clears the screen on all the particles. For some reason it is not working for me. 

--Main class local water = require ("water") --walls local left\_side\_piece = display.newRect( -40, display.contentHeight-220, 400, 70 ) physics.addBody( left\_side\_piece, "static" ) left\_side\_piece.rotation = 80 local center\_piece = display.newRect( display.contentCenterX, display.contentHeight-16, 400, 120 ) physics.addBody( center\_piece, "static" ) local right\_side\_piece = display.newRect( display.contentWidth+40, display.contentHeight-220, 400, 70 ) physics.addBody( right\_side\_piece, "static" ) right\_side\_piece.rotation = -80 water.start() water:setDelay(1100) function onColl(self, event) --print("Collision occured") if event.y \<= 200 and event.y \>0 then print("Clear all") water:clearAll() water:stop() end end water:setEventListener(onColl)

Water.lua: 

-- A class that contains all water physics --Variables local physics = (require"physics") physics.start() local w = display.contentWidth local h = display.contentHeight local particleSystem = physics.newParticleSystem({ filename = "particle.png", colorMixingStrength = 0.3, gravityScale = 1.3, strictContactCheck = true, radius = 3, imageRadius = 6 }) --Variables that set water modes/ class variable local class = {} local continue = false local difficultyLevel = 1 local delay = 1200 local lifetime = 150 -- a array of possible colors with rgba values local colors = { water = {0.5, 0.81,0.95}, clearWater = {(125)/255, (231/255) , (243/255)}, cocoCola = {165/255, 64/255, 33/255} } --images outlines local pentagon\_outline = graphics.newOutline( 1, "Pentagon Outline.png" ) local heart\_outline = graphics.newOutline(1, "Heart Outline.png" ) --All functions --function to get random color local function getRandomColor() return {math.random(0,105) /255,math.random(200,255) /255, math.random(200,255) /255} --return {0.5, 0.81,0.95, 0.32} end --function to return particle group paremter of a drop local function getDropParam(flags , x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, radius = 25, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return particle group parameter of a square local function getSquareParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, halfWidth = 25, halfHeight = 25, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a triangle local function getTriangleParam(flags, x , color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, shape = { 0,0, 32,64, 64,0 }, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a rectangle local function getRectangleParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, halfWidth = 25, halfHeight = 37, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a pentagon local function getPentagonParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, outline = pentagon\_outline, width = 10, height = 10, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a heart local function getHeartParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, outline = heart\_outline, width = 10, height = 10, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end local function onTimer() flags = {"water", "colorMixing", "fixtureContactListener"} paramColor = getRandomColor() -- get a random x value x = math.random(0, display.contentWidth) --Pick a random shape num = math.random() if num \< (1.0 / 6.0) then --drop param = getDropParam(flags, x, paramColor) elseif num \< (2.0 / 6.0) then -- square param = getSquareParam(flags,x, paramColor) elseif num \< (3.0 / 6.0) then -- triangle param = getTriangleParam(flags, x, paramColor) elseif num \< (4.0 / 6.0) then -- rectangle param = getRectangleParam(flags, x, paramColor) elseif num \< (5.0 / 6.0) then -- pentagon param = getPentagonParam(flags, x, paramColor) else -- heart param = getHeartParam(flags, x, paramColor) end --param = getHeartParam({"water", "colorMixing"}, math.random(0, display.contentWidth),getRandomColor()) particleSystem:createGroup( param) end --creates a loop of continously calling onTimer() with a specified delay local function repeater() if continue == true then onTimer() timer.performWithDelay(delay, function() repeater() end, 1) end end --Class Functions --function to start the water stream function class:start() continue = true timer.performWithDelay( 1200, repeater, 1 ) end --fucntion to end the water stream function class:stop() continue = false end --function to change the delay function class:setDelay(num) delay = num end --function to change the lifetime of particles function class:setLifeTime(num) lifetime = num end --function to addEventListener to the particles function class:setEventListener(func) particleSystem.particleCollision = func particleSystem:addEventListener( "particleCollision" ) end -- function to clear all particles on the the screen function class:clearAll() print("Clearing particles") particleSystem:destroyParticles({ x = 0, y = 0, halfWidth = 10000, halfHeight = 100000 }) end return class

I try to clear the particles in the method clearAll()

Everything else works fine. 

Hi @2172580,

Are you sure your class and the particle system are associated properly? Your call to “particleSystem:destroyParticles()” looks correct, so there may be an issue with the class setup… i.e. particleSystem (as the pointer) is not actually referring to your particle system, so the call silently fails. I would suggest you carefully check how you’ve set up your class and ensure that the call is pointing to the correct object.

Best regards,

Brent

I changed my code, to call a local function which would change the local variable. It still won’t work. 

New water.lua:

-- A class that contains all water physics --Variables local physics = (require"physics") physics.start() local w = display.contentWidth local h = display.contentHeight local particleSystem = physics.newParticleSystem({ filename = "particle.png", colorMixingStrength = 0.3, gravityScale = 1.3, strictContactCheck = true, radius = 3, imageRadius = 6 }) --Variables that set water modes/ class variable local class = {} local continue = false local difficultyLevel = 1 local delay = 1200 local lifetime = 150 -- a array of possible colors with rgba values local colors = { water = {0.5, 0.81,0.95}, clearWater = {(125)/255, (231/255) , (243/255)}, cocoCola = {165/255, 64/255, 33/255} } --images outlines local pentagon\_outline = graphics.newOutline( 1, "Pentagon Outline.png" ) local heart\_outline = graphics.newOutline(1, "Heart Outline.png" ) --All functions --function to get random color local function getRandomColor() return {math.random(0,105) /255,math.random(200,255) /255, math.random(200,255) /255} --return {0.5, 0.81,0.95, 0.32} end --function to clear the screen of particles local function clearAll() print("Clearing particles") particleSystem:destroyParticles({ x = 0, y = 0, halfWidth = 10000, halfHeight = 100000 }) end --function to return particle group paremter of a drop local function getDropParam(flags , x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, radius = 25, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return particle group parameter of a square local function getSquareParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, halfWidth = 25, halfHeight = 25, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a triangle local function getTriangleParam(flags, x , color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, shape = { 0,0, 32,64, 64,0 }, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a rectangle local function getRectangleParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, halfWidth = 25, halfHeight = 37, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a pentagon local function getPentagonParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, outline = pentagon\_outline, width = 10, height = 10, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a heart local function getHeartParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, outline = heart\_outline, width = 10, height = 10, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end local function onTimer() flags = {"water", "colorMixing", "fixtureContactListener"} paramColor = getRandomColor() -- get a random x value x = math.random(0, display.contentWidth) --Pick a random shape num = math.random() if num \< (1.0 / 6.0) then --drop param = getDropParam(flags, x, paramColor) elseif num \< (2.0 / 6.0) then -- square param = getSquareParam(flags,x, paramColor) elseif num \< (3.0 / 6.0) then -- triangle param = getTriangleParam(flags, x, paramColor) elseif num \< (4.0 / 6.0) then -- rectangle param = getRectangleParam(flags, x, paramColor) elseif num \< (5.0 / 6.0) then -- pentagon param = getPentagonParam(flags, x, paramColor) else -- heart param = getHeartParam(flags, x, paramColor) end --param = getHeartParam({"water", "colorMixing"}, math.random(0, display.contentWidth),getRandomColor()) particleSystem:createGroup( param) end --creates a loop of continously calling onTimer() with a specified delay local function repeater() if continue == true then onTimer() timer.performWithDelay(delay, function() repeater() end, 1) end end --Class Functions --function to start the water stream function class:start() continue = true timer.performWithDelay( 1200, repeater, 1 ) end --fucntion to end the water stream function class:stop() continue = false end --function to change the delay function class:setDelay(num) delay = num end --function to change the lifetime of particles function class:setLifeTime(num) lifetime = num end --function to addEventListener to the particles function class:setEventListener(func) particleSystem.particleCollision = func particleSystem:addEventListener( "particleCollision" ) end -- function to clear all particles on the the screen function class:clearAll() clearAll() end return class

Hi again,

Well, the destroyParticles() API definitely works, as you can see in our sample project (same as what is bundled in your Corona installation):

https://github.com/coronalabs/samples-coronasdk/tree/master/Physics/LiquidFun-Destroy

So, there’s still some issue with your class and how you’re hooking it up. How do you call the “clearAll” function from outside “water.lua”?

Best regards,

Brent

Thank you for pointing me to the example. I got it to work, it changed my function from: 

local function clearAll() print("Clearing particles") particleSystem:destroyParticles({ x = 0, y = 0, halfWidth = display.contentWidth + 100, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; halfHeight = display.contentHeight + 100 }) end

to: 

local function clearAll() local number\_of\_particles\_destroyed number\_of\_particles\_destroyed = particleSystem:destroyParticles({ x = 0, y = 0, angle = 90, halfWidth = display.contentWidth + 100, halfHeight = display.contentHeight + 100 }) end

Thanks for your help! 

Hi @2172580,

Are you sure your class and the particle system are associated properly? Your call to “particleSystem:destroyParticles()” looks correct, so there may be an issue with the class setup… i.e. particleSystem (as the pointer) is not actually referring to your particle system, so the call silently fails. I would suggest you carefully check how you’ve set up your class and ensure that the call is pointing to the correct object.

Best regards,

Brent

I changed my code, to call a local function which would change the local variable. It still won’t work. 

New water.lua:

-- A class that contains all water physics --Variables local physics = (require"physics") physics.start() local w = display.contentWidth local h = display.contentHeight local particleSystem = physics.newParticleSystem({ filename = "particle.png", colorMixingStrength = 0.3, gravityScale = 1.3, strictContactCheck = true, radius = 3, imageRadius = 6 }) --Variables that set water modes/ class variable local class = {} local continue = false local difficultyLevel = 1 local delay = 1200 local lifetime = 150 -- a array of possible colors with rgba values local colors = { water = {0.5, 0.81,0.95}, clearWater = {(125)/255, (231/255) , (243/255)}, cocoCola = {165/255, 64/255, 33/255} } --images outlines local pentagon\_outline = graphics.newOutline( 1, "Pentagon Outline.png" ) local heart\_outline = graphics.newOutline(1, "Heart Outline.png" ) --All functions --function to get random color local function getRandomColor() return {math.random(0,105) /255,math.random(200,255) /255, math.random(200,255) /255} --return {0.5, 0.81,0.95, 0.32} end --function to clear the screen of particles local function clearAll() print("Clearing particles") particleSystem:destroyParticles({ x = 0, y = 0, halfWidth = 10000, halfHeight = 100000 }) end --function to return particle group paremter of a drop local function getDropParam(flags , x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, radius = 25, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return particle group parameter of a square local function getSquareParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, halfWidth = 25, halfHeight = 25, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a triangle local function getTriangleParam(flags, x , color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, shape = { 0,0, 32,64, 64,0 }, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a rectangle local function getRectangleParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, halfWidth = 25, halfHeight = 37, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a pentagon local function getPentagonParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, outline = pentagon\_outline, width = 10, height = 10, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end --function to return a particle group parameter of a heart local function getHeartParam(flags, x, color) param = { flags = flags, x = x, y = -1 \* math.random( 100, 320), color = color, outline = heart\_outline, width = 10, height = 10, linearVelocityX = 0, linearVelocityY = 10, lifetime = lifetime } return param end local function onTimer() flags = {"water", "colorMixing", "fixtureContactListener"} paramColor = getRandomColor() -- get a random x value x = math.random(0, display.contentWidth) --Pick a random shape num = math.random() if num \< (1.0 / 6.0) then --drop param = getDropParam(flags, x, paramColor) elseif num \< (2.0 / 6.0) then -- square param = getSquareParam(flags,x, paramColor) elseif num \< (3.0 / 6.0) then -- triangle param = getTriangleParam(flags, x, paramColor) elseif num \< (4.0 / 6.0) then -- rectangle param = getRectangleParam(flags, x, paramColor) elseif num \< (5.0 / 6.0) then -- pentagon param = getPentagonParam(flags, x, paramColor) else -- heart param = getHeartParam(flags, x, paramColor) end --param = getHeartParam({"water", "colorMixing"}, math.random(0, display.contentWidth),getRandomColor()) particleSystem:createGroup( param) end --creates a loop of continously calling onTimer() with a specified delay local function repeater() if continue == true then onTimer() timer.performWithDelay(delay, function() repeater() end, 1) end end --Class Functions --function to start the water stream function class:start() continue = true timer.performWithDelay( 1200, repeater, 1 ) end --fucntion to end the water stream function class:stop() continue = false end --function to change the delay function class:setDelay(num) delay = num end --function to change the lifetime of particles function class:setLifeTime(num) lifetime = num end --function to addEventListener to the particles function class:setEventListener(func) particleSystem.particleCollision = func particleSystem:addEventListener( "particleCollision" ) end -- function to clear all particles on the the screen function class:clearAll() clearAll() end return class

Hi again,

Well, the destroyParticles() API definitely works, as you can see in our sample project (same as what is bundled in your Corona installation):

https://github.com/coronalabs/samples-coronasdk/tree/master/Physics/LiquidFun-Destroy

So, there’s still some issue with your class and how you’re hooking it up. How do you call the “clearAll” function from outside “water.lua”?

Best regards,

Brent

Thank you for pointing me to the example. I got it to work, it changed my function from: 

local function clearAll() print("Clearing particles") particleSystem:destroyParticles({ x = 0, y = 0, halfWidth = display.contentWidth + 100, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; halfHeight = display.contentHeight + 100 }) end

to: 

local function clearAll() local number\_of\_particles\_destroyed number\_of\_particles\_destroyed = particleSystem:destroyParticles({ x = 0, y = 0, angle = 90, halfWidth = display.contentWidth + 100, halfHeight = display.contentHeight + 100 }) end

Thanks for your help!