2d_sprite_animation.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. .. _doc_2d_sprite_animation:
  2. 2D sprite animation
  3. ===================
  4. Introduction
  5. ------------
  6. In this tutorial, you'll learn how to create 2D animated
  7. characters with the AnimatedSprite2D class and the AnimationPlayer.
  8. Typically, when you create or download an animated character,
  9. it will come in one of two ways: as individual images or as a single sprite sheet
  10. containing all the animation's frames.
  11. Both can be animated in Godot with the AnimatedSprite2D class.
  12. First, we'll use :ref:`AnimatedSprite2D <class_AnimatedSprite2D>` to
  13. animate a collection of individual images.
  14. Then we will animate a sprite sheet using this class.
  15. Finally, we will learn another way to animate a sprite sheet
  16. with :ref:`AnimationPlayer <class_AnimationPlayer>` and the *Animation*
  17. property of :ref:`Sprite2D <class_Sprite2D>`.
  18. .. note:: Art for the following examples by https://opengameart.org/users/ansimuz and tgfcoder.
  19. Individual images with AnimatedSprite2D
  20. ---------------------------------------
  21. In this scenario, you have a collection of images, each containing one of your
  22. character's animation frames. For this example, we'll use the following
  23. animation:
  24. .. image:: img/2d_animation_run_preview.gif
  25. You can download the images here:
  26. `2d_sprite_animation_assets.zip <https://github.com/godotengine/godot-docs-project-starters/releases/download/latest-4.x/2d_sprite_animation_assets.zip>`_
  27. Unzip the images and place them in your project folder. Set up your scene tree
  28. with the following nodes:
  29. .. image:: img/2d_animation_tree1.webp
  30. .. note:: The root node could also be :ref:`Area2D <class_Area2D>` or
  31. :ref:`RigidBody2D <class_RigidBody2D>`. The animation will still be
  32. made in the same way. Once the animation is completed, you can
  33. assign a shape to the CollisionShape2D. See
  34. :ref:`Physics Introduction <doc_physics_introduction>` for more
  35. information.
  36. Now select the ``AnimatedSprite2D`` and in its *SpriteFrames* property, select
  37. "New SpriteFrames".
  38. .. image:: img/2d_animation_new_spriteframes.webp
  39. Click on the new SpriteFrames resource and you'll see a new panel appear at the
  40. bottom of the editor window:
  41. .. image:: img/2d_animation_spriteframes.webp
  42. From the FileSystem dock on the left side, drag the 8 individual images into
  43. the center part of the SpriteFrames panel. On the left side, change the name
  44. of the animation from "default" to "run".
  45. .. image:: img/2d_animation_spriteframes_done.webp
  46. Use the "Play" buttons on the top-right of the *Filter Animations* input to preview the animation.
  47. You should now see the animation playing in the viewport.
  48. However, it is a bit slow. To fix this,
  49. change the *Speed (FPS)* setting in the SpriteFrames panel to 10.
  50. You can add additional animations by clicking the "Add Animation" button and
  51. adding additional images.
  52. Controlling the animation
  53. ~~~~~~~~~~~~~~~~~~~~~~~~~
  54. Once the animation is complete, you can control the animation via code using
  55. the ``play()`` and ``stop()`` methods. Here is a brief example to play the
  56. animation while the right arrow key is held, and stop it when the key is
  57. released.
  58. .. tabs::
  59. .. code-tab:: gdscript GDScript
  60. extends CharacterBody2D
  61. @onready var _animated_sprite = $AnimatedSprite2D
  62. func _process(_delta):
  63. if Input.is_action_pressed("ui_right"):
  64. _animated_sprite.play("run")
  65. else:
  66. _animated_sprite.stop()
  67. .. code-tab:: csharp
  68. using Godot;
  69. public partial class Character : CharacterBody2D
  70. {
  71. private AnimatedSprite2D _animatedSprite;
  72. public override void _Ready()
  73. {
  74. _animatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  75. }
  76. public override _Process(float _delta)
  77. {
  78. if (Input.IsActionPressed("ui_right"))
  79. {
  80. _animatedSprite.Play("run");
  81. }
  82. else
  83. {
  84. _animatedSprite.Stop();
  85. }
  86. }
  87. }
  88. Sprite sheet with AnimatedSprite2D
  89. ----------------------------------
  90. You can also easily animate from a sprite sheet with the class ``AnimatedSprite2D``.
  91. We will use this public domain sprite sheet:
  92. .. image:: img/2d_animation_frog_spritesheet.png
  93. Right-click the image and choose "Save Image As" to download it,
  94. and then copy the image into your project folder.
  95. Set up your scene tree the same way you did previously when using individual images.
  96. Select the ``AnimatedSprite2D`` and in its *SpriteFrames* property, select "New SpriteFrames".
  97. Click on the new SpriteFrames resource.
  98. This time, when the bottom panel appears, select "Add frames from a Sprite Sheet".
  99. .. image:: img/2d_animation_add_from_spritesheet.webp
  100. You will be prompted to open a file. Select your sprite sheet.
  101. A new window will open, showing your sprite sheet.
  102. The first thing you will need to do is to change the number of vertical and horizontal images in your sprite sheet.
  103. In this sprite sheet, we have four images horizontally and two images vertically.
  104. .. image:: img/2d_animation_spritesheet_select_rows.webp
  105. Next, select the frames from the sprite sheet that you want to include in your animation.
  106. We will select the top four, then click "Add 4 frames" to create the animation.
  107. .. image:: img/2d_animation_spritesheet_selectframes.webp
  108. You will now see your animation under the list of animations in the bottom panel.
  109. Double click on default to change the name of the animation to jump.
  110. .. image:: img/2d_animation_spritesheet_animation.webp
  111. Finally, check the play button on the SpriteFrames editor to see your frog jump!
  112. .. image:: img/2d_animation_play_spritesheet_animation.webp
  113. Sprite sheet with AnimationPlayer
  114. ---------------------------------
  115. Another way that you can animate when using a sprite sheet is to use a standard
  116. :ref:`Sprite2D <class_Sprite2D>` node to display the texture, and then animating the
  117. change from texture to texture with :ref:`AnimationPlayer <class_AnimationPlayer>`.
  118. Consider this sprite sheet, which contains 6 frames of animation:
  119. .. image:: img/2d_animation_player-run.png
  120. Right-click the image and choose "Save Image As" to download, then copy the
  121. image into your project folder.
  122. Our goal is to display these images one after another in a loop. Start by
  123. setting up your scene tree:
  124. .. image:: img/2d_animation_tree2.webp
  125. .. note:: The root node could also be :ref:`Area2D <class_Area2D>` or
  126. :ref:`RigidBody2D <class_RigidBody2D>`. The animation will still be
  127. made in the same way. Once the animation is completed, you can
  128. assign a shape to the CollisionShape2D. See
  129. :ref:`Physics Introduction <doc_physics_introduction>` for more
  130. information.
  131. Drag the spritesheet into the Sprite's *Texture* property, and you'll see the
  132. whole sheet displayed on the screen. To slice it up into individual frames,
  133. expand the *Animation* section in the Inspector and set the *Hframes* to ``6``.
  134. *Hframes* and *Vframes* are the number of horizontal and vertical frames in
  135. your sprite sheet.
  136. .. image:: img/2d_animation_setframes.webp
  137. Now try changing the value of the *Frame* property. You'll see that it ranges
  138. from ``0`` to ``5`` and the image displayed by the Sprite2D changes accordingly.
  139. This is the property we'll be animating.
  140. Select the ``AnimationPlayer`` and click the "Animation" button followed by
  141. "New". Name the new animation "walk". Set the animation length to ``0.6`` and
  142. click the "Loop" button so that our animation will repeat.
  143. .. image:: img/2d_animation_new_animation.webp
  144. Now select the ``Sprite2D`` node and click the key icon to add a new track.
  145. .. image:: img/2d_animation_new_track.webp
  146. Continue adding frames at each point in the timeline (``0.1`` seconds by
  147. default), until you have all the frames from 0 to 5. You'll see the frames
  148. actually appearing in the animation track:
  149. .. image:: img/2d_animation_full_animation.webp
  150. Press "Play" on the animation to see how it looks.
  151. .. image:: img/2d_animation_running.gif
  152. Controlling an AnimationPlayer animation
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. Like with AnimatedSprite2D, you can control the animation via code using
  155. the ``play()`` and ``stop()`` methods. Again, here is an example to play the
  156. animation while the right arrow key is held, and stop it when the key is
  157. released.
  158. .. tabs::
  159. .. code-tab:: gdscript GDScript
  160. extends CharacterBody2D
  161. @onready var _animation_player = $AnimationPlayer
  162. func _process(_delta):
  163. if Input.is_action_pressed("ui_right"):
  164. _animation_player.play("walk")
  165. else:
  166. _animation_player.stop()
  167. .. code-tab:: csharp
  168. using Godot;
  169. public partial class Character : CharacterBody2D
  170. {
  171. private AnimationPlayer _animationPlayer;
  172. public override void _Ready()
  173. {
  174. _animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
  175. }
  176. public override void _Process(float _delta)
  177. {
  178. if (Input.IsActionPressed("ui_right"))
  179. {
  180. _animationPlayer.Play("walk");
  181. }
  182. else
  183. {
  184. _animationPlayer.Stop();
  185. }
  186. }
  187. }
  188. .. note:: If updating both an animation and a separate property at once
  189. (for example, a platformer may update the sprite's ``h_flip``/``v_flip``
  190. properties when a character turns while starting a 'turning' animation),
  191. it's important to keep in mind that ``play()`` isn't applied instantly.
  192. Instead, it's applied the next time the :ref:`AnimationPlayer <class_AnimationPlayer>` is processed.
  193. This may end up being on the next frame, causing a 'glitch' frame,
  194. where the property change was applied but the animation was not.
  195. If this turns out to be a problem, after calling ``play()``, you can call ``advance(0)``
  196. to update the animation immediately.
  197. Summary
  198. -------
  199. These examples illustrate the two classes you can use in Godot for 2D animation.
  200. ``AnimationPlayer`` is a bit more complex than ``AnimatedSprite2D``,
  201. but it provides additional functionality, since you can also
  202. animate other properties like position or scale.
  203. The class ``AnimationPlayer`` can also be used with an ``AnimatedSprite2D``.
  204. Experiment to see what works best for your needs.