Class Sprite2D
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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.
public class Sprite2D : Node2D, IDisposable
- Inheritance
-
Sprite2D
- Implements
- Inherited Members
Constructors
Sprite2D()
public Sprite2D()
Properties
Centered
If true
, texture is centered.
public bool Centered { get; set; }
Property Value
FlipH
If true
, texture is flipped horizontally.
public bool FlipH { get; set; }
Property Value
FlipV
If true
, texture is flipped vertically.
public bool FlipV { get; set; }
Property Value
Frame
Current frame to display from sprite sheet. Hframes or Vframes must be greater than 1. This property is automatically adjusted when Hframes or 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
.
public int Frame { get; set; }
Property Value
FrameCoords
Coordinates of the frame to display from sprite sheet. This is as an alias for the Frame property. Hframes or Vframes must be greater than 1.
public Vector2I FrameCoords { get; set; }
Property Value
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
.
public int Hframes { get; set; }
Property Value
Offset
The texture's drawing offset.
public Vector2 Offset { get; set; }
Property Value
RegionEnabled
If true
, texture is cut from a larger atlas texture. See RegionRect.
public bool RegionEnabled { get; set; }
Property Value
RegionFilterClipEnabled
If true
, the outermost pixels get blurred out. RegionEnabled must be true
.
public bool RegionFilterClipEnabled { get; set; }
Property Value
RegionRect
The region of the atlas texture to display. RegionEnabled must be true
.
public Rect2 RegionRect { get; set; }
Property Value
Texture
Texture2D object to draw.
public Texture2D Texture { get; set; }
Property Value
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
.
public int Vframes { get; set; }
Property Value
Methods
GetRect()
Returns a Rect2 representing the Sprite2D's boundary in local coordinates. Can be used to detect if the Sprite2D was clicked.
Example:
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseButton inputEventMouse)
{
if (inputEventMouse.Pressed && inputEventMouse.ButtonIndex == MouseButton.Left)
{
if (GetRect().HasPoint(ToLocal(inputEventMouse.Position)))
{
GD.Print("A click!");
}
}
}
}
public Rect2 GetRect()
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsPixelOpaque(Vector2)
Returns true
, if the pixel at the given position is opaque and false
in other case.
Note: It also returns false
, if the sprite's texture is null
or if the given position is invalid.
public bool IsPixelOpaque(Vector2 pos)
Parameters
pos
Vector2
Returns
Events
FrameChanged
Emitted when the Frame changes.
public event Action FrameChanged
Event Type
TextureChanged
Emitted when the Texture changes.
public event Action TextureChanged