2d_sprite_animation.rst 9.9 KB

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