-3

All I am trying to do is add a half or full second delay to a line of code all i want is to play one animation and let it finish the switch to another.

  if (keyWentDown("space")) {
    player.setAnimation("attack");
    player.x = player.x+0.001;
  }
  drawSprites();
}

1 Answers1

0

You can make a delay(milliseconds) function that returns a Promise and await it before running your code.

const delay = (time) => new Promise(res => setTimeout(res, time))

async function YourCode() {
   // code before delay
   await delay(1000)
  // code after delay
}

Not the async keyword of the function to be able to await the delay

Abdellah Hariti
  • 657
  • 4
  • 8