class_sprite2d.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/Sprite2D.xml.
  6. .. _class_Sprite2D:
  7. Sprite2D
  8. ========
  9. **Inherits:** :ref:`Node2D<class_Node2D>` **<** :ref:`CanvasItem<class_CanvasItem>` **<** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
  10. General-purpose sprite node.
  11. Description
  12. -----------
  13. A node that displays a 2D texture. The texture displayed can be a region from a larger atlas texture, or a frame from a sprite sheet animation.
  14. Tutorials
  15. ---------
  16. - `Instancing Demo <https://godotengine.org/asset-library/asset/148>`__
  17. Properties
  18. ----------
  19. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  20. | :ref:`bool<class_bool>` | :ref:`centered<class_Sprite2D_property_centered>` | ``true`` |
  21. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  22. | :ref:`bool<class_bool>` | :ref:`flip_h<class_Sprite2D_property_flip_h>` | ``false`` |
  23. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  24. | :ref:`bool<class_bool>` | :ref:`flip_v<class_Sprite2D_property_flip_v>` | ``false`` |
  25. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  26. | :ref:`int<class_int>` | :ref:`frame<class_Sprite2D_property_frame>` | ``0`` |
  27. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  28. | :ref:`Vector2i<class_Vector2i>` | :ref:`frame_coords<class_Sprite2D_property_frame_coords>` | ``Vector2i(0, 0)`` |
  29. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  30. | :ref:`int<class_int>` | :ref:`hframes<class_Sprite2D_property_hframes>` | ``1`` |
  31. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  32. | :ref:`Vector2<class_Vector2>` | :ref:`offset<class_Sprite2D_property_offset>` | ``Vector2(0, 0)`` |
  33. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  34. | :ref:`bool<class_bool>` | :ref:`region_enabled<class_Sprite2D_property_region_enabled>` | ``false`` |
  35. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  36. | :ref:`bool<class_bool>` | :ref:`region_filter_clip_enabled<class_Sprite2D_property_region_filter_clip_enabled>` | ``false`` |
  37. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  38. | :ref:`Rect2<class_Rect2>` | :ref:`region_rect<class_Sprite2D_property_region_rect>` | ``Rect2(0, 0, 0, 0)`` |
  39. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  40. | :ref:`Texture2D<class_Texture2D>` | :ref:`texture<class_Sprite2D_property_texture>` | |
  41. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  42. | :ref:`int<class_int>` | :ref:`vframes<class_Sprite2D_property_vframes>` | ``1`` |
  43. +-----------------------------------+---------------------------------------------------------------------------------------+-----------------------+
  44. Methods
  45. -------
  46. +---------------------------+---------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`Rect2<class_Rect2>` | :ref:`get_rect<class_Sprite2D_method_get_rect>` **(** **)** |const| |
  48. +---------------------------+---------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`bool<class_bool>` | :ref:`is_pixel_opaque<class_Sprite2D_method_is_pixel_opaque>` **(** :ref:`Vector2<class_Vector2>` pos **)** |const| |
  50. +---------------------------+---------------------------------------------------------------------------------------------------------------------+
  51. Signals
  52. -------
  53. .. _class_Sprite2D_signal_frame_changed:
  54. - **frame_changed** **(** **)**
  55. Emitted when the :ref:`frame<class_Sprite2D_property_frame>` changes.
  56. ----
  57. .. _class_Sprite2D_signal_texture_changed:
  58. - **texture_changed** **(** **)**
  59. Emitted when the :ref:`texture<class_Sprite2D_property_texture>` changes.
  60. Property Descriptions
  61. ---------------------
  62. .. _class_Sprite2D_property_centered:
  63. - :ref:`bool<class_bool>` **centered**
  64. +-----------+---------------------+
  65. | *Default* | ``true`` |
  66. +-----------+---------------------+
  67. | *Setter* | set_centered(value) |
  68. +-----------+---------------------+
  69. | *Getter* | is_centered() |
  70. +-----------+---------------------+
  71. If ``true``, texture is centered.
  72. ----
  73. .. _class_Sprite2D_property_flip_h:
  74. - :ref:`bool<class_bool>` **flip_h**
  75. +-----------+-------------------+
  76. | *Default* | ``false`` |
  77. +-----------+-------------------+
  78. | *Setter* | set_flip_h(value) |
  79. +-----------+-------------------+
  80. | *Getter* | is_flipped_h() |
  81. +-----------+-------------------+
  82. If ``true``, texture is flipped horizontally.
  83. ----
  84. .. _class_Sprite2D_property_flip_v:
  85. - :ref:`bool<class_bool>` **flip_v**
  86. +-----------+-------------------+
  87. | *Default* | ``false`` |
  88. +-----------+-------------------+
  89. | *Setter* | set_flip_v(value) |
  90. +-----------+-------------------+
  91. | *Getter* | is_flipped_v() |
  92. +-----------+-------------------+
  93. If ``true``, texture is flipped vertically.
  94. ----
  95. .. _class_Sprite2D_property_frame:
  96. - :ref:`int<class_int>` **frame**
  97. +-----------+------------------+
  98. | *Default* | ``0`` |
  99. +-----------+------------------+
  100. | *Setter* | set_frame(value) |
  101. +-----------+------------------+
  102. | *Getter* | get_frame() |
  103. +-----------+------------------+
  104. Current frame to display from sprite sheet. :ref:`hframes<class_Sprite2D_property_hframes>` or :ref:`vframes<class_Sprite2D_property_vframes>` must be greater than 1.
  105. ----
  106. .. _class_Sprite2D_property_frame_coords:
  107. - :ref:`Vector2i<class_Vector2i>` **frame_coords**
  108. +-----------+-------------------------+
  109. | *Default* | ``Vector2i(0, 0)`` |
  110. +-----------+-------------------------+
  111. | *Setter* | set_frame_coords(value) |
  112. +-----------+-------------------------+
  113. | *Getter* | get_frame_coords() |
  114. +-----------+-------------------------+
  115. Coordinates of the frame to display from sprite sheet. This is as an alias for the :ref:`frame<class_Sprite2D_property_frame>` property. :ref:`hframes<class_Sprite2D_property_hframes>` or :ref:`vframes<class_Sprite2D_property_vframes>` must be greater than 1.
  116. ----
  117. .. _class_Sprite2D_property_hframes:
  118. - :ref:`int<class_int>` **hframes**
  119. +-----------+--------------------+
  120. | *Default* | ``1`` |
  121. +-----------+--------------------+
  122. | *Setter* | set_hframes(value) |
  123. +-----------+--------------------+
  124. | *Getter* | get_hframes() |
  125. +-----------+--------------------+
  126. The number of columns in the sprite sheet.
  127. ----
  128. .. _class_Sprite2D_property_offset:
  129. - :ref:`Vector2<class_Vector2>` **offset**
  130. +-----------+-------------------+
  131. | *Default* | ``Vector2(0, 0)`` |
  132. +-----------+-------------------+
  133. | *Setter* | set_offset(value) |
  134. +-----------+-------------------+
  135. | *Getter* | get_offset() |
  136. +-----------+-------------------+
  137. The texture's drawing offset.
  138. ----
  139. .. _class_Sprite2D_property_region_enabled:
  140. - :ref:`bool<class_bool>` **region_enabled**
  141. +-----------+---------------------------+
  142. | *Default* | ``false`` |
  143. +-----------+---------------------------+
  144. | *Setter* | set_region_enabled(value) |
  145. +-----------+---------------------------+
  146. | *Getter* | is_region_enabled() |
  147. +-----------+---------------------------+
  148. If ``true``, texture is cut from a larger atlas texture. See :ref:`region_rect<class_Sprite2D_property_region_rect>`.
  149. ----
  150. .. _class_Sprite2D_property_region_filter_clip_enabled:
  151. - :ref:`bool<class_bool>` **region_filter_clip_enabled**
  152. +-----------+---------------------------------------+
  153. | *Default* | ``false`` |
  154. +-----------+---------------------------------------+
  155. | *Setter* | set_region_filter_clip_enabled(value) |
  156. +-----------+---------------------------------------+
  157. | *Getter* | is_region_filter_clip_enabled() |
  158. +-----------+---------------------------------------+
  159. If ``true``, the outermost pixels get blurred out. :ref:`region_enabled<class_Sprite2D_property_region_enabled>` must be ``true``.
  160. ----
  161. .. _class_Sprite2D_property_region_rect:
  162. - :ref:`Rect2<class_Rect2>` **region_rect**
  163. +-----------+------------------------+
  164. | *Default* | ``Rect2(0, 0, 0, 0)`` |
  165. +-----------+------------------------+
  166. | *Setter* | set_region_rect(value) |
  167. +-----------+------------------------+
  168. | *Getter* | get_region_rect() |
  169. +-----------+------------------------+
  170. The region of the atlas texture to display. :ref:`region_enabled<class_Sprite2D_property_region_enabled>` must be ``true``.
  171. ----
  172. .. _class_Sprite2D_property_texture:
  173. - :ref:`Texture2D<class_Texture2D>` **texture**
  174. +----------+--------------------+
  175. | *Setter* | set_texture(value) |
  176. +----------+--------------------+
  177. | *Getter* | get_texture() |
  178. +----------+--------------------+
  179. :ref:`Texture2D<class_Texture2D>` object to draw.
  180. ----
  181. .. _class_Sprite2D_property_vframes:
  182. - :ref:`int<class_int>` **vframes**
  183. +-----------+--------------------+
  184. | *Default* | ``1`` |
  185. +-----------+--------------------+
  186. | *Setter* | set_vframes(value) |
  187. +-----------+--------------------+
  188. | *Getter* | get_vframes() |
  189. +-----------+--------------------+
  190. The number of rows in the sprite sheet.
  191. Method Descriptions
  192. -------------------
  193. .. _class_Sprite2D_method_get_rect:
  194. - :ref:`Rect2<class_Rect2>` **get_rect** **(** **)** |const|
  195. Returns a :ref:`Rect2<class_Rect2>` representing the Sprite2D's boundary in local coordinates. Can be used to detect if the Sprite2D was clicked. Example:
  196. .. tabs::
  197. .. code-tab:: gdscript
  198. func _input(event):
  199. if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
  200. if get_rect().has_point(to_local(event.position)):
  201. print("A click!")
  202. .. code-tab:: csharp
  203. public override void _Input(InputEvent inputEvent)
  204. {
  205. if (inputEvent is InputEventMouseButton inputEventMouse)
  206. {
  207. if (inputEventMouse.Pressed && inputEventMouse.ButtonIndex == (int)ButtonList.Left)
  208. {
  209. if (GetRect().HasPoint(ToLocal(inputEventMouse.Position)))
  210. {
  211. GD.Print("A click!");
  212. }
  213. }
  214. }
  215. }
  216. ----
  217. .. _class_Sprite2D_method_is_pixel_opaque:
  218. - :ref:`bool<class_bool>` **is_pixel_opaque** **(** :ref:`Vector2<class_Vector2>` pos **)** |const|
  219. Returns ``true``, if the pixel at the given position is opaque and ``false`` in other case.
  220. \ **Note:** It also returns ``false``, if the sprite's texture is ``null`` or if the given position is invalid.
  221. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  222. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  223. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  224. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  225. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  226. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`