2d_sprite_animation.rst 9.9 KB

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