2d_sprite_animation.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. .. _doc_2d_sprite_animation:
  2. 2D Sprite animation
  3. ===================
  4. Introduction
  5. ------------
  6. In this tutorial, you'll learn two different ways to create 2D animated
  7. characters. 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. Depending on which type of assets you
  10. have, you can choose one of the following solutions.
  11. First, we'll use :ref:`AnimatedSprite <class_AnimatedSprite>` to
  12. animate a collection of individual images. Then, to use a sprite sheet, we'll
  13. use :ref:`AnimationPlayer <class_AnimationPlayer>` along with the *Animation*
  14. property of :ref:`Sprite <class_Sprite>`.
  15. .. note:: Art for the following examples by https://opengameart.org/users/ansimuz
  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.
  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 AnimationPlayer
  63. ---------------------------------
  64. In the event you have a sprite sheet containing all of your animation frames,
  65. you can't easily use ``AnimatedSprite``. Instead, you can use a standard
  66. :ref:`Sprite <class_Sprite>` node to display the texture, and then animate the
  67. change from texture to texture with :ref:`AnimationPlayer <class_AnimationPlayer>`.
  68. Consider this sprite sheet, which contains 6 frames of animation:
  69. .. image:: img/2d_animation_player-run.png
  70. Right-click the image and choose "Save Image As" to download, then copy the
  71. image into your project folder.
  72. Our goal is to display these images one after another in a loop. Start by
  73. setting up your scene tree:
  74. .. image:: img/2d_animation_tree2.png
  75. .. note:: The root node could also be :ref:`Area2D <class_Area2D>` or
  76. :ref:`RigidBody2D <class_RigidBody2D>`. The animation will still be
  77. made in the same way. Once the animation is completed, you can
  78. assign a shape to the CollisionShape2D. See
  79. :ref:`Physics Introduction <doc_physics_introduction>` for more
  80. information.
  81. Drag the spritesheet into the Sprite's *Texture* property, and you'll see the
  82. whole sheet displayed on the screen. To slice it up into individual frames,
  83. expand the *Animation* section in the Inspector and set the *Hframes* to ``6``.
  84. *Hframes* and *Vframes* are the number of horizontal and vertical frames in
  85. your sprite sheet.
  86. .. image:: img/2d_animation_setframes.png
  87. Now try changing the value of the *Frame* property. You'll see that it ranges
  88. from ``0`` to ``5`` and the image displayed by the Sprite changes accordingly.
  89. This is the property we'll be animating.
  90. Select the ``AnimationPlayer`` and click the "Animation" button followed by
  91. "New". Name the new animation "walk". Set the animation length to ``0.6`` and
  92. click the "Loop" button so that our animation will repeat.
  93. .. image:: img/2d_animation_new_animation.png
  94. Now select the ``Sprite`` node and click the key icon to add a new track.
  95. .. image:: img/2d_animation_new_track.png
  96. Continue adding frames at each point in the timeline (``0.1`` seconds by
  97. default), until you have all the frames from 0 to 5. You'll see the frames
  98. actually appearing in the animation track:
  99. .. image:: img/2d_animation_full_animation.png
  100. Press "Play" on the animation to see how it looks.
  101. .. image:: img/2d_animation_running.gif
  102. Summary
  103. -------
  104. These examples illustrate the two most common situations you'll encounter in
  105. 2D animation. Each has its benefits. Working with ``AnimationPlayer`` is
  106. a bit more complex, but provides additional functionality, since you can also
  107. animate other properties like position or scale. Experiment and see which
  108. works best for your needs.