Skip to content
Download

Shoot

The shot is another object, created with spawn. It moves by itself with the speed set in onSpawn.

objects/Bullet.lua
function onSpawn(self)
self.vspeed = -5
end
function onUpdate(self)
if self.y < -8 then self:destroy() end
end

In the object that shoots:

function onUpdate(self)
if pressed("atirar") then
spawn("Bullet", self.x, self.y, "Instancias")
end
end
function onSpawn(self)
self.podeAtirar = true
end
function onUpdate(self)
if held("atirar") and self.podeAtirar then
spawn("Bullet", self.x, self.y, "Instancias")
self.podeAtirar = false
self:after(10, function() self.podeAtirar = true end)
end
end

spawn returns the created instance. Its onSpawn has already run, so what is set afterwards is what counts.

local bala = spawn("Bullet", self.x, self.y, "Instancias")
bala:setSpeed(5)
bala:setDirection(self:angleTo(mouseX(), mouseY()))

Reference: Instances, Timers.