2. The ship
Object
Section titled “Object”-
In Resources, right-click the
objectsfolder › New object…, namedPlayer. -
In the Inspector, under Appearance, pick the sprite
nave. -
Under Variables, + Variable › number, named
velocidadewith the value2(pixels per step). -
Open the room
Mainand dragPlayerinto it. In the instance Inspector, Position X160, Y150.
Open code, in the object Inspector, opens the file. Replace the contents:
function onSpawn(self) self.animSpeed = 0.2end
function onUpdate(self) local dx = axis("esquerda", "direita") local dy = axis("cima", "baixo") if dx ~= 0 and dy ~= 0 then dx = dx * 0.7071 dy = dy * 0.7071 end
self.x = clamp(self.x + dx * self.velocidade, 8, roomWidth() - 8) self.y = clamp(self.y + dy * self.velocidade, 8, roomHeight() - 8)end#pragma once#include <brasa/brasa.hpp>
class Player : public brasa::Entity {public: BRASA_VARS(Player)
void onSpawn() override; void onUpdate() override;};#include "Player.hpp"#include <algorithm>
using namespace brasa;
void Player::onSpawn() { animSpeed = 0.2f;}
void Player::onUpdate() { float dx = axis("esquerda", "direita"); float dy = axis("cima", "baixo"); if (dx != 0.0f && dy != 0.0f) { dx *= 0.7071f; dy *= 0.7071f; }
const float maxX = static_cast<float>(roomWidth()) - 8.0f; const float maxY = static_cast<float>(roomHeight()) - 8.0f; x = std::clamp(x + dx * velocidade, 8.0f, maxX); y = std::clamp(y + dy * velocidade, 8.0f, maxY);}
BRASA_ENTITY(Player);onSpawnruns once;onUpdate, every step.animSpeed = 0.2changes frame every 5 steps.axisreturns-1to1for a pair of actions.esquerda,direita,cimaandbaixocome with the project, bound to the arrows, WASD and the gamepad.- The
0.7071factor evens out the diagonal speed;clampkeeps the ship inside the room. velocidadeis the Inspector variable.
Play (F5). The ship moves with the arrows or WASD. Change velocidade in the Inspector to see the effect, without touching the code.
Next part: 3. Shots
