Sprite2D
General-purpose sprite node.
General-purpose sprite node.
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.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
Sprite2DConstructor
public Sprite2D();Properties12
CenteredProperty
public bool Centered { get; set; }If true, 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 to false, or consider enabling ProjectSettings.rendering/2d/snap/snap_2d_vertices_to_pixel and ProjectSettings.rendering/2d/snap/snap_2d_transforms_to_pixel.
FlipHProperty
public bool FlipH { get; set; }If true, texture is flipped horizontally.
FlipVProperty
public bool FlipV { get; set; }If true, texture is flipped vertically.
FrameProperty
public int Frame { get; set; }Current frame to display from sprite sheet. Sprite2D.Hframes or Sprite2D.Vframes must be greater than 1. This property is automatically adjusted when Sprite2D.Hframes or Sprite2D.Vframes are changed to keep pointing to the same visual frame (same column and row). If that's impossible, this value is reset to 0.
FrameCoordsProperty
public Vector2I FrameCoords { get; set; }Coordinates of the frame to display from sprite sheet. This is as an alias for the Sprite2D.Frame property. Sprite2D.Hframes or Sprite2D.Vframes must be greater than 1.
HframesProperty
public int Hframes { get; set; }The number of columns in the sprite sheet. When this property is changed, Sprite2D.Frame is adjusted so that the same visual frame is maintained (same row and column). If that's impossible, Sprite2D.Frame is reset to 0.
OffsetProperty
public Vector2 Offset { get; set; }The texture's drawing offset.
Note: When you increase Sprite2D.Offset.y in Sprite2D, the sprite moves downward on screen (i.e., +Y is down).
RegionEnabledProperty
public bool RegionEnabled { get; set; }Enables sampling RegionRect instead of the full texture. RegionRect uses source texture pixels. In the tested shader path, UV already includes the region; do not apply its offset again.
RegionFilterClipEnabledProperty
public bool RegionFilterClipEnabled { get; set; }If true, the area outside of the Sprite2D.RegionRect is clipped to avoid bleeding of the surrounding texture pixels. Sprite2D.RegionEnabled must be true.
RegionRectProperty
public Rect2 RegionRect { get; set; }Source rectangle in Texture pixels, used when RegionEnabled is true. It is neither the normalized shader REGION_RECT nor the scaled sprite size. Hframes/Vframes further divide the selected rectangle into frames.
VframesProperty
public int Vframes { get; set; }The number of rows in the sprite sheet. When this property is changed, Sprite2D.Frame is adjusted so that the same visual frame is maintained (same row and column). If that's impossible, Sprite2D.Frame is reset to 0.
Methods26
GetFrameint
public int GetFrame();intGetFrameCoordsVector2I
public Vector2I GetFrameCoords();Vector2IGetHframesint
public int GetHframes();intGetOffsetVector2
public Vector2 GetOffset();Vector2GetRectRect2
public Rect2 GetRect();Returns a Rect2 representing the Sprite2D's boundary in local coordinates.
Example: Detect if the Sprite2D was clicked:
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseButton inputEventMouse)
{
if (inputEventMouse.Pressed && inputEventMouse.ButtonIndex == MouseButton.Left)
{
if (GetRect().HasPoint(ToLocal(inputEventMouse.Position)))
{
ReckfellApi.Print("A click!");
}
}
}
}Rect2GetRegionRectRect2
public Rect2 GetRegionRect();Source rectangle in Texture pixels, used when RegionEnabled is true. It is neither the normalized shader REGION_RECT nor the scaled sprite size. Hframes/Vframes further divide the selected rectangle into frames.
Rect2The stored Rect2 in texture pixels, even when RegionEnabled is disabled.
GetTextureTexture2D
public Texture2D GetTexture();Texture2DGetVframesint
public int GetVframes();intIsCenteredbool
public bool IsCentered();boolIsFlippedHbool
public bool IsFlippedH();boolIsFlippedVbool
public bool IsFlippedV();boolIsPixelOpaquebool
public bool IsPixelOpaque(Vector2 pos);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 Sprite2D.Texture is null. pos is in local coordinates.
| Name | Type | Default |
|---|---|---|
pos | Vector2 | — |
boolIsRegionEnabledbool
public bool IsRegionEnabled();Enables sampling RegionRect instead of the full texture. RegionRect uses source texture pixels. In the tested shader path, UV already includes the region; do not apply its offset again.
booltrue when source selection uses RegionRect.
IsRegionFilterClipEnabledbool
public bool IsRegionFilterClipEnabled();boolSetCenteredvoid
public void SetCentered(bool centered);| Name | Type | Default |
|---|---|---|
centered | bool | — |
SetFlipHvoid
public void SetFlipH(bool flipH);| Name | Type | Default |
|---|---|---|
flipH | bool | — |
SetFlipVvoid
public void SetFlipV(bool flipV);| Name | Type | Default |
|---|---|---|
flipV | bool | — |
SetFramevoid
public void SetFrame(int frame);| Name | Type | Default |
|---|---|---|
frame | int | — |
SetFrameCoordsvoid
public void SetFrameCoords(Vector2I coords);| Name | Type | Default |
|---|---|---|
coords | Vector2I | — |
SetHframesvoid
public void SetHframes(int hframes);| Name | Type | Default |
|---|---|---|
hframes | int | — |
SetOffsetvoid
public void SetOffset(Vector2 offset);| Name | Type | Default |
|---|---|---|
offset | Vector2 | — |
SetRegionEnabledvoid
public void SetRegionEnabled(bool enabled);Enables sampling RegionRect instead of the full texture. RegionRect uses source texture pixels. In the tested shader path, UV already includes the region; do not apply its offset again.
| Name | Type | Default |
|---|---|---|
enabled | bool | — |
enabledtrue to use RegionRect.
SetRegionFilterClipEnabledvoid
public void SetRegionFilterClipEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetRegionRectvoid
public void SetRegionRect(Rect2 rect);Source rectangle in Texture pixels, used when RegionEnabled is true. It is neither the normalized shader REGION_RECT nor the scaled sprite size. Hframes/Vframes further divide the selected rectangle into frames.
| Name | Type | Default |
|---|---|---|
rect | Rect2 | — |
rectOffset and size in source texture pixels, before sprite scaling.
SetTexturevoid
public void SetTexture(Texture2D texture);| Name | Type | Default |
|---|---|---|
texture | Texture2D | — |
SetVframesvoid
public void SetVframes(int vframes);| Name | Type | Default |
|---|---|---|
vframes | int | — |
Signals and events2
FrameChangedEvent
public event Action FrameChanged;Emitted when the Sprite2D.Frame changes.
TextureChangedEvent
public event Action TextureChanged;Emitted when the Sprite2D.Texture changes.