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.

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 專案中移動、縮放與旋轉子節點,並控制節點的繪製順序。

注意: Node2DControl 都繼承自 CanvasItem,因此共享 CanvasItem.z_indexCanvasItem.visible 等屬性與概念。

教學

屬性

Vector2

global_position

float

global_rotation

float

global_rotation_degrees

Vector2

global_scale

float

global_skew

Transform2D

global_transform

Vector2

position

Vector2(0, 0)

float

rotation

0.0

float

rotation_degrees

Vector2

scale

Vector2(1, 1)

float

skew

0.0

Transform2D

transform

方法

void

apply_scale(ratio: Vector2)

float

get_angle_to(point: Vector2) const

Transform2D

get_relative_transform_to_parent(parent: Node) const

void

global_translate(offset: Vector2)

void

look_at(point: Vector2)

void

move_local_x(delta: float, scaled: bool = false)

void

move_local_y(delta: float, scaled: bool = false)

void

rotate(radians: float)

Vector2

to_global(local_point: Vector2) const

Vector2

to_local(global_point: Vector2) const

void

translate(offset: Vector2)


屬性說明

Vector2 global_position 🔗

  • void set_global_position(value: Vector2)

  • Vector2 get_global_position()

全域位置。另見 position


float global_rotation 🔗

  • void set_global_rotation(value: float)

  • float get_global_rotation()

全域旋轉(弧度)。另見 rotation


float global_rotation_degrees 🔗

  • void set_global_rotation_degrees(value: float)

  • float get_global_rotation_degrees()

輔助屬性,可用角度(非弧度)存取 global_rotation。另見 rotation_degrees


Vector2 global_scale 🔗

全域縮放。另見 scale


float global_skew 🔗

  • void set_global_skew(value: float)

  • float get_global_skew()

全域傾斜(弧度)。另見 skew


Transform2D global_transform 🔗

全域 Transform2D。另見 transform


Vector2 position = Vector2(0, 0) 🔗

相對於父節點的位置。另見 global_position


float rotation = 0.0 🔗

  • void set_rotation(value: float)

  • float get_rotation()

以弧度表示、相對於父節點的旋轉。另見 global_rotation

注意: 此屬性在屬性面板中以角度方式編輯。如需在腳本中使用角度,請用 rotation_degrees


float rotation_degrees 🔗

  • void set_rotation_degrees(value: float)

  • float get_rotation_degrees()

輔助屬性,可用角度(非弧度)存取 rotation。另見 global_rotation_degrees


Vector2 scale = Vector2(1, 1) 🔗

節點相對於父節點的縮放。未縮放值為 (1, 1)。另見 global_scale

注意: 2D 下,變換矩陣無法分解出負 X 軸縮放。Godot 以變換矩陣儲存縮放,分解時 X 軸負縮放會變為 Y 軸負縮放並帶有 180 度旋轉。


float skew = 0.0 🔗

若設定為非零值,節點會向一方傾斜。可用於偽 3D 效果。另見 global_skew

注意: 傾斜只作用於 X 軸,且在旋轉與縮放 之間

注意: 此屬性於屬性面板中以角度編輯。如需於腳本中以角度設定,請使用 skew = deg_to_rad(value_in_degrees)


Transform2D 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 指定的量進行局部平移。如果 scaledfalse,則將正規化移動,使其與節點的 scale 無關。


void rotate(radians: float) 🔗

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.