Skip to content
Download

3. Shots

  1. Create the object Bullet, with the sprite tiro. It does not go in the room: it is created by code.

  2. In the Layers panel, + Layer › Instances, named Tiros, below Instancias. Then click Instancias again.

  3. In Project › Project settings… › Input actions, + Action named atirar, bound to the space bar and to Pad:A.

  4. On Player, create the variable intervaloTiro, of type integer, with the value 10 (steps between shots).

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

A negative vspeed goes up: the engine adds the speed to the position every step. A shot that leaves the screen is destroyed.

On Player, add the “can shoot” flag and, at the end of onUpdate, the shot:

objects/Player.lua
function onSpawn(self)
self.animSpeed = 0.2
self.podeAtirar = true
end
function onUpdate(self)
-- ...
if held("atirar") and self.podeAtirar then
spawn("Bullet", self.x, self.y - 8, "Tiros")
self.podeAtirar = false
self:after(self.intervaloTiro, function() self.podeAtirar = true end)
end
end
  • spawn creates the shot on the Tiros layer, 8 pixels above the ship.
  • after allows the next shot after intervaloTiro steps.
  • C++: Player.cpp uses Bullet with no #include.

Play. Holding the space bar fires a burst. Change intervaloTiro to 3 or 30.

Next part: 4. Enemies