I am attempting to create an enemy in a game that shoots a bullet every 5 seconds.
I'm currently using the performAfterDelay function in the timer library to delay the shoot function.
function enemy:shoot(x, y)
local projectileInstance = bullets(x, y, "images/squareBullets", 2, "square")
projectileInstance:add()
end
function enemy:ability()
local function timerCallback()
self:shoot(self.x, self.y)
end
pd.timer.performAfterDelay(5000, timerCallback)
--pd.timer.performAfterDelay(50, timerCallback)
end
function enemy:update()
enemy.super.update(self) --overrride default update
self:ability()
pd.timer.updateTimers()
gfx.sprite.update()
end
However, multiple bullets show up on screen instead one every 5 seconds.
I think this happens because the performAfterDelay function execute the shoot function slower than the playdate update function executes.
I guess my question is: how do I decrease the rate at which new bullet sprites are created?