d
Panagiotis Georgiadis(drpaneas)

How to synchronize a character’s movement direction with an animation.

In this part, one of the learnings is about "How to synchronize a character’s movement direction with a texture". While this is indeed very useful, it's very common for 2D top-down RPGs to have a moving animation walking, running, jumping etc ... 

How would I be able to accomplish the same thing using an animated sprite-sheet? e.g. When the user presses RightKey, the player should a. Move to the right, and b. play the walk_right animation.

  • Xananax replied

    You perfectly can, yes. The same logic can be used for any kind of complexity. It always boils down to:

    1. Understand what the player is doing
    2. Change the displayed frame, set of frames, or animation

    "Understand what the player is doing" means reading the current action. This is commonly done through reading inputs. So, when you press "up" the KinematicBody independently moves up, while the Sprite changes frame.

    But it can also be done by reading the current velocity, direction, or any other indicator.

    This example by KidsCanCode demonstrates a sprite that walks and uses an AnimatedSprite.

    http://kidscancode.org/godot_recipes/3.x/2d/8_direction/

    You'll see that the base logic is about similar to what we're doing but more detailed and longer. Not fundamentally different, though.

    Later in this course, the Side Scroller lesson will present a character that can jump, push, and walk using the AnimationPlayer. We give the code for it because we are teaching something else in that lesson. But you can take your time reading SideScroller/SideScrollSkin/Skin.gd. It's a jump in terms of complexity, but I think after finishing all the chapters, you should be able to parse it out.

    If not, feel free to ask more questions here!