Clean up dead sprites properly.

This commit is contained in:
Anna Rose 2023-09-30 18:23:42 -04:00
parent 9102397c77
commit 5019be454c
4 changed files with 30 additions and 13 deletions

View File

@ -38,7 +38,11 @@ function Bullet:update()
obj:damage(self.damage) obj:damage(self.damage)
end end
if #collisions >= 1 then if #collisions >= 1 or self:outOfBounds() then
self:remove() self:remove()
end end
end end
function Bullet:outOfBounds()
return self.x < 0 or self.y < 0 or self.x > 400 or self.y > 240
end

View File

@ -17,9 +17,17 @@ function Ebi:init()
if math.random(2) == 1 then if math.random(2) == 1 then
dir = -1 dir = -1
end end
self.vector = {x=0, y=dir} self.vector = {x=0, y=dir}
self:weaponTimer() self.weaponTimer = playdate.timer.new(2500,
function()
local b = Bullet(2, 1, {x=-1, y=0}, 0x2)
b:moveTo(self.x - (self.width/2) - 1, self.y)
b:add()
end
)
self.weaponTimer.repeats = true
end end
function Ebi:update() function Ebi:update()
@ -31,13 +39,7 @@ function Ebi:update()
end end
end end
function Ebi:weaponTimer() function Ebi:delete()
local t = playdate.timer.new(1000, Ebi.super.delete(self)
function() self.weaponTimer:remove()
local b = Bullet(2, 1, {x=-1, y=0}, 0x2)
b:moveTo(self.x - (self.width/2) - 1, self.y)
b:add()
end
)
t.repeats = true
end end

View File

@ -30,7 +30,7 @@ function Entity:damage(amount)
self.health = math.max(self.health - (amount - self.armor), 0) self.health = math.max(self.health - (amount - self.armor), 0)
if self.health == 0 then if self.health == 0 then
self:remove() self:delete()
end end
end end
@ -38,3 +38,8 @@ function Entity:update()
local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y)) local collisions = select(3, self:moveWithCollisions(self.x + self.vector.x, self.y + self.vector.y))
return collisions return collisions
end end
-- override this if you create timers
function Entity:delete()
self:remove()
end

View File

@ -46,8 +46,14 @@ function setup()
enemy:add() enemy:add()
makeWalls() makeWalls()
drawBackground() drawBackground()
-- debug, TODO remove this code
playdate.inputHandlers.push({
BButtonUp = function()
print(gfx.sprite.spriteCount())
end
})
end end
function makeWalls() function makeWalls()