Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

481
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!