3. Tiros
No editor
Seção intitulada “No editor”-
Crie o objeto
Bullet, com o spritetiro. Ele não vai na room: é criado pelo código. -
No painel Camadas, + Camada › Instâncias, com o nome
Tiros, abaixo deInstancias. Depois clique de novo emInstancias. -
Em Projeto › Configurações do projeto… › Ações de entrada, + Ação com o nome
atirar, ligada à barra de espaço e aPad:A. -
No
Player, crie a variávelintervaloTiro, do tipo inteiro, com o valor10(passos entre os tiros).
function onSpawn(self) self.vspeed = -5end
function onUpdate(self) if self.y < -8 then self:destroy() endend#pragma once#include <brasa/brasa.hpp>
class Bullet : public brasa::Entity {public: BRASA_VARS(Bullet)
void onSpawn() override; void onUpdate() override;};#include "Bullet.hpp"
using namespace brasa;
void Bullet::onSpawn() { vspeed = -5.0f; }
void Bullet::onUpdate() { if (y < -8.0f) destroy();}
BRASA_ENTITY(Bullet);vspeed negativo sobe: a engine soma a velocidade à posição a cada passo. O tiro que sai da tela é destruído.
No Player, acrescente a marca de “pode atirar” e, no fim do onUpdate, o tiro:
function onSpawn(self) self.animSpeed = 0.2 self.podeAtirar = trueend
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) endendprivate: bool canShoot_ = true;void Player::onUpdate() { // ... if (held("atirar") && canShoot_) { spawn<Bullet>(x, y - 8.0f, "Tiros"); canShoot_ = false; after(intervaloTiro, [this] { canShoot_ = true; }); }}spawncria o tiro na camadaTiros, 8 pixels acima da nave.afterlibera o próximo tiro depois deintervaloTiropassos.- C++: o
Player.cppusaBulletsem#include.
Play. Segurar a barra de espaço dispara uma rajada. Mude intervaloTiro para 3 ou 30.
Próxima parte: 4. Inimigos
