Shoot
The shot is another object, created with spawn. It moves by itself with the speed set in onSpawn.
function onSpawn(self) self.vspeed = -5end
function onUpdate(self) if self.y < -8 then self:destroy() endendclass Bullet : public Entity {public: void onSpawn() override { vspeed = -5; } void onUpdate() override { if (y < -8) destroy(); }};BRASA_ENTITY(Bullet);In the object that shoots:
function onUpdate(self) if pressed("atirar") then spawn("Bullet", self.x, self.y, "Instancias") endendvoid onUpdate() override { if (pressed("atirar")) spawn<Bullet>(x, y, "Instancias");}Delay between shots
Section titled “Delay between shots”function onSpawn(self) self.podeAtirar = trueend
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) endendbool podeAtirar_ = true;
void onUpdate() override { if (held("atirar") && podeAtirar_) { spawn<Bullet>(x, y, "Instancias"); podeAtirar_ = false; after(10, [this] { podeAtirar_ = true; }); }}Toward the mouse
Section titled “Toward the mouse”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()))if (Bullet* bala = spawn<Bullet>(x, y, "Instancias")) { bala->setSpeed(5); bala->setDirection(angleTo(mouseX(), mouseY()));}