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.

Sprite2D

Hérite de : Node2D < CanvasItem < Node < Object

Nœud de sprite à usage général.

Description

Un nœud qui affiche une texture 2D. La texture affichée peut être une région à partir d'une texture plus grande de l'atlas, ou d'une trame d'une animation de feuille de sprite.

Tutoriels

Propriétés

bool

centered

true

bool

flip_h

false

bool

flip_v

false

int

frame

0

Vector2i

frame_coords

Vector2i(0, 0)

int

hframes

1

Vector2

offset

Vector2(0, 0)

bool

region_enabled

false

bool

region_filter_clip_enabled

false

Rect2

region_rect

Rect2(0, 0, 0, 0)

Texture2D

texture

int

vframes

1

Méthodes

Rect2

get_rect() const

bool

is_pixel_opaque(pos: Vector2) const


Signaux

frame_changed() 🔗

Émis quand une frame changes.


texture_changed() 🔗

Émis quand une texture change.


Descriptions des propriétés

bool centered = true 🔗

  • void set_centered(value: bool)

  • bool is_centered()

Si true, la texture est centrée.

Note : Pour les jeux avec une esthétique pixel art, les textures peuvent apparaître déformées lorsqu'elles sont centrées. Ceci est causé par leur position se trouvant entre les pixels. Pour éviter cela, définissez cette propriété à false, ou envisagez d'activer ProjectSettings.rendering/2d/snap/snap_2d_vertices_to_pixel et ProjectSettings.rendering/2d/snap/snap_2d_transforms_to_pixel.


bool flip_h = false 🔗

  • void set_flip_h(value: bool)

  • bool is_flipped_h()

Si true, la texture est inversée horizontalement.


bool flip_v = false 🔗

  • void set_flip_v(value: bool)

  • bool is_flipped_v()

Si true, la texture est inversée verticalement.


int frame = 0 🔗

  • void set_frame(value: int)

  • int get_frame()

Trame actuelle à afficher à partir de la feuille de sprite. hframes ou vframes doivent être supérieurs à 1. Cette propriété est automatiquement rajustée lorsque hframes ou vframes sont modifiés pour garder le pointage sur la même trame visuelle (même colonne et rangée). Si c'est impossible, cette valeur est réinitialisée à 0.


Vector2i frame_coords = Vector2i(0, 0) 🔗

Les coordonnées de la trame à afficher de la feuille de sprite. Il s'agit d'un raccourci de la propriété frame. hframes ou vframes doivent être supérieurs à 1.


int hframes = 1 🔗

  • void set_hframes(value: int)

  • int get_hframes()

The number of columns in the sprite sheet. When this property is changed, frame is adjusted so that the same visual frame is maintained (same row and column). If that's impossible, frame is reset to 0.


Vector2 offset = Vector2(0, 0) 🔗

Le décalage de dessin de la texture.

Note : Lorsque vous augmentez offset.y dans Sprite2D, le sprite se déplace vers le bas à l'écran (c.-à-d. +Y pointe vers le bas).


bool region_enabled = false 🔗

  • void set_region_enabled(value: bool)

  • bool is_region_enabled()

If true, texture is cut from a larger atlas texture. See region_rect.

Note: When using a custom Shader on a Sprite2D, the UV shader built-in will refer to the entire texture space. Use the REGION_RECT built-in to get the currently visible region defined in region_rect instead. See CanvasItem shaders for details.


bool region_filter_clip_enabled = false 🔗

  • void set_region_filter_clip_enabled(value: bool)

  • bool is_region_filter_clip_enabled()

If true, the area outside of the region_rect is clipped to avoid bleeding of the surrounding texture pixels. region_enabled must be true.


Rect2 region_rect = Rect2(0, 0, 0, 0) 🔗

  • void set_region_rect(value: Rect2)

  • Rect2 get_region_rect()

La région de la texture atlas à afficher. region_enabled doit être true.


Texture2D texture 🔗

L’objet Texture2D à dessiner.


int vframes = 1 🔗

  • void set_vframes(value: int)

  • int get_vframes()

The number of rows in the sprite sheet. When this property is changed, frame is adjusted so that the same visual frame is maintained (same row and column). If that's impossible, frame is reset to 0.


Descriptions des méthodes

Rect2 get_rect() const 🔗

Renvoie un Rect2 représentant la bordure du Sprite2D dans les coordonnées locales.

Exemple : Détecter si le Sprite2D a été cliqué :

func _input(event):
    if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
        if get_rect().has_point(to_local(event.position)):
            print("Un clic !")

bool is_pixel_opaque(pos: Vector2) const 🔗

Returns true if the pixel at the given position is opaque, false otherwise. Also returns false if the given position is out of bounds or this sprite's texture is null. pos is in local coordinates.