Skip to main content

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

Sup.ArcadePhysics2D.setGravity(0, -0.02);

class PlayerBehavior extends Sup.Behavior {
  speed = 0.08;
  jumpSpeed = 0.35;
  frameSpeed = 1;
  reverseSpeed = -1;

  update() {
    Sup.ArcadePhysics2D.collides(this.actor.arcadeBody2D, Sup.ArcadePhysics2D.getAllBodies());

    // As explained above, we get the current velocity
    let velocity = this.actor.arcadeBody2D.getVelocity();

    // We override the `.x` component based on the player's input
    if (Sup.Input.isKeyDown("DOWN"))  {
      this.actor.spriteRenderer.setAnimation("Crouch",true);  
      velocity.x = 0.000001;
      //this.actor.spriteRenderer.setPlaybackSpeed(this.frameSpeed);
      
    }else if (Sup.Input.isKeyDown("RIGHT")) {
      velocity.x = this.speed;
      this.actor.spriteRenderer.setAnimation("Walk");
      this.actor.spriteRenderer.setPlaybackSpeed(this.frameSpeed);
    } else if (Sup.Input.isKeyDown("LEFT")) {
      velocity.x = -this.speed + 0.02;
      this.actor.spriteRenderer.setAnimation("Walk");
      this.actor.spriteRenderer.setPlaybackSpeed(this.reverseSpeed);
      //velocity.x = this.speed - 0.08;
    } else {
      velocity.x = 0;
    }
      

    // If the player is on the ground and wants to jump,
    // we update the `.y` component accordingly
    let touchBottom = this.actor.arcadeBody2D.getTouches().bottom;
    if (touchBottom) {
      if (Sup.Input.wasKeyJustPressed("UP")) {
        velocity.y = this.jumpSpeed;
        this.actor.spriteRenderer.setAnimation("Jump");
      } else {
        // Here, we should play either "Idle" or "Run" depending on the horizontal speed
        if (velocity.x === 0) {
          this.actor.spriteRenderer.setAnimation("Idle");
          this.actor.spriteRenderer.setPlaybackSpeed(this.frameSpeed);
        } else {
          if (velocity.x === 0.000001) {
            this.actor.spriteRenderer.setAnimation("Crouch",false);
          }
        }
        
      }
    } else {
      // Here, we should play either "Jump" or "Fall" depending on the vertical speed
      if (velocity.y >= 0) this.actor.spriteRenderer.setAnimation("Jump");
      else this.actor.spriteRenderer.setAnimation("Fall");
    }

    // Finally, we apply the velocity back to the ArcadePhysics body
    this.actor.arcadeBody2D.setVelocity(velocity);
  }
}
Sup.registerBehavior(PlayerBehavior);

(1 edit)

I built on top of the preset built into SuperPowers. PLEASE NOTE IT SAYS true BUT THAT WAS JUST A TEST

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!