Nil error from Runtime:EventListener after removing object

Hi

I call a function move(event) from Runtime:addEventListener( “enterFrame”, move );

All works well until I remove the object “plant1” as a result of a collision, then I get  nil error from the move function, even though I am checking if plant1 != nil ?

error : Attempt to perform arithmetic on field ‘x’ (a nil value)

collision event.

plant1[event.target] = nil 

event.target:removeSelf()  

move function

local function move(event)

local tDelta = event.time - tPrevious

tPrevious = event.time

if plant1 ~= null then

  plant1.x = plant1.x - xOffset

end

end

if plant1 ~= nil then

old habits, still errors

if plant1 ~= nil

attempt to perform arithmetic on field ‘x’ (a nil value)

stack traceback:

Can you copy/paste your code.  Make sure to put it inside and (take out the spaces).

Also post your full stack trace.  It wasn’t listed above.

Rob

local function myCollisionPlant(self, event)  if  event.phase == "began"  then   if event.target.type ==  "badPlant" and event.other.type == "bird" then  lifeCount = lifeCount - 1  displayLifeCount.text = "Lifes: " .. lifeCount  audio.play(losePoint, {channel = audio.findFreeChannel()});  event.target:removeSelf()    plant1[event.target] = nil         -- We remove object from table  display.getCurrentStage():setFocus(nil) end  elseif (event.phase == "ended") then end end   function kick1(event) ----- plant   plant1 = display.newSprite(sheet2, { name="cat4", start=1, count=22, time=2000} )  plant1.xScale = -1;   plant1.y = baseline -25  plant1.x = \_W+50--\_W+150; plant1.y = baseline -2  physics.addBody(plant1, "kinematic",{density=0, friction=0, bounce=0, radius=40})   plant1.type = "badPlant"  plant1:play()   plant1.collision = myCollisionPlant  plant1:addEventListener("collision", plant1)  plant1.isSensor = true end   function scene:createScene( event     group = scene.view;     bird = display.newImage(group,"bird1.png",10)     bird.x = 200     bird.y =  centerY/3   --random(\_H2) -500 --\* .5     bird.type = "bird"    physics.addBody( bird ,"dynamic", { density=1.0, friction=1, bounce=0, radius=20})     bird.isFixedRotation=true end   function scene:enterScene( event )  group = self.view  timer1 = timer.performWithDelay( math.random(5000,6000), kick1,-1) --plant end   tPrevious = system.getTimer() local function move(event) local tDelta = event.time - tPrevious tPrevious = event.time  local xOffset = ( 0.2 \* tDelta ) if plant1 ~= nil then  plant1.x = plant1.x - xOffset end   end   -- Start everything moving Runtime:addEventListener( "enterFrame", move );

Try changing
plant1[event.target] = nil
to
event.target = nil

The comment says “we remove object from table” but as far as I can tell plant1 is a display object name not a table name.

As a side note, every time your timer runs it overwrites the previous plant1. If you are not destroying plant1 before the next one is generated, you’re creating a memory leak.

if plant1 ~= nil then

old habits, still errors

if plant1 ~= nil

attempt to perform arithmetic on field ‘x’ (a nil value)

stack traceback:

Can you copy/paste your code.  Make sure to put it inside and (take out the spaces).

Also post your full stack trace.  It wasn’t listed above.

Rob

local function myCollisionPlant(self, event)  if  event.phase == "began"  then   if event.target.type ==  "badPlant" and event.other.type == "bird" then  lifeCount = lifeCount - 1  displayLifeCount.text = "Lifes: " .. lifeCount  audio.play(losePoint, {channel = audio.findFreeChannel()});  event.target:removeSelf()    plant1[event.target] = nil         -- We remove object from table  display.getCurrentStage():setFocus(nil) end  elseif (event.phase == "ended") then end end   function kick1(event) ----- plant   plant1 = display.newSprite(sheet2, { name="cat4", start=1, count=22, time=2000} )  plant1.xScale = -1;   plant1.y = baseline -25  plant1.x = \_W+50--\_W+150; plant1.y = baseline -2  physics.addBody(plant1, "kinematic",{density=0, friction=0, bounce=0, radius=40})   plant1.type = "badPlant"  plant1:play()   plant1.collision = myCollisionPlant  plant1:addEventListener("collision", plant1)  plant1.isSensor = true end   function scene:createScene( event     group = scene.view;     bird = display.newImage(group,"bird1.png",10)     bird.x = 200     bird.y =  centerY/3   --random(\_H2) -500 --\* .5     bird.type = "bird"    physics.addBody( bird ,"dynamic", { density=1.0, friction=1, bounce=0, radius=20})     bird.isFixedRotation=true end   function scene:enterScene( event )  group = self.view  timer1 = timer.performWithDelay( math.random(5000,6000), kick1,-1) --plant end   tPrevious = system.getTimer() local function move(event) local tDelta = event.time - tPrevious tPrevious = event.time  local xOffset = ( 0.2 \* tDelta ) if plant1 ~= nil then  plant1.x = plant1.x - xOffset end   end   -- Start everything moving Runtime:addEventListener( "enterFrame", move );

Try changing
plant1[event.target] = nil
to
event.target = nil

The comment says “we remove object from table” but as far as I can tell plant1 is a display object name not a table name.

As a side note, every time your timer runs it overwrites the previous plant1. If you are not destroying plant1 before the next one is generated, you’re creating a memory leak.