Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Alright, so here's the issue:

 if (Sup.Input.isKeyDown("DOWN"))  {
      this.actor.spriteRenderer.setAnimation("Crouch",true);  

Your animation isn't actually looping, it's just constantly being restarted and applied to the character.


The function:

Sup.Input.isKeyDown('KEY-NAME')

runs every update loop (60 times a second). So, if you're holding down the key, this if statement will return true and run the code inside every frame. Hence, your sprite is constantly setting it's animation to "Crouch" and is looking like it's looping the animation.


To fix this, you can set a variable at the beginning of the behavior and check against it, like this:

class PlayerBehavior extends Sup.Behavior {
 crouching = false;
 update() {
  if( Sup.Input.isKeyDown("DOWN") ) {
   if( !this.crouching ) {
    this.actor.spriteRenderer.setAnimation("Crouch", false);
    this.crouching = true;
   }
  } else if( this.crouching ) {
    this.actor.spriteRenderer.setAnimation("Idle", false);
    this.crouching = false;
  }
 }
}

This way, the code will only set the animation once, and prevent 'looping' the animation every frame you're holding the button down for.


Another way to fix this, is to use:

Sup.Input.wasKeyJustPressed("KEY-NAME")

which will only run once when the key is first pressed down.


If you need any more info, let me know!