Problem : Sprite still shows in the list when I do a #count on an array even after I do a :remove()
Description : I have an array call rocks. I initialize it empty at first with rocks = {}. Then add some sprites to it. Later I cycle through them and remove them by doing rock[i]:remove(). It removes from the screen just fine, but when I do #rocks to get a count it still has the same amount...
I was expecting the table count to go down as I remove them. What am I missing?
Here is a little demo to showcase what I'm talking about.
main.lua
import "coreLibs/object"
import "coreLibs/graphics"
import "coreLibs/sprites"
import "coreLibs/timer"
import "Rock"--my rock class
local pd <const> = playdate
local gfx <const> = pd.graphics
local rocks = {}
function addRocks(amount)
local buffer = 16
local locx = 164
local locy = 140
if amount > 0 then
for i = 0 , amount do
rocks[i] = Rock(locx,locy)
rocks[i]:add()
if locx > 383 then
locx = 164
locy+=16
else
locx += buffer
end
end
end
end
local function initalize()
addRocks(105) -- Adding 105 rocks here
end
initalize()
function playdate.update()
if pd.buttonIsPressed(pd.kButtonA) then
print("rocks",#rocks)-- prints count from rocks
for i = 0 , #rocks do -- removes all rocks from screen
rocks[i]:remove()
end
end
gfx.sprite.update()
pd.timer.updateTimers()
end
Shows all rocks
Pressed A to remove all rocks. Console still shows a count of 105 O.....o
I pressed the button a lot to really show the count not changing.