Clean up dead sprites properly.
This commit is contained in:
parent
9102397c77
commit
5019be454c
|
@ -38,7 +38,11 @@ function Bullet:update()
|
|||
obj:damage(self.damage)
|
||||
end
|
||||
|
||||
if #collisions >= 1 then
|
||||
if #collisions >= 1 or self:outOfBounds() then
|
||||
self:remove()
|
||||
end
|
||||
end
|
||||
|
||||
function Bullet:outOfBounds()
|
||||
return self.x < 0 or self.y < 0 or self.x > 400 or self.y > 240
|
||||
end
|
||||
|
|
|
@ -17,9 +17,17 @@ function Ebi:init()
|
|||
if math.random(2) == 1 then
|
||||
dir = -1
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
function Ebi:update()
|
||||
|
@ -31,13 +39,7 @@ function Ebi:update()
|
|||
end
|
||||
end
|
||||
|
||||
function Ebi:weaponTimer()
|
||||
local t = playdate.timer.new(1000,
|
||||
function()
|
||||
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
|
||||
function Ebi:delete()
|
||||
Ebi.super.delete(self)
|
||||
self.weaponTimer:remove()
|
||||
end
|
||||
|
|
|
@ -30,7 +30,7 @@ function Entity:damage(amount)
|
|||
self.health = math.max(self.health - (amount - self.armor), 0)
|
||||
|
||||
if self.health == 0 then
|
||||
self:remove()
|
||||
self:delete()
|
||||
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))
|
||||
return collisions
|
||||
end
|
||||
|
||||
-- override this if you create timers
|
||||
function Entity:delete()
|
||||
self:remove()
|
||||
end
|
||||
|
|
|
@ -46,8 +46,14 @@ function setup()
|
|||
enemy:add()
|
||||
|
||||
makeWalls()
|
||||
|
||||
drawBackground()
|
||||
|
||||
-- debug, TODO remove this code
|
||||
playdate.inputHandlers.push({
|
||||
BButtonUp = function()
|
||||
print(gfx.sprite.spriteCount())
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
function makeWalls()
|
||||
|
|
Loading…
Reference in New Issue
Block a user