Sprite2D

Inherits:Node2D<CanvasItem<Node<Object

General-purpose sprite node.

Description

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.

Tutorials

Properties

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

Methods

Rect2

get_rect()const

bool

is_pixel_opaque(pos:Vector2)const


Signals

frame_changed()🔗

Emitted when theframe changes.


texture_changed()🔗

Emitted when thetexture changes.


Property Descriptions

boolcentered =true🔗

Iftrue, texture is centered.

Note: For games with a pixel art aesthetic, textures may appear deformed when centered. This is caused by their position being between pixels. To prevent this, set this property tofalse, or consider enablingProjectSettings.rendering/2d/snap/snap_2d_vertices_to_pixel andProjectSettings.rendering/2d/snap/snap_2d_transforms_to_pixel.


boolflip_h =false🔗

Iftrue, texture is flipped horizontally.


boolflip_v =false🔗

Iftrue, texture is flipped vertically.


intframe =0🔗

Current frame to display from sprite sheet.hframes orvframes must be greater than 1. This property is automatically adjusted whenhframes orvframes are changed to keep pointing to the same visual frame (same column and row). If that's impossible, this value is reset to0.


Vector2iframe_coords =Vector2i(0,0)🔗

Coordinates of the frame to display from sprite sheet. This is as an alias for theframe property.hframes orvframes must be greater than 1.


inthframes =1🔗

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 to0.


Vector2offset =Vector2(0,0)🔗

The texture's drawing offset.


boolregion_enabled =false🔗

Iftrue, texture is cut from a larger atlas texture. Seeregion_rect.


boolregion_filter_clip_enabled =false🔗

Iftrue, the area outside of theregion_rect is clipped to avoid bleeding of the surrounding texture pixels.region_enabled must betrue.


Rect2region_rect =Rect2(0,0,0,0)🔗

The region of the atlas texture to display.region_enabled must betrue.


Texture2Dtexture🔗

Texture2D object to draw.


intvframes =1🔗

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 to0.


Method Descriptions

Rect2get_rect()const🔗

Returns aRect2 representing the Sprite2D's boundary in local coordinates.

Example: Detect if the Sprite2D was clicked:

func_input(event):ifeventisInputEventMouseButtonandevent.pressedandevent.button_index==MOUSE_BUTTON_LEFT:ifget_rect().has_point(to_local(event.position)):print("A click!")

boolis_pixel_opaque(pos:Vector2)const🔗

Returnstrue, if the pixel at the given position is opaque andfalse in other case. The position is in local coordinates.

Note: It also returnsfalse, if the sprite's texture isnull or if the given position is invalid.


User-contributed notes

Please read theUser-contributed notes policy before submitting a comment.