Right, don't remove from within the loop ^....^
I took a different approch! After looking through some similar topics I found an comment from sgeos
I was like O.......O Why didn't I think of that!
So now instead of removing from the list then adding I'm just adding all the sprites up front. It's kind of a waste since not all sprites will be shown at once.
But if figure I get the performance boost by not having to generate new sprites and I don't have to :remove them from screen AND remove them from the array then add them back later so it's a win win in my book.
Here are my results
import "coreLibs/object"
import "coreLibs/graphics"
import "coreLibs/sprites"
import "coreLibs/timer"
import "Rock"
local pd <const> = playdate
local gfx <const> = pd.graphics
local rocks = {}
function addRocks(amount)
local buffer = 16
local locx = 164--164
local locy = 140
if amount > 0 then
for i = 0 , amount do
rocks[i] = Rock(locx,locy)
if i == amount then
rocks[i].isLast = true
end
rocks[i]:add()
if locx > 383 then
locx = 164
locy+=16
else
locx += buffer
end
end
end
end
--local x, y
function getLastPosition()
local x , y = 0,0
for i = 0, #rocks do
if rocks[i].isLast == true then
x , y = rocks[i]:getPosition()
if x >= 383 then
x = 164
y += 16
else
x += 16
end
rocks[i].isLast = false
break
end
end
return x, y
end
local function initalize()
addRocks(250)
end
initalize()
function playdate.update()
for i = 0 , #rocks do
rocks[i]:moveUp()
end
for i = 0 , #rocks do
if rocks[i].isCrushed == true then
local x, y = getLastPosition()
rocks[i].isCrushed = false
rocks[i].isLast = true
rocks[i]:moveTo(x,y)
rocks[i]:add()
end
end
gfx.sprite.update()
pd.timer.updateTimers()
end