Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

482
Visualizações
How can I make 2D animations run more smoothly?

I am trying to make a 2d game. I am using this sprite sheet:

enter image description here

I want to make the animation of the character when he moves, but every time I move the character (with the keyboard arrows), the character moves to my chosen side, but in glitchy way, and doesn't move with the animation.

There is my code:

let walkCycle = [0, 1, 2, 3];
let walkIndex = 0;

let width = 50;
let height = 60;
let SCALE = 2;
let SCALED_WIDTH = SCALE * width;
let SCALED_HEIGHT = SCALE * height;

function drawFrame(frameX, frameY, canvasX, canvasY) {
  context.drawImage(
   heroImg,
   frameX * width,  
   frameY * height,  
   width, 
   height, 
   canvasX, 
   canvasY, 
   SCALED_WIDTH,
   SCALED_HEIGHT
 );
}

let DOWN = 0;
let UP = 2;
let LEFT = 3;
let RIGHT = 1;

let currentDirection = DOWN;

function moveHero() {
  let hasMoved = false;

  if (38 in keysDown) {
    
    moveChar(0, -1, UP);
    hasMoved = true;
  }
    if (40 in keysDown) {
    
    moveChar(0, 1, DOWN);
    hasMoved = true;
  }
    if (37 in keysDown) {
    
    moveChar(-1, 0, LEFT);
    hasMoved = true;
  }
    if (39 in keysDown) {
    
    moveChar(1, 0, RIGHT);
    hasMoved = true;
    }
   if (!hasMoved) {
    walkInex = 0;
   }
   if (hasMoved) {
     
     walkIndex++;
     if (walkIndex >= walkCycle.length) {
       walkIndex = 0;
    }
  }
  drawFrame(walkCycle[walkIndex],
   0, hero.x, hero.y);
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda