Display object removal

Hi

I’ve been making a card game but i ran into issues with removing display objects.
I’ve made a hole in the sprite of the cards to see the background to check that the display objected being removed its actually gone but its still there. I dont really understand what i’m doing wrong.
Here is the code for the creation of the cards and its removal:

function drawEntities()
  for key, value in pairs(cells) do
    local image
    local entity = value[1]
    local num = value[2]
    local x = value[3]
    local y = value[4]
    if entity == 'enemy' then
      drawBlank('enemy',x,y,key,background)     
      local image = display.newImage(spritesheet,num)
      image:scale( imgscale, imgscale )
      image.anchorX = 0
      image.anchorY = 0
      image.x = x
      image.y = y + math.floor(RECT_H / 4)
      cells[key]['sprite'] = image
    elseif entity == 'player' then
      drawBlank('player',x,y,key,background)
      local image = display.newImage(playersprite,num)
      image:scale( imgscale, imgscale )
      image.anchorX = 0
      image.anchorY = 0
      image.x = x
      image.y = y + math.floor(RECT_H / 4)
      cells[key]['sprite'] = image
    end
  end
end

function attack(cell,playerPos)
  display.remove(cells[playerPos]['blank'])
  display.remove(cells[playerPos]['sprite'])
  display.remove(cells[cell]['blank'])
  display.remove(cells[cell]['sprite'])
  local x1 = cells[cell][3]
  local y1 = cells[cell][4]
  local x2 = cells[playerPos][3]
  local y2 = cells[playerPos][4]
  local adjacent = neighbours[cell]
  emptyPlaces[playerPos] = {x2,y2}
  cells[cell] = {'player',1,x1,y1}
  cells[cell]['enemies'] = adjacent
  currPlayerpos = cell
end

Well, just based on that, I’d wager you are not using correct indices.

The display.remove() call will first check if whatever is being removed exists and then try to remove it, so if you’re trying to remove something that doesn’t exist, your game won’t crash, but you won’t receive any warnings either.

I would debug this by adding some property to the tables and seeing if they exist in the table entries you’re trying to remove, like the entity type. Then, before you try removing them, just print out the target object’s x, y, entity type, etc. and see if it has all of the correct information.

Also, from the looks of things, you could reduce your code quite a bit by doing:

function drawEntities()
  for key, value in pairs(cells) do
    local image
    local entity = value[1]
    local num = value[2]
    local x = value[3]
    local y = value[4]

    drawBlank(entity,x,y,key,background)
    local image = display.newImage(playersprite,num)
    image:scale( imgscale, imgscale )
    image.anchorX = 0
    image.anchorY = 0
    image.x = x
    image.y = y + math.floor(RECT_H / 4)
    cells[key]['sprite'] = image
  end
end

Thanks it was a dumb mistake but i was able to solve it by checking the number of objects on screen.