I have an issue with my game with trying to remove particles. It works at one function, but fails in the collision function.
Here is the code for the function of making particles:
[lua]
local function particleTrail(xLoc, yLoc)
local s = 1
for i = 1, math.random(2,5) do
local particle = display.newImageRect(“Spaceship” … (rfn) … “Particle.png”, 18, 18)
particle.x = xLoc
particle.y = yLoc
particle.alpha = 1
transition.to(particle, {time = 1500, x = math.random(230, 270), y = math.random(330, 350), alpha = 0, onComplete = function (target) display.remove(target) end} )
end
return particle
end
local function particleTrail2(xLoc, yLoc)
local s = 1
if (bluespaceship ~= nil) then
for i = 1, math.random(2,5) do
local particle = display.newImageRect(“Spaceship” … (rfn) … “Particle.png”, 18, 18)
particle.x = xLoc
particle.y = yLoc
particle.alpha = 1
transition.to(particle, {time = 1500, x = math.random(230, 270), y = math.random(-30, -10), alpha = 0, onComplete = function (target) display.remove(target) end} )
end
end
return particle
end
local function addTrail(event)
if (bluespaceship ~= nil) then
if bluespaceship.type == “bottomVertical” then
particleTrail(bluespaceship.x, bluespaceship.y)
ptmr.status = “running”
end
if bluespaceship.type == “topVertical” then
particleTrail2(bluespaceship.x, bluespaceship.y)
ptmr.status = “running”
end
end
end
ptmr = timer.performWithDelay(200, addTrail, -1)
[/lua]
Basically, there are two functions for creating particles and based on the preconditions, I call either one.
This is the code for the function where it is removed with no problem:
[lua]
local function removeSP()
display.remove(bluespaceship)
bluespaceship = nil
end
local function sendToPlanet()
removeSP()
if (rfn == 1) then
particleExplosion(quadrant1.x, quadrant1.y)
addScore(quadrant1.x, quadrant1.y)
end
if (rfn == 3) then
particleExplosion(quadrant2.x, quadrant2.y)
addScore3(quadrant2.x, quadrant2.y)
end
if (rfn == 7) then
particleExplosion(quadrant9.x, quadrant9.y)
addScore4(quadrant9.x, quadrant9.y)
end
end
[/lua]
This is the code of the collision function where it does not work. Instead, the particle, instead of emitting from the spaceship (which I removed), starts coming from the coordinates (0,0) or the top left of the screen.
[lua]
local function escapeSensorCollision(self,event)
if (event.phase == “began”) then
spaceshipLives = spaceshipLives - 1
print(“Collision Received!”)
if (event.other.name == “bluespaceship”) then
display.remove(bluespaceship)
bluespaceship = nil
end
if (event.other.name == “redspaceship”) then
display.remove(redspaceship)
redspaceship = nil
end
end
end
escapesensor.collision = escapeSensorCollision
escapesensor:addEventListener(“collision”, escapesensor)
escapesensor2.collision = escapeSensorCollision
escapesensor2:addEventListener(“collision”, escapesensor2)
[/lua]
Could someone tell me why this is occurring and a possible solution to this problem? Thanks so much!