Skip to content

Movement Animations

shiyiling1 edited this page Sep 2, 2021 · 4 revisions

Editing and Adding Movement Animations

  1. Update or create the atlas file of the character you wish to edit. To do this use a Texture packer as described in this wiki under animations. Be sure to follow the formatting of file outlined in these texture packers and read the documentation if you get stuck.

  2. Add a AnimationRederComponent() to the entity you wish to animate.

  3. Add specific animations to the AnimationRedercomponent using: animator.addAnimation(name, framerate, playMode).

  4. Add the atlas file to the GameArea's textures ie ForestGameArea.forestTexturesAtlases = {"images/male_character"}. This will load your sprite skin into the game environment.

  5. Add a eventListener to the AnimationController for your component. This will trigger the animation when the event is called.

How Animations Are Called

How Animations are Called For the Player Animation trigger events will be thrown on the key release managed from KeyboardPlayerInputCompnent:

public boolean keyUp(int keycode) {
        switch (keycode) {
            case Keys.W:
                entity.getEvents().trigger("standUp");
                return true;
            case Keys.A:
                entity.getEvents().trigger("standLeft");
                return true;
            case Keys.S:
                entity.getEvents().trigger("standDown");
                return true;

How Animations Are Triggered from NPC's

Movement events are thrown from PhysicsNovementComponent.movementEvents(). private int lastDirection stores the last compass direction taken by the entity as an integer, where north equals 0, east equals 1 etc.

It is important to check for a change in direction to prevent animations being initialized every update(). This would result in a still image because update() is called > than the animation frame rate.

  public void movementEvents() {
    Vector2 entityDirection = getDirection();
    float x = entityDirection.x;
    float y = entityDirection.y;

    if (lastDirection != currentDirection) {
      if (x < 0 && y < 0.5 && y > -0.5) {
        entity.getEvents().trigger("walkLeft");
        lastDirection = 3;
      } else if (x > 0 && y < 0.5 && y > -0.5) {
        entity.getEvents().trigger("walkRight");
        lastDirection = 1;
      } else if (x < 0.5 && x > -0.5 && y > 0) {
        entity.getEvents().trigger("walkUp");
        lastDirection = 0;
      } else if (x < 0.5 && x > -0.5 && y < 0) {
        lastDirection = 2;
        entity.getEvents().trigger("walkDown");
      }
    }
Clone this wiki locally