In fact the code presents two immediate problems:
- The manipulation of keyboard events in the way that is done is not the most efficient to obtain the desired effect. Works well for web applications but not for games.
- If the animation aims games, it must be done within an animation loop and must make use of a graphical API.
Movements
In the case of a keyboard-sensitive animation the detection of the keys and displacement of the action target must not necessarily occur together, preferably separate events and be processed as distinct activities.
Uses both of these events keydown
and keyup
together.
keydown
to check and inform the system if a certain key has been pressed and nothing more!
keyup
to check and inform the system if a certain key has been released and nothing more!
Graphics
As for the graphical API Javascript natively provides two interfaces to Canvas API to work with raster and the SVG API to work with vector graphics.
To display the graphics the Canvas API will be used through the element HTML <canvas>
which is the element HTML used to draw graphs via script.
To use graphical methods on <canvas>
it is first necessary to obtain the graphic context which methods will be applied, whether it is a 2D/3D display context or whether the context is memory, to obtain the graphic context it is used the method HTMLCanvasElement.getContext()
.
To animate the graphics it is necessary to inform the browser that you want to carry out an animation and ask the browser to call a specific function to update an animation frame before the next repeat of the interface using the method window.requestAnimationFrame()
.
Example:
/**movimente com as setas direcionais do teclado**/
//Os estados das teclas a serem usados durante um frame de animação
let up = false;
let down = false;
let left = false;
let right = false;
//A posição do personagem na tela
let pos = {
x: 0,
y: 0
};
let lado = 50; //Medida da lateral do personagem(retângulo no caso)
let velocidade = 3; //Velocidade de deslocamento do personagem
let canvas; //Elemento canvas
let ctx; //Contexto gráfico do canvas
//Ao carregar a página...
window.addEventListener("load", () => {
//Obtém o canvas e contexto gráfico
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
//Quando pressionada uma tecla específica sinaliza o seu estado como pressionado
document.addEventListener("keydown", (event) => {
if (event.key == "ArrowUp") up = true;
if (event.key == "ArrowDown") down = true;
if (event.key == "ArrowLeft") left = true;
if (event.key == "ArrowRight") right = true;
});
//Quando solta um tecla específica sinaliza o seu estado como não pressionado
document.addEventListener("keyup", (event) => {
if (event.key == "ArrowUp") up = false;
if (event.key == "ArrowDown") down = false;
if (event.key == "ArrowLeft") left = false;
if (event.key == "ArrowRight") right = false;
});
//Inicia o loop de animação.
requestAnimationFrame(frame);
});
//A cada frame de animação
function frame() {
//Para cada uma das teclas do direcional verifica se estiver pressionada aplica um deslocamento
if (up) pos.y -= velocidade;
if (down) pos.y += velocidade;
if (left) pos.x -= velocidade;
if (right) pos.x += velocidade;
ctx.clearRect(0, 0, canvas.width, canvas.height); //Limpa a tela
ctx.fillStyle = "rgb(200,0,0)"; //Seta a cor do desenho como vermelho
ctx.fillRect(pos.x, pos.y, lado, lado); //Desenha o personagem tela(um retângulo)
requestAnimationFrame(frame); //Faz a solicitação de um novo quadro de animação
}
<!--Para animações mais performáticas use o elemento canvas-->
<canvas id="canvas"></canvas>
Excellent!!!! = D
– Daniel Mendes
Great answer! thanks for your attention.
– felipe cardozo