Skip to content
czw-qwert edited this page Aug 31, 2026 · 1 revision
private void ctrlChanged(boolean pressed) { //it is for crouch
    if (pressed) {
      textureRenderComponent.setTexture(CROUCH_TEXTURE);
      sneaking = true;
      crouching = true;
      updateSpeed();
    } else {
      textureRenderComponent.setTexture(NORMAL_TEXTURE);
      sneaking = false;
      crouching = false;
      updateSpeed();
    }
  }

It is for crouch feature for player. pressed is used to know the left control key is pressed. and use textureRenderComponent.setTexture to change the body of player. CROUCH_TEXTURE and NORMAL_TEXTURE are string. They record the file path of xxx.png which are the different different states of player.

private void updateSpeed() {
    Body body = physicsComponent.getBody();
    if(crouching==true){
      Speed.x = MAX_SPEED.cpy().x * CrouchSpeedRate;
    }
      else{
        Speed = MAX_SPEED.cpy();
    }
    Vector2 desiredVelocity = walkDirection.cpy().scl(Speed);

It is used to update the speed in crouching. Using if to make sure that player is in crouch states and change the speed in x direction for example the speed of x direction of MAX_SPEED is 30 and the rate is 0.2, the speed of crouch is 6 = 30 * 0.2.

Clone this wiki locally