Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Node2D
繼承: CanvasItem < Node < Object
被繼承: AnimatedSprite2D, AudioListener2D, AudioStreamPlayer2D, BackBufferCopy, Bone2D, Camera2D, CanvasGroup, CanvasModulate, CollisionObject2D, CollisionPolygon2D, CollisionShape2D, CPUParticles2D, GPUParticles2D, Joint2D, Light2D, LightOccluder2D, Line2D, Marker2D, MeshInstance2D, MultiMeshInstance2D, NavigationLink2D, NavigationObstacle2D, NavigationRegion2D, Parallax2D, ParallaxLayer, Path2D, PathFollow2D, Polygon2D, RayCast2D, RemoteTransform2D, ShapeCast2D, Skeleton2D, Sprite2D, TileMap, TileMapLayer, TouchScreenButton, VisibleOnScreenNotifier2D
2D 遊戲物件,所有 2D 相關節點的父類。具有位置、旋轉、縮放和傾斜屬性。
說明
2D 遊戲物件,具有變換(位置、旋轉、縮放)。所有 2D 節點,包括物理物件與精靈,皆繼承自 Node2D。使用 Node2D 作為父節點,可在 2D 專案中移動、縮放與旋轉子節點,並控制節點的繪製順序。
注意: Node2D 與 Control 都繼承自 CanvasItem,因此共享 CanvasItem.z_index、CanvasItem.visible 等屬性與概念。
教學
屬性
|
||
|
||
|
||
|
||
方法
void |
apply_scale(ratio: Vector2) |
get_angle_to(point: Vector2) const |
|
get_relative_transform_to_parent(parent: Node) const |
|
void |
global_translate(offset: Vector2) |
void |
|
void |
move_local_x(delta: float, scaled: bool = false) |
void |
move_local_y(delta: float, scaled: bool = false) |
void |
|
void |
屬性說明
全域位置。另見 position。
全域旋轉(弧度)。另見 rotation。
float global_rotation_degrees 🔗
輔助屬性,可用角度(非弧度)存取 global_rotation。另見 rotation_degrees。
全域縮放。另見 scale。
全域傾斜(弧度)。另見 skew。
Transform2D global_transform 🔗
void set_global_transform(value: Transform2D)
Transform2D get_global_transform()
全域 Transform2D。另見 transform。
Vector2 position = Vector2(0, 0) 🔗
相對於父節點的位置。另見 global_position。
以弧度表示、相對於父節點的旋轉。另見 global_rotation。
注意: 此屬性在屬性面板中以角度方式編輯。如需在腳本中使用角度,請用 rotation_degrees。
輔助屬性,可用角度(非弧度)存取 rotation。另見 global_rotation_degrees。
Vector2 scale = Vector2(1, 1) 🔗
節點相對於父節點的縮放。未縮放值為 (1, 1)。另見 global_scale。
注意: 2D 下,變換矩陣無法分解出負 X 軸縮放。Godot 以變換矩陣儲存縮放,分解時 X 軸負縮放會變為 Y 軸負縮放並帶有 180 度旋轉。
若設定為非零值,節點會向一方傾斜。可用於偽 3D 效果。另見 global_skew。
注意: 傾斜只作用於 X 軸,且在旋轉與縮放 之間。
注意: 此屬性於屬性面板中以角度編輯。如需於腳本中以角度設定,請使用 skew = deg_to_rad(value_in_degrees)。
Transform2D transform 🔗
void set_transform(value: Transform2D)
Transform2D get_transform()
節點相對於父節點的 Transform2D。另見 global_transform。
方法說明
void apply_scale(ratio: Vector2) 🔗
將目前縮放值乘以 ratio 向量。
float get_angle_to(point: Vector2) const 🔗
Returns the angle between the node and the point in radians. See also look_at().
Illustration of the returned angle.
Transform2D get_relative_transform_to_parent(parent: Node) const 🔗
回傳相對於本節點父節點的 Transform2D。
void global_translate(offset: Vector2) 🔗
將 offset 向量加到節點的全域位置。
void look_at(point: Vector2) 🔗
Rotates the node so that its local +X axis points towards the point, which is expected to use global coordinates. This method is a combination of both rotate() and get_angle_to().
point should not be the same as the node's position, otherwise the node always looks to the right.
void move_local_x(delta: float, scaled: bool = false) 🔗
Applies a local translation on the node's X axis with the amount specified in delta. If scaled is false, normalizes the movement to occur independently of the node's scale.
void move_local_y(delta: float, scaled: bool = false) 🔗
在節點的 Y 軸上應用由 delta 指定的量進行局部平移。如果 scaled 為 false,則將正規化移動,使其與節點的 scale 無關。
Applies a rotation to the node, in radians, starting from its current rotation. This is equivalent to rotation += radians.
Vector2 to_global(local_point: Vector2) const 🔗
將給定的本地位置轉換為全域座標空間的位置。輸入必須是相對於該 Node2D 的本地座標。例如,對子節點的位置使用本方法可正確轉換為全域座標,但對自身位置使用則會因納入自己的變換而得到錯誤結果。
Vector2 to_local(global_point: Vector2) const 🔗
將給定的全域座標位置轉換為本地座標空間的位置。輸出為相對於本 Node2D 的本地座標。適合用於取得子節點的位置,但不適合用來取得自身相對於父節點的位置。
void translate(offset: Vector2) 🔗
Translates the node by the given offset in local coordinates. This is equivalent to position += offset.