Using tap to clear particles

Ok, so I am trying to create a water game. For the game I have liquid objects falling from the top and collecting on the bottom. So i was trying to implement a method where if the user clicked on the screen, a square of particles around it are removed. For some reason, when I implement this, it rarely works. I have when the method is called, to draw a red rectangle around the area the particles are supposed to be cleared from. When i click on the screen, the red rectangle appears showing that the method was called, but the particles are not cleared. Why won’t this work? 

Main.lua: 

--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) timer.performWithDelay(10000, function() print(water:getParticleNum()) end, 0) local function onTouch(event) water:clearArea(event.x, event.y,32, 32) end Runtime:addEventListener("tap", onTouch)

Water.lua:

By the way the method that is supposed is clearArea(x1, y2, width, height) 

When water:clearArea is called, class:clearArea is called, which calls clearArea()

-- 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 local colorFlag = "random" local limit = 20000 -- 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 getColor() return {math.random(0,105) /255,math.random(200,255) /255, math.random(200,255) /255} end --function to clear the screen of particles local function clearAll() print("clear all") 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 }) print(number\_of\_particles\_destroyed) end --function to clear a specified area of particles local function clearArea(x1, y1, width, height) print("clearing area") local number\_of\_particles\_destroyed number\_of\_particles\_destroyed = particleSystem:destroyParticles({ x = x1, y = x1, halfWidth = width / 2, halfHeight = height /2 }) local rectClear = display.newRect(x1, y1, width , height) rectClear.strokeWidth = 3 rectClear:setFillColor( 0,0,0,0 ) rectClear:setStrokeColor( 1, 0, 0 ) 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 = getColor() -- 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),getColor()) particleSystem:createGroup( param) --check to see if the number of particles exceeds the limit if particleSystem.particleCount \> limit then continue = false end 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() timer.performWithDelay(10, clearAll, 1) end function class:setDifficultyLevel(num) difficultyLevel = num --when the difficulty level is changed the delay and lifetime values are changed too delay = 1200 - (70 \* difficultyLevel) lifetime = 150 + (10 \* difficultyLevel) end --function to return the number of particles in the sytstem function class:getParticleNum() return particleSystem.particleCount end --function to set the limit of particles in system function class:setLimit(num) limit = num end --function to clear a select area of particles function class:clearArea(x, y, width, height) timer.performWithDelay(1, clearArea(x,y,width,height), 1) end return class