2d_sprite_animation.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. func _process(delta):
  58. if Input.is_action_pressed("ui_right"):
  59. $AnimatedSprite.play("run")
  60. else:
  61. $AnimatedSprite.stop()
  62. Sprite sheet with AnimatedSprite
  63. --------------------------------
  64. You can also easily animate from a sprite sheet with the class ``AnimatedSprite``. We will use this public domain sprite sheet:
  65. .. image:: img/2d_animation_frog_spritesheet.png
  66. Right-click the image and choose "Save Image As" to download it, and then copy the image into your project folder.
  67. Set up your scene tree the same way you did previously when using individual images. Select the ``AnimatedSprite`` and in its *SpriteFrames* property, select
  68. "New SpriteFrames".
  69. Click on the new SpriteFrames resource. This time, when the bottom panel appears, select "Add frames from a Sprite Sheet".
  70. .. image:: img/2d_animation_add_from_spritesheet.png
  71. You will be prompted to open a file. Select your sprite sheet.
  72. 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.
  73. .. image:: img/2d_animation_spritesheet_select_rows.png
  74. 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.
  75. .. image:: img/2d_animation_spritesheet_selectframes.png
  76. 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.
  77. .. image:: img/2d_animation_spritesheet_animation.png
  78. Finally, check Playing on the AnimatedSprite in the inspector to see your frog jump!
  79. .. image:: img/2d_animation_play_spritesheet_animation.png
  80. Sprite sheet with AnimationPlayer
  81. ---------------------------------
  82. Another way that you can animate when using a sprite sheet is to use a standard
  83. :ref:`Sprite <class_Sprite>` node to display the texture, and then animating the
  84. change from texture to texture with :ref:`AnimationPlayer <class_AnimationPlayer>`.
  85. Consider this sprite sheet, which contains 6 frames of animation:
  86. .. image:: img/2d_animation_player-run.png
  87. Right-click the image and choose "Save Image As" to download, then copy the
  88. image into your project folder.
  89. Our goal is to display these images one after another in a loop. Start by
  90. setting up your scene tree:
  91. .. image:: img/2d_animation_tree2.png
  92. .. note:: The root node could also be :ref:`Area2D <class_Area2D>` or
  93. :ref:`RigidBody2D <class_RigidBody2D>`. The animation will still be
  94. made in the same way. Once the animation is completed, you can
  95. assign a shape to the CollisionShape2D. See
  96. :ref:`Physics Introduction <doc_physics_introduction>` for more
  97. information.
  98. Drag the spritesheet into the Sprite's *Texture* property, and you'll see the
  99. whole sheet displayed on the screen. To slice it up into individual frames,
  100. expand the *Animation* section in the Inspector and set the *Hframes* to ``6``.
  101. *Hframes* and *Vframes* are the number of horizontal and vertical frames in
  102. your sprite sheet.
  103. .. image:: img/2d_animation_setframes.png
  104. Now try changing the value of the *Frame* property. You'll see that it ranges
  105. from ``0`` to ``5`` and the image displayed by the Sprite changes accordingly.
  106. This is the property we'll be animating.
  107. Select the ``AnimationPlayer`` and click the "Animation" button followed by
  108. "New". Name the new animation "walk". Set the animation length to ``0.6`` and
  109. click the "Loop" button so that our animation will repeat.
  110. .. image:: img/2d_animation_new_animation.png
  111. Now select the ``Sprite`` node and click the key icon to add a new track.
  112. .. image:: img/2d_animation_new_track.png
  113. Continue adding frames at each point in the timeline (``0.1`` seconds by
  114. default), until you have all the frames from 0 to 5. You'll see the frames
  115. actually appearing in the animation track:
  116. .. image:: img/2d_animation_full_animation.png
  117. Press "Play" on the animation to see how it looks.
  118. .. image:: img/2d_animation_running.gif
  119. Controlling an AnimationPlayer animation
  120. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  121. Like with AnimatedSprite, you can control the animation via code using
  122. the ``play()`` and ``stop()`` methods. Again, here is an example to play the
  123. animation while the right arrow key is held, and stop it when the key is
  124. released.
  125. .. tabs::
  126. .. code-tab:: gdscript GDScript
  127. extends KinematicBody2D
  128. func _process(delta):
  129. if Input.is_action_pressed("ui_right"):
  130. $AnimationPlayer.play("walk")
  131. else:
  132. $AnimationPlayer.stop()
  133. .. note:: If updating both an animation and a separate property at once
  134. (for example, a platformer may update the sprite's ``h_flip``/``v_flip``
  135. properties when a character turns while starting a 'turning' animation),
  136. it's important to keep in mind that ``play()`` isn't applied instantly.
  137. Instead, it's applied the next time the :ref:`AnimationPlayer <class_AnimationPlayer>` is processed.
  138. This may end up being on the next frame, causing a 'glitch' frame,
  139. where the property change was applied but the animation was not.
  140. If this turns out to be a problem, after calling ``play()``, you can call ``advance(0)``
  141. to update the animation immediately.
  142. Summary
  143. -------
  144. These examples illustrate the two classes you can use in Godot for
  145. 2D animation. ``AnimationPlayer`` is
  146. a bit more complex than ``AnimatedSprite``, but it provides additional functionality, since you can also
  147. 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.