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.