attempt to perform arithmetic

Hello,
I added if what checks if the zombie isn’t nil, but it still give error after zombie gets deleted.

if(zombie) then
  zombie.x = zombie.x + 2
end

And this is function where zombie is deleted

if zombie.hp <= 0 then
  Runtime:removeEventListener("enterFrame", enemy)
  timer.performWithDelay(1, function()
    zombie:removeSelf()
  end)
end

That means that that zombie is not nil, but zombie.x is

When you have a display object, you can check if it was deleted this way:

if zombie.removeSelf then
  zombie.x = zombie.x + 2
end

Why are you removing the zombie after 1 ms?

I removing it after 1ms because this “if” is in collision function

And within the removeEventListener, shouldn’t it say zombie instead of enemy?

Because what I imagine is that:

if(zombie) then
  zombie.x = zombie.x + 2
end

Is inside the enterFrame callback, right? and it is operating over a removed zombie object. When you call removeSelf() the object does not become nil, but some of it’s entries do, (like removeSelf and x among others)

No, enemy is another function what moves zombie too, but I need to keep that function where

if(zombie) then
  zombie.x = zombie.x +2
end

is .

And replacing the if(zombie) then with -> if zombie.removeSelf then worked?

Yeah, it worked!

Thanks :slight_smile:

And if I replace
zombie.removeSelf() to display.remove( zombie )
what I need to write instead zombie.removeSelf what is in if ?

The if still the same.

Both ways do the exact same thing whit the only difference that display.remove(zombie) won’t trigger any error if zombie is nil

Actually, you should probably set

zombie = nil

after you removed it. Reason for doing it this way is that you will release it from memory as well (if there are no other references to this object). This is correct way to remove objects. Unless you have some reason to keep zombie in memory afret you removed it I suggest you do it this way. You can check it the way you had it before:

if zombie then
    zombie.x = zombie.x + 2
end