why does this only remove one bomb not both ?

Ok so when I call cancelBomb() wht does it only delete one bomb please?

if I just spawn one bomb it works fine.

[lua]

createBomb = function()

  local bomb = display.newRect(0, 0, 2, 10)
  bomb.x = enemyYellow[1].x
  bomb.y = enemyYellow[1].y

  local params = {isBullet = true,isSensor = true, friction = 0,bounce = 0, }
  bomb.isBodyActive = true
  bomb.myName = ‘bomb’
  physics.addBody(bomb, params)
  bomb:addEventListener(“collision”, bomb)
  bomb:applyForce(0, 0.005, bomb.x, bomb.y)

  cancelBomb = function()
      if bombSpawnTimer then timer.cancel(bombSpawnTimer) bombSpawnTimer = nil end
      bomb:removeSelf()
      bomb = nil
  end
end

spawnBomb = function()
    if enemyActive then
        bombSpawnTimer = timer.performWithDelay(1000,createBomb,2)
    else

    end
end

spawnBomb()

[/lua]

I believe it’s because you are creating bomb twice.  The second creation of the bomb is overwriting the first and therefore there is no more reference to the first.

You should put bomb in a table so that the first bomb doesn’t get lost.

You should use return bomb at the end of your createBomb function and store that returned bomb into a table you can later access.  That way you can have multiple instances of this bomb that should all behave as expected.

I believe it’s because you are creating bomb twice.  The second creation of the bomb is overwriting the first and therefore there is no more reference to the first.

You should put bomb in a table so that the first bomb doesn’t get lost.

You should use return bomb at the end of your createBomb function and store that returned bomb into a table you can later access.  That way you can have multiple instances of this bomb that should all behave as expected.