In game development, animations play a crucial role in creating an engaging and immersive experience for players. Adding 2D animations to your Godot game can bring your characters and environments to life, making them more visually appealing and interactive.
Fortunately, Godot provides powerful tools and features to create and control 2D animations easily.
Setting Up the Godot Game
To begin, set up a basic 2D game scene in the Godot game engine. Create a new scene and add a KinematicBody2D node as the player character. Inside the KinematicBody2D, add a CollisionShape2D with a rectangle shape that represents the player's collision boundaries.
The code used in this article is available in this GitHub repository and is free for you to use under the MIT license.
Additionally, add an AnimatedSprite node to handle the player's animations. Furthermore, make sure that you map the following input actions in your Input Map:
Next, write the GDScript code to control the player's movement. Attach the following script to the KinematicBody2D node:
extends KinematicBody2Dconst SPEED = 200
func _physics_process(delta):
var velocity = Vector2.ZERO
if Input.is_action_pressed("move_left"):
velocity.x -= SPEED
if Input.is_action_pressed("move_right"):
velocity.x += SPEED
if Input.is_action_pressed("move_up"):
velocity.y -= SPEED
if Input.is_action_pressed("move_down"):
velocity.y += SPEED
velocity = move_and_slide(velocity)
This script sets up a constant speed for the player and allows them to move left, right, up, and down using the arrow keys or WASD.

Adding SpriteSheet in AnimatedSprite
Now, configure the AnimatedSprite to use a sprite sheet for animations. Select the AnimatedSprite node and navigate to the Frames section in the node's properties tab. Here, click on the New SpriteFrames button.
Switch to the SpriteFrames tab located at the bottom of the Godot editor. In the SpriteFrames tab, click on the New Animation button. Create animations such as walk and idle by adding appropriate frames to each animation.
Additionally, you have the option to create other animations, such as shooting, jumping, and climbing for a platformer game. After that, click on the Add Frames from SpriteSheet button to automatically extract individual frames from the sprite sheet.

Controlling Animations Using GDScript
Now that you have your animations set up, you can control them programmatically using GDScript.
Playing and Stopping the Animation
Controlling the playback of animations is essential to provide dynamic and interactive experiences in your game. The AnimatedSprite node in Godot offers methods to play and stop animations as per your game logic.
Extend the KinematicBody2D node and handle the animation control within the _physics_process function. You can use the play_animation and stop_animation inputs to trigger the corresponding animation actions.
extends KinematicBody2Dfunc _physics_process(delta):
# Play the animation
if Input.is_action_just_pressed("play_animation"):
$AnimatedSprite.play()
# Stop the animation and reset to the first frame
if Input.is_action_just_pressed("stop_animation"):
$AnimatedSprite.stop()
$AnimatedSprite.frame = 0
By mapping the appropriate input actions, you can provide players with control over the animation playback in your game.
For example, you might bind the play_animation action to a button press or a specific event in your game, allowing the player to trigger an animation sequence at a desired moment. Additionally, you can find copyright-free music to play while the animation is running.
Similarly, you can trigger the stop_animation actions to completely stop the animation.
By incorporating these animation control mechanisms, you can add depth and interactivity to your game's animations, creating more engaging and immersive experiences for your players.
Rotating the Animation
Rotating the animation can add visual interest and variety to your game. You can programmatically rotate the AnimatedSprite node to change the orientation of the animation. You can apply the rotation in degrees using the rotate() method.
extends KinematicBody2Dfunc _physics_process(delta):
if Input.is_action_just_pressed("rotate_animation"):
# Rotate the animation by 45 degrees clockwise
$AnimatedSprite.rotate(deg2rad(45))
When you press the buttons associated with the rotate_animation (you can define this action in your input map), the rotate() method is called on the AnimatedSprite node. It rotates the node by 45 degrees in a clockwise direction using deg2rad() to convert the degrees to radians.

Keep in mind that it will apply rotation to the entire AnimatedSprite node, including all frames of the animation. Therefore, if you want to rotate only specific frames, you may need to split them into separate AnimatedSprite nodes or use other techniques such as flipping individual frames.
Flipping the Animation
Flipping the animation horizontally or vertically can be useful for reflecting changes in character direction. In Godot, the AnimatedSprite node provides properties to control flipping.
To flip the animation horizontally, set the flip_h property of the AnimatedSprite to true. This will mirror the animation along the horizontal axis. Similarly, setting the flip_v property to true will mirror the animation along the vertical axis.
extends KinematicBody2Dfunc _physics_process(delta):
if Input.is_action_just_pressed("flip_animation"):
$AnimatedSprite.flip_h = true
# or $AnimatedSprite.flip_v = true for vertical flipping
If the player presses the flip_animation input action then set the flip_h property of the AnimatedSprite to true. This will flip the animation horizontally.
Utilizing Signals in AnimatedSprite
In addition to controlling animations programmatically, Godot provides a powerful event system called signals. Signals allow you to respond to specific events or changes that occur during the execution of your game.
In the case of AnimatedSprite, there are two important signals that you can use: animation_finished() and frame_changed().
1. animation_finished() Signal
The animation_finished() signal is emitted when the animation reaches the last frame, either during a single playback or when it loops. This signal is useful when you want to perform actions or trigger events when an animation completes.
extends KinematicBody2Dfunc _ready():
$AnimatedSprite.connect("animation_finished", self, "_on_animation_finished")
func _on_animation_finished():
# Perform actions or trigger events
print("Animation finished!")
# Additional code here...
Connect the animation_finished() signal of the AnimatedSprite to the _on_animation_finished() method in the same script using the connect() function.
When the animation finishes playing, you can execute custom logic or trigger other functionalities using the _on_animation_finished() method.
2. frame_changed() Signal
The frame_changed() signal is emitted whenever the current frame of the animation changes. This can occur when the animation is playing or when you programmatically modify the frame. You can use this signal to detect frame changes and react accordingly.
extends KinematicBody2Dfunc _ready():
$AnimatedSprite.connect("frame_changed", self, "_on_frame_changed")
func _on_frame_changed():
# Perform actions based on the current frame
var currentFrame = $AnimatedSprite.frame
print("Current frame: ", currentFrame)
# Additional code here...
Connect the frame_changed() signal of the AnimatedSprite to the _on_frame_changed() method in the same script. When the frame changes, you can access the current frame using the _on_frame_changed() method and perform actions or logic based on the frame's value.
By utilizing signals, you can respond to animation events such as completion or frame changes and incorporate dynamic behaviors or trigger specific actions in your game.
Make Godot Games More Engaging Using Animations
Adding 2D animations to your Godot games can greatly enhance the overall player experience. Animations bring characters to life, making their movements and actions more visually appealing. By incorporating animations for various actions such as walking, running, attacking, and jumping, you can create a dynamic and immersive gameplay environment.
Moreover, you can also use animations to provide visual feedback to the player. This feedback helps to make the game more engaging and responsive, enhancing the player's sense of control and involvement.
ncG1vNJzZmivp6x7rq3KnqysnZ%2Bbe6S7zGhpnWWRo7aurdOipqdll6SxsMCMrqqippdirq%2B1zJqrnpyjpb%2BqwMRo