Search code examples
flame

how to tell the joystick is changing direction


I want my player is able to change clothes if the joystick direction is changed, I mean if for now is gameRef.joystick.direction is JoystickDirection.right the player is moving right, otherwise player is moving left, but the clothes on the player can not tell the moment when the player direction is changed. I tried to use such method:

  bool moveLeft = false;
  bool moveRight = false;
  bool moveTop = false;
  bool moveDown = false;
  bool directionChanged = false;

And for update method:

moveLeft = gameRef.joystick.direction == JoystickDirection.left;
moveRight = gameRef.joystick.direction == JoystickDirection.right;
moveTop = gameRef.joystick.direction == JoystickDirection.up;
moveDown = gameRef.joystick.direction == JoystickDirection.down;

now I am stuck in how or where should I put directionChanged to let player change clothes at the exact moment the player change to another direction, maybe something like:

    Future<void> update(double dt) async {
    if (moveRight) {
          directionChanged = true;
          changeClothes();
      }
    }

    changeClothes(){
      //clothe change method;
      directionChanged = false; 
    }

But then the joystick towards left, the directionChanged is already true, so cloth is still for right state.

thanks for any clue!


Solution

  • Save down the last joystick direction outside of the update loop and call the changeClothes method when the value isn't the same as it was in the last iteration.

      JoystickDirection lastDirection = JoystickDirection.idle;
    
      void update(double dt) {
        if(gameRef.joystick.direction != lastDirection) {
          changeClothes();
          lastDirection = gameRef.joystick.direction;
        }
      }