2D Sprite blitheness¶

Introduction¶

In this tutorial, yous'll learn how to create 2nd animated characters with the AnimatedSprite class and the AnimationPlayer. Typically, when y'all create or download an animated character, it will come in one of ii means: as private images or equally a single sprite canvas containing all the animation's frames. Both can be animated in Godot with the AnimatedSprite class.

Outset, nosotros'll use AnimatedSprite to animate a collection of private images. Then nosotros will animate a sprite sheet using this class. Finally, we will learn some other way to breathing a sprite sheet with AnimationPlayer and the Animation property of Sprite.

Individual images with AnimatedSprite¶

In this scenario, you have a collection of images, each containing one of your grapheme's animation frames. For this example, we'll apply the following animation:

../../_images/2d_animation_run_preview.gif

Yous tin can download the images here: run_animation.cypher

Unzip the images and identify them in your project folder. Gear up your scene tree with the following nodes:

../../_images/2d_animation_tree1.png

Note

The root node could also exist Area2D or RigidBody2D. The animation volition still be made in the aforementioned way. Once the blitheness is completed, you can assign a shape to the CollisionShape2D. See Physics Introduction for more information.

Now select the AnimatedSprite and in its SpriteFrames property, select "New SpriteFrames".

../../_images/2d_animation_new_spriteframes.png

Click on the new SpriteFrames resource and you'll see a new panel appear at the bottom of the editor window:

../../_images/2d_animation_spriteframes.png

From the FileSystem dock on the left side, drag the 8 individual images into the center part of the SpriteFrames panel. On the left side, alter the proper noun of the animation from "default" to "run".

../../_images/2d_animation_spriteframes_done.png

Dorsum in the Inspector, check the box for the Playing holding. You should now encounter the blitheness playing in the viewport. Nonetheless, it is a flake slow. To gear up this, modify the Speed (FPS) setting in the SpriteFrames panel to x.

You tin add additional animations by clicking the "New Animation" button and adding additional images.

Controlling the animation¶

Once the animation is complete, you tin control the animation via code using the play() and stop() methods. Here is a brief instance to play the animation while the right pointer central is held, and stop it when the key is released.

                                extends                KinematicBody2D                onready                var                _animated_sprite                =                $                AnimatedSprite                func                _process                (                _delta                ):                if                Input                .                is_action_pressed                (                "ui_right"                ):                _animated_sprite                .                play                (                "run"                )                else                :                _animated_sprite                .                finish                ()              

Sprite sail with AnimatedSprite¶

You can also hands breathing from a sprite sail with the form AnimatedSprite . Nosotros will apply this public domain sprite sheet:

../../_images/2d_animation_frog_spritesheet.png

Correct-click the image and choose "Salvage Epitome As" to download it, and then copy the image into your project folder.

Gear up up your scene tree the same way yous did previously when using individual images. Select the AnimatedSprite and in its SpriteFrames property, select "New SpriteFrames".

Click on the new SpriteFrames resources. This time, when the bottom panel appears, select "Add frames from a Sprite Canvas".

../../_images/2d_animation_add_from_spritesheet.png

You lot volition be prompted to open a file. Select your sprite sheet.

A new window will open, showing your sprite sheet. The showtime affair you lot will need to exercise is to change the number of vertical and horizontal images in your sprite sheet. In this sprite canvass, nosotros have 4 images horizontally and two images vertically.

../../_images/2d_animation_spritesheet_select_rows.png

Next, select the frames from the sprite sheet that y'all want to include in your animation. Nosotros will select the elevation 4, then click "Add together 4 frames" to create the animation.

../../_images/2d_animation_spritesheet_selectframes.png

You will now meet your animation nether the listing of animations in the bottom panel. Double click on default to change the name of the blitheness to jump.

../../_images/2d_animation_spritesheet_animation.png

Finally, cheque Playing on the AnimatedSprite in the inspector to see your frog bound!

../../_images/2d_animation_play_spritesheet_animation.png

Sprite sheet with AnimationPlayer¶

Another way that you can breathing when using a sprite sheet is to use a standard Sprite node to display the texture, and and then animating the modify from texture to texture with AnimationPlayer.

Consider this sprite sheet, which contains vi frames of animation:

../../_images/2d_animation_player-run.png

Right-click the image and choose "Salvage Image As" to download, then re-create the paradigm into your projection folder.

Our goal is to display these images ane after another in a loop. Start past setting up your scene tree:

../../_images/2d_animation_tree2.png

Note

The root node could also be Area2D or RigidBody2D. The animation will withal be made in the same fashion. Once the animation is completed, you tin assign a shape to the CollisionShape2D. See Physics Introduction for more information.

Elevate the spritesheet into the Sprite's Texture property, and y'all'll see the whole sheet displayed on the screen. To piece it upwards into individual frames, expand the Animation department in the Inspector and set up the Hframes to 6 . Hframes and Vframes are the number of horizontal and vertical frames in your sprite sail.

../../_images/2d_animation_setframes.png

At present try irresolute the value of the Frame property. You'll see that it ranges from 0 to 5 and the epitome displayed by the Sprite changes appropriately. This is the belongings we'll be animating.

Select the AnimationPlayer and click the "Animation" push followed by "New". Proper name the new animation "walk". Set the blitheness length to 0.half dozen and click the "Loop" button so that our animation volition echo.

../../_images/2d_animation_new_animation.png

Now select the Sprite node and click the key icon to add a new rails.

../../_images/2d_animation_new_track.png

Continue adding frames at each point in the timeline ( 0.1 seconds by default), until y'all accept all the frames from 0 to 5. You'll run into the frames actually appearing in the animation rail:

../../_images/2d_animation_full_animation.png

Press "Play" on the animation to meet how information technology looks.

../../_images/2d_animation_running.gif

Controlling an AnimationPlayer blitheness¶

Like with AnimatedSprite, yous tin control the animation via code using the play() and stop() methods. Again, here is an example to play the animation while the right pointer cardinal is held, and stop it when the key is released.

                                extends                KinematicBody2D                onready                var                _animation_player                =                $                AnimationPlayer                func                _process                (                _delta                ):                if                Input                .                is_action_pressed                (                "ui_right"                ):                _animation_player                .                play                (                "walk"                )                else                :                _animation_player                .                stop                ()              

Note

If updating both an blitheness and a separate property at once (for example, a platformer may update the sprite's h_flip / v_flip backdrop when a character turns while starting a 'turning' blitheness), it'southward of import to continue in mind that play() isn't applied instantly. Instead, information technology's applied the next time the AnimationPlayer is processed. This may terminate up beingness on the adjacent frame, causing a 'glitch' frame, where the property change was applied only the animation was not. If this turns out to exist a problem, after calling play() , you can phone call advance(0) to update the animation immediately.

Summary¶

These examples illustrate the ii classes yous tin can utilize in Godot for 2nd blitheness. AnimationPlayer is a bit more circuitous than AnimatedSprite , but it provides boosted functionality, since you lot can also animate other properties similar position or scale. The course AnimationPlayer can also be used with an AnimatedSprite . Experiment to run across what works all-time for your needs.