class_node2d.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the Node2D.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_Node2D:
  6. Node2D
  7. ======
  8. **Inherits:** :ref:`CanvasItem<class_CanvasItem>` **<** :ref:`Node<class_Node>` **<** :ref:`Object<class_Object>`
  9. **Inherited By:** :ref:`AnimatedSprite2D<class_AnimatedSprite2D>`, :ref:`AudioListener2D<class_AudioListener2D>`, :ref:`AudioStreamPlayer2D<class_AudioStreamPlayer2D>`, :ref:`BackBufferCopy<class_BackBufferCopy>`, :ref:`Bone2D<class_Bone2D>`, :ref:`CPUParticles2D<class_CPUParticles2D>`, :ref:`Camera2D<class_Camera2D>`, :ref:`CanvasGroup<class_CanvasGroup>`, :ref:`CanvasModulate<class_CanvasModulate>`, :ref:`CollisionObject2D<class_CollisionObject2D>`, :ref:`CollisionPolygon2D<class_CollisionPolygon2D>`, :ref:`CollisionShape2D<class_CollisionShape2D>`, :ref:`GPUParticles2D<class_GPUParticles2D>`, :ref:`Joint2D<class_Joint2D>`, :ref:`Light2D<class_Light2D>`, :ref:`LightOccluder2D<class_LightOccluder2D>`, :ref:`Line2D<class_Line2D>`, :ref:`MeshInstance2D<class_MeshInstance2D>`, :ref:`MultiMeshInstance2D<class_MultiMeshInstance2D>`, :ref:`NavigationRegion2D<class_NavigationRegion2D>`, :ref:`ParallaxLayer<class_ParallaxLayer>`, :ref:`Path2D<class_Path2D>`, :ref:`PathFollow2D<class_PathFollow2D>`, :ref:`Polygon2D<class_Polygon2D>`, :ref:`Position2D<class_Position2D>`, :ref:`RayCast2D<class_RayCast2D>`, :ref:`RemoteTransform2D<class_RemoteTransform2D>`, :ref:`ShapeCast2D<class_ShapeCast2D>`, :ref:`Skeleton2D<class_Skeleton2D>`, :ref:`Sprite2D<class_Sprite2D>`, :ref:`TileMap<class_TileMap>`, :ref:`TouchScreenButton<class_TouchScreenButton>`, :ref:`VisibleOnScreenNotifier2D<class_VisibleOnScreenNotifier2D>`
  10. A 2D game object, inherited by all 2D-related nodes. Has a position, rotation, scale, and Z index.
  11. Description
  12. -----------
  13. A 2D game object, with a transform (position, rotation, and scale). All 2D nodes, including physics objects and sprites, inherit from Node2D. Use Node2D as a parent node to move, scale and rotate children in a 2D project. Also gives control of the node's render order.
  14. Tutorials
  15. ---------
  16. - :doc:`Custom drawing in 2D <../tutorials/2d/custom_drawing_in_2d>`
  17. - `All 2D Demos <https://github.com/godotengine/godot-demo-projects/tree/master/2d>`__
  18. Properties
  19. ----------
  20. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  21. | :ref:`Vector2<class_Vector2>` | :ref:`global_position<class_Node2D_property_global_position>` | |
  22. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  23. | :ref:`float<class_float>` | :ref:`global_rotation<class_Node2D_property_global_rotation>` | |
  24. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  25. | :ref:`Vector2<class_Vector2>` | :ref:`global_scale<class_Node2D_property_global_scale>` | |
  26. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  27. | :ref:`Transform2D<class_Transform2D>` | :ref:`global_transform<class_Node2D_property_global_transform>` | |
  28. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  29. | :ref:`Vector2<class_Vector2>` | :ref:`position<class_Node2D_property_position>` | ``Vector2(0, 0)`` |
  30. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  31. | :ref:`float<class_float>` | :ref:`rotation<class_Node2D_property_rotation>` | ``0.0`` |
  32. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  33. | :ref:`Vector2<class_Vector2>` | :ref:`scale<class_Node2D_property_scale>` | ``Vector2(1, 1)`` |
  34. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  35. | :ref:`float<class_float>` | :ref:`skew<class_Node2D_property_skew>` | ``0.0`` |
  36. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  37. | :ref:`Transform2D<class_Transform2D>` | :ref:`transform<class_Node2D_property_transform>` | |
  38. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  39. | :ref:`bool<class_bool>` | :ref:`y_sort_enabled<class_Node2D_property_y_sort_enabled>` | ``false`` |
  40. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  41. | :ref:`bool<class_bool>` | :ref:`z_as_relative<class_Node2D_property_z_as_relative>` | ``true`` |
  42. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  43. | :ref:`int<class_int>` | :ref:`z_index<class_Node2D_property_z_index>` | ``0`` |
  44. +---------------------------------------+-----------------------------------------------------------------+-------------------+
  45. Methods
  46. -------
  47. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | void | :ref:`apply_scale<class_Node2D_method_apply_scale>` **(** :ref:`Vector2<class_Vector2>` ratio **)** |
  49. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`float<class_float>` | :ref:`get_angle_to<class_Node2D_method_get_angle_to>` **(** :ref:`Vector2<class_Vector2>` point **)** |const| |
  51. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`Transform2D<class_Transform2D>` | :ref:`get_relative_transform_to_parent<class_Node2D_method_get_relative_transform_to_parent>` **(** :ref:`Node<class_Node>` parent **)** |const| |
  53. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | void | :ref:`global_translate<class_Node2D_method_global_translate>` **(** :ref:`Vector2<class_Vector2>` offset **)** |
  55. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | void | :ref:`look_at<class_Node2D_method_look_at>` **(** :ref:`Vector2<class_Vector2>` point **)** |
  57. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | void | :ref:`move_local_x<class_Node2D_method_move_local_x>` **(** :ref:`float<class_float>` delta, :ref:`bool<class_bool>` scaled=false **)** |
  59. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | void | :ref:`move_local_y<class_Node2D_method_move_local_y>` **(** :ref:`float<class_float>` delta, :ref:`bool<class_bool>` scaled=false **)** |
  61. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | void | :ref:`rotate<class_Node2D_method_rotate>` **(** :ref:`float<class_float>` radians **)** |
  63. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`Vector2<class_Vector2>` | :ref:`to_global<class_Node2D_method_to_global>` **(** :ref:`Vector2<class_Vector2>` local_point **)** |const| |
  65. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`Vector2<class_Vector2>` | :ref:`to_local<class_Node2D_method_to_local>` **(** :ref:`Vector2<class_Vector2>` global_point **)** |const| |
  67. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | void | :ref:`translate<class_Node2D_method_translate>` **(** :ref:`Vector2<class_Vector2>` offset **)** |
  69. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------+
  70. Property Descriptions
  71. ---------------------
  72. .. _class_Node2D_property_global_position:
  73. - :ref:`Vector2<class_Vector2>` **global_position**
  74. +----------+----------------------------+
  75. | *Setter* | set_global_position(value) |
  76. +----------+----------------------------+
  77. | *Getter* | get_global_position() |
  78. +----------+----------------------------+
  79. Global position.
  80. ----
  81. .. _class_Node2D_property_global_rotation:
  82. - :ref:`float<class_float>` **global_rotation**
  83. +----------+----------------------------+
  84. | *Setter* | set_global_rotation(value) |
  85. +----------+----------------------------+
  86. | *Getter* | get_global_rotation() |
  87. +----------+----------------------------+
  88. Global rotation in radians.
  89. ----
  90. .. _class_Node2D_property_global_scale:
  91. - :ref:`Vector2<class_Vector2>` **global_scale**
  92. +----------+-------------------------+
  93. | *Setter* | set_global_scale(value) |
  94. +----------+-------------------------+
  95. | *Getter* | get_global_scale() |
  96. +----------+-------------------------+
  97. Global scale.
  98. ----
  99. .. _class_Node2D_property_global_transform:
  100. - :ref:`Transform2D<class_Transform2D>` **global_transform**
  101. +----------+-----------------------------+
  102. | *Setter* | set_global_transform(value) |
  103. +----------+-----------------------------+
  104. | *Getter* | get_global_transform() |
  105. +----------+-----------------------------+
  106. Global :ref:`Transform2D<class_Transform2D>`.
  107. ----
  108. .. _class_Node2D_property_position:
  109. - :ref:`Vector2<class_Vector2>` **position**
  110. +-----------+---------------------+
  111. | *Default* | ``Vector2(0, 0)`` |
  112. +-----------+---------------------+
  113. | *Setter* | set_position(value) |
  114. +-----------+---------------------+
  115. | *Getter* | get_position() |
  116. +-----------+---------------------+
  117. Position, relative to the node's parent.
  118. ----
  119. .. _class_Node2D_property_rotation:
  120. - :ref:`float<class_float>` **rotation**
  121. +-----------+---------------------+
  122. | *Default* | ``0.0`` |
  123. +-----------+---------------------+
  124. | *Setter* | set_rotation(value) |
  125. +-----------+---------------------+
  126. | *Getter* | get_rotation() |
  127. +-----------+---------------------+
  128. Rotation in radians, relative to the node's parent.
  129. ----
  130. .. _class_Node2D_property_scale:
  131. - :ref:`Vector2<class_Vector2>` **scale**
  132. +-----------+-------------------+
  133. | *Default* | ``Vector2(1, 1)`` |
  134. +-----------+-------------------+
  135. | *Setter* | set_scale(value) |
  136. +-----------+-------------------+
  137. | *Getter* | get_scale() |
  138. +-----------+-------------------+
  139. The node's scale. Unscaled value: ``(1, 1)``.
  140. ----
  141. .. _class_Node2D_property_skew:
  142. - :ref:`float<class_float>` **skew**
  143. +-----------+-----------------+
  144. | *Default* | ``0.0`` |
  145. +-----------+-----------------+
  146. | *Setter* | set_skew(value) |
  147. +-----------+-----------------+
  148. | *Getter* | get_skew() |
  149. +-----------+-----------------+
  150. ----
  151. .. _class_Node2D_property_transform:
  152. - :ref:`Transform2D<class_Transform2D>` **transform**
  153. +----------+----------------------+
  154. | *Setter* | set_transform(value) |
  155. +----------+----------------------+
  156. | *Getter* | get_transform() |
  157. +----------+----------------------+
  158. Local :ref:`Transform2D<class_Transform2D>`.
  159. ----
  160. .. _class_Node2D_property_y_sort_enabled:
  161. - :ref:`bool<class_bool>` **y_sort_enabled**
  162. +-----------+---------------------------+
  163. | *Default* | ``false`` |
  164. +-----------+---------------------------+
  165. | *Setter* | set_y_sort_enabled(value) |
  166. +-----------+---------------------------+
  167. | *Getter* | is_y_sort_enabled() |
  168. +-----------+---------------------------+
  169. If ``true``, child nodes with the lowest Y position are drawn before those with a higher Y position. If ``false``, Y-sorting is disabled. Y-sorting only affects children that inherit from :ref:`CanvasItem<class_CanvasItem>`.
  170. You can nest nodes with Y-sorting. Child Y-sorted nodes are sorted in the same space as the parent Y-sort. This feature allows you to organize a scene better or divide it into multiple ones without changing your scene tree.
  171. ----
  172. .. _class_Node2D_property_z_as_relative:
  173. - :ref:`bool<class_bool>` **z_as_relative**
  174. +-----------+--------------------------+
  175. | *Default* | ``true`` |
  176. +-----------+--------------------------+
  177. | *Setter* | set_z_as_relative(value) |
  178. +-----------+--------------------------+
  179. | *Getter* | is_z_relative() |
  180. +-----------+--------------------------+
  181. If ``true``, the node's Z index is relative to its parent's Z index. If this node's Z index is 2 and its parent's effective Z index is 3, then this node's effective Z index will be 2 + 3 = 5.
  182. ----
  183. .. _class_Node2D_property_z_index:
  184. - :ref:`int<class_int>` **z_index**
  185. +-----------+--------------------+
  186. | *Default* | ``0`` |
  187. +-----------+--------------------+
  188. | *Setter* | set_z_index(value) |
  189. +-----------+--------------------+
  190. | *Getter* | get_z_index() |
  191. +-----------+--------------------+
  192. Z index. Controls the order in which the nodes render. A node with a higher Z index will display in front of others. Must be between :ref:`RenderingServer.CANVAS_ITEM_Z_MIN<class_RenderingServer_constant_CANVAS_ITEM_Z_MIN>` and :ref:`RenderingServer.CANVAS_ITEM_Z_MAX<class_RenderingServer_constant_CANVAS_ITEM_Z_MAX>` (inclusive).
  193. Method Descriptions
  194. -------------------
  195. .. _class_Node2D_method_apply_scale:
  196. - void **apply_scale** **(** :ref:`Vector2<class_Vector2>` ratio **)**
  197. Multiplies the current scale by the ``ratio`` vector.
  198. ----
  199. .. _class_Node2D_method_get_angle_to:
  200. - :ref:`float<class_float>` **get_angle_to** **(** :ref:`Vector2<class_Vector2>` point **)** |const|
  201. Returns the angle between the node and the ``point`` in radians.
  202. `Illustration of the returned angle. <https://raw.githubusercontent.com/godotengine/godot-docs/master/img/node2d_get_angle_to.png>`__
  203. ----
  204. .. _class_Node2D_method_get_relative_transform_to_parent:
  205. - :ref:`Transform2D<class_Transform2D>` **get_relative_transform_to_parent** **(** :ref:`Node<class_Node>` parent **)** |const|
  206. Returns the :ref:`Transform2D<class_Transform2D>` relative to this node's parent.
  207. ----
  208. .. _class_Node2D_method_global_translate:
  209. - void **global_translate** **(** :ref:`Vector2<class_Vector2>` offset **)**
  210. Adds the ``offset`` vector to the node's global position.
  211. ----
  212. .. _class_Node2D_method_look_at:
  213. - void **look_at** **(** :ref:`Vector2<class_Vector2>` point **)**
  214. Rotates the node so it points towards the ``point``, which is expected to use global coordinates.
  215. ----
  216. .. _class_Node2D_method_move_local_x:
  217. - void **move_local_x** **(** :ref:`float<class_float>` delta, :ref:`bool<class_bool>` scaled=false **)**
  218. Applies a local translation on the node's X axis based on the :ref:`Node._process<class_Node_method__process>`'s ``delta``. If ``scaled`` is ``false``, normalizes the movement.
  219. ----
  220. .. _class_Node2D_method_move_local_y:
  221. - void **move_local_y** **(** :ref:`float<class_float>` delta, :ref:`bool<class_bool>` scaled=false **)**
  222. Applies a local translation on the node's Y axis based on the :ref:`Node._process<class_Node_method__process>`'s ``delta``. If ``scaled`` is ``false``, normalizes the movement.
  223. ----
  224. .. _class_Node2D_method_rotate:
  225. - void **rotate** **(** :ref:`float<class_float>` radians **)**
  226. Applies a rotation to the node, in radians, starting from its current rotation.
  227. ----
  228. .. _class_Node2D_method_to_global:
  229. - :ref:`Vector2<class_Vector2>` **to_global** **(** :ref:`Vector2<class_Vector2>` local_point **)** |const|
  230. Transforms the provided local position into a position in global coordinate space. The input is expected to be local relative to the ``Node2D`` it is called on. e.g. Applying this method to the positions of child nodes will correctly transform their positions into the global coordinate space, but applying it to a node's own position will give an incorrect result, as it will incorporate the node's own transformation into its global position.
  231. ----
  232. .. _class_Node2D_method_to_local:
  233. - :ref:`Vector2<class_Vector2>` **to_local** **(** :ref:`Vector2<class_Vector2>` global_point **)** |const|
  234. Transforms the provided global position into a position in local coordinate space. The output will be local relative to the ``Node2D`` it is called on. e.g. It is appropriate for determining the positions of child nodes, but it is not appropriate for determining its own position relative to its parent.
  235. ----
  236. .. _class_Node2D_method_translate:
  237. - void **translate** **(** :ref:`Vector2<class_Vector2>` offset **)**
  238. Translates the node by the given ``offset`` in local coordinates.
  239. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  240. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  241. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  242. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  243. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  244. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`