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
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
|
Methods
Signals
frame_changed()🔗
Emitted when theframe changes.
texture_changed()🔗
Emitted when thetexture changes.
Property Descriptions
boolis_centered()
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.
boolis_flipped_h()
Iftrue
, texture is flipped horizontally.
boolis_flipped_v()
Iftrue
, texture is flipped vertically.
intget_frame()
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)
🔗
Vector2iget_frame_coords()
Coordinates of the frame to display from sprite sheet. This is as an alias for theframe property.hframes orvframes must be greater than 1.
intget_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 to0
.
Vector2get_offset()
The texture's drawing offset.
boolis_region_enabled()
Iftrue
, texture is cut from a larger atlas texture. Seeregion_rect.
boolregion_filter_clip_enabled =false
🔗
boolis_region_filter_clip_enabled()
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)
🔗
Rect2get_region_rect()
The region of the atlas texture to display.region_enabled must betrue
.
Texture2Dget_texture()
Texture2D object to draw.
intget_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 to0
.
Method Descriptions
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!")
publicoverridevoid_Input(InputEvent@event){if(@eventisInputEventMouseButtoninputEventMouse){if(inputEventMouse.Pressed&&inputEventMouse.ButtonIndex==MouseButton.Left){if(GetRect().HasPoint(ToLocal(inputEventMouse.Position))){GD.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.