Class TSprite

Unit

Declaration

type TSprite = class(TObject)

Description

Sprite is an animation composed from frames arranged in rows and columns inside a single image. Frames are read from left to right and from top to bottom.

In the simple case, a sprite represents a single animation, and the frames are just in consecutive order.

With the help of "custom animations" feature, you can define many animations in a sprite. Each custom animation is an array of frames with corresponding frames duration. Frames can be in any order. It is valid to use some particural frame many times with different duration time.

Hierarchy

  • TObject
  • TSprite

Overview

Methods

Public constructor Create(const URL: string; const AFrames, AColumns, ARows: Cardinal; const ASmoothScaling: Boolean = True; const ATimeLoop: Boolean = True; const APlay: Boolean = False);
Public constructor CreateFrameSize(const AImage: TGLImage; const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal; const ATimeLoop: Boolean = True; const APlay: Boolean = False); overload;
Public constructor CreateFrameSize(const URL: string; const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal; const ASmoothScaling: Boolean = True; const ATimeLoop: Boolean = True; const APlay: Boolean = False); overload;
Public destructor Destroy; override;
Public procedure Update(const SecondsPassed: TFloatTime);
Public procedure Play;
Public procedure Stop;
Public procedure Pause; deprecated 'use Stop';
Public procedure Draw; overload;
Public procedure Draw(const AX, AY: Single); overload;
Public procedure Draw(const AX, AY, DrawWidth, DrawHeight: Single); overload;
Public procedure Draw(const ScreenRectangle: TRectangle); overload;
Public procedure Draw(const ScreenRectangle: TFloatRectangle); overload;
Public procedure DrawFlipped(const ScreenRect: TRectangle; const FlipHorizontal, FlipVertical: boolean); overload;
Public procedure DrawFlipped(const ScreenRect: TFloatRectangle; const FlipHorizontal, FlipVertical: boolean); overload;
Public procedure Move(AX, AY: Single; ARot: Single=0);
Public function AddAnimation(const AAnimation: TSpriteAnimation): Integer; overload;
Public function AddAnimation(const AnimationFrames: array of Cardinal): Integer; overload;
Public function AddSpriteAnimation(const AnimationFrames: array of TSpriteAnimationFrame): Integer; deprecated 'use AddAnimation';
Public function SwitchToAnimation(const AAnimIndex: Integer; const ACustomFrame: Cardinal = 0): Boolean;
Public function Duration: TFloatTime;

Properties

Public property X: Single read FX write FX;
Public property Y: Single read FY write FY;
Public property Position: TVector2 read GetPosition write SetPosition;
Public property CenterX: Single read GetCenterX write SetCenterX default 0.5;
Public property CenterY: Single read GetCenterY write SetCenterY default 0.5;
Public property DrawingWidth: Single read FDrawingWidth write FDrawingWidth;
Public property DrawingHeight: Single read FDrawingHeight write FDrawingHeight;
Public property DrawRect: TFloatRectangle read GetDrawRect write SetDrawRect;
Public property Center: TVector2 read GetCenter write SetCenter;
Public property Rotation: Single read GetRotation write SetRotation default 0;
Public property Color: TCastleColor read GetColor write SetColor;
Public property Frames: Cardinal read FFrames;
Public property FramesPerSecond: Single read FFramesPerSecond write SetFramesPerSecond default DefaultFramesPerSecond;
Public property Frame: Cardinal read FFrame write SetFrame;
Public property Playing: Boolean read FPlaying write FPlaying default False;
Public property TimeLoop: Boolean read FTimeLoop write FTimeLoop;
Public property Time: TFloatTime read FTime write SetTime;
Public property FrameWidth: Cardinal read FFrameWidth write FFrameWidth;
Public property FrameHeight: Cardinal read FFrameHeight write FFrameHeight;
Public property LeftMargin: Cardinal read FLeftMargin write FLeftMargin default 0;
Public property LeftMarginOffset: Cardinal read FLeftMargin write FLeftMargin default 0; deprecated 'use LeftMargin';
Public property TopMargin: Cardinal read FTopMargin write FTopMargin default 0;
Public property TopMarginOffset: Cardinal read FTopMargin write FTopMargin default 0; deprecated 'use TopMargin';
Public property HorizontalSpacing: Cardinal read FHorizontalSpacing write FHorizontalSpacing default 0;
Public property VerticalSpacing: Cardinal read FVerticalSpacing write FVerticalSpacing default 0;
Public property VerticalSpacingBottom: boolean read FVerticalSpacingBottom write FVerticalSpacingBottom default false;
Public property TimeBackwards: Boolean read FTimeBackwards write FTimeBackwards default False;
Public property CurrentAnimation: Integer read FCurrentAnimation default -1;
Public property HorizontalFlip: Boolean read FHorizontalFlip write FHorizontalFlip default False;
Public property VerticalFlip: Boolean read FVerticalFlip write FVerticalFlip default False;
Public property DiagonalFlip: Boolean read FDiagonalFlip write FDiagonalFlip default False;

Description

Methods

Public constructor Create(const URL: string; const AFrames, AColumns, ARows: Cardinal; const ASmoothScaling: Boolean = True; const ATimeLoop: Boolean = True; const APlay: Boolean = False);

Constructor.

Parameters
URL
URL of source image.
AFrames
Total numer of animation frames.
AColumns
Mumber of columns.
ARows
Number of rows.
ASmoothscaling
Enables interpolation, see TGLImageCore.SmoothScaling for details, default True.
ATimeLoop
Should animation be repeated?, default True.
APlay
Is animation playing? if not then current frame will be displayed.
Public constructor CreateFrameSize(const AImage: TGLImage; const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal; const ATimeLoop: Boolean = True; const APlay: Boolean = False); overload;

Constructor that takes explicit frame size, and loaded TGLImage instance.

Parameters
AImage
Source image (will be owned by this sprite).
AFrames
Total numer of animation frames.
AColumns
Number of columns.
AFrameWidth
Width of each frame.
AFrameHeight
Height of each frame.
ATimeLoop
Should animation be repeated?, default True.
APlay
Is animation playing? if not then current frame will be displayed.
Public constructor CreateFrameSize(const URL: string; const AFrames, AColumns, AFrameWidth, AFrameHeight: Cardinal; const ASmoothScaling: Boolean = True; const ATimeLoop: Boolean = True; const APlay: Boolean = False); overload;

Constructor that takes explicit frame size.

Parameters
URL
URL of source image.
AFrames
Total numer of animation frames.
AColumns
Number of columns.
AFrameWidth
Width of each frame.
AFrameHeight
Height of each frame.
ASmoothscaling
Enables interpolation, see TGLImageCore.SmoothScaling for details, default True.
ATimeLoop
Should animation be repeated?, default True.
APlay
Is animation playing? if not then current frame will be displayed.
Public destructor Destroy; override;
 
Public procedure Update(const SecondsPassed: TFloatTime);

Update current frame.

Parameters
SecondsPassed
Time from previous update.
Public procedure Play;

Play playing animation., When playing, the Time will move forward when you call Update.

Public procedure Stop;

Stop playing animation. When stopped, the Time will not move forward, regardless if you call Update.

Public procedure Pause; deprecated 'use Stop';

Warning: this symbol is deprecated: use Stop

 
Public procedure Draw; overload;

Draw the sprite.

The overloaded version without AX, AY or ScreenRectangle parameters uses the last position set by Move method. This is the position of the bottom-left frame corner on screen.

The overloaded version without DrawWidth, DrawHeight or ScreenRectangle parameters uses the last size set by DrawingWidth, DrawingHeight properties. This is the size of the frame on screen.

All versions use the rotation set by the last Move method. This is the rotation of the frame on screen.

The overloaded versions deliberately look and work similar to TGLImageCore.Draw versions.

Public procedure Draw(const AX, AY: Single); overload;
 
Public procedure Draw(const AX, AY, DrawWidth, DrawHeight: Single); overload;
 
Public procedure Draw(const ScreenRectangle: TRectangle); overload;
 
Public procedure Draw(const ScreenRectangle: TFloatRectangle); overload;
 
Public procedure DrawFlipped(const ScreenRect: TRectangle; const FlipHorizontal, FlipVertical: boolean); overload;

Draw the sprite, optionally flipped horizontally and/or vertically. These methods ignore the HorizontalFlip, VerticalFlip, and DiagonalFlip – which axis is flipped (if any) depends only on the parameters to these methods.

Public procedure DrawFlipped(const ScreenRect: TFloatRectangle; const FlipHorizontal, FlipVertical: boolean); overload;
 
Public procedure Move(AX, AY: Single; ARot: Single=0);

Move sprite to position and rotation.

Public function AddAnimation(const AAnimation: TSpriteAnimation): Integer; overload;

Add a custom animation (using an explicit sequence of frames), that can be later used by SwitchToAnimation.

Parameters
AAnimation
The animation information. The animation instance (TSpriteAnimation) becomes owned by this object, so don't free it yourself.
AnimationFrames
The animation information can also be given as a simple list of frame indexes. In this case, all frames are assumed to take the same time: 1 / FramesPerSecond.
Returns

The animation index, that can be used with SwitchToAnimation.

Public function AddAnimation(const AnimationFrames: array of Cardinal): Integer; overload;
 
Public function AddSpriteAnimation(const AnimationFrames: array of TSpriteAnimationFrame): Integer; deprecated 'use AddAnimation';

Warning: this symbol is deprecated: use AddAnimation

 
Public function SwitchToAnimation(const AAnimIndex: Integer; const ACustomFrame: Cardinal = 0): Boolean;

Switch to the animation with index AAnimIndex. Use animation index obtained from AddAnimation to use a custom animation, or use -1 to use the default animation (spanning all sprite frames).

Returns

True on success, False if AAnimIndex is out of bounds.

Public function Duration: TFloatTime;

Duration, in seconds, of the currently used animation. See CurrentAnimation to know what the current animation is.

Properties

Public property X: Single read FX write FX;
 
Public property Y: Single read FY write FY;
 
Public property Position: TVector2 read GetPosition write SetPosition;
 
Public property CenterX: Single read GetCenterX write SetCenterX default 0.5;

Center X of rotation. Values between 0 and 1.

Public property CenterY: Single read GetCenterY write SetCenterY default 0.5;

Center Y of rotation. Values between 0 and 1.

Public property DrawingWidth: Single read FDrawingWidth write FDrawingWidth;

Destination frame width to draw.

Public property DrawingHeight: Single read FDrawingHeight write FDrawingHeight;

Destination frame height to draw.

Public property DrawRect: TFloatRectangle read GetDrawRect write SetDrawRect;

Drawing rectangle. This is just a shortcut to get / set properties X, Y, DrawingWidth, DrawingHeight.

Public property Center: TVector2 read GetCenter write SetCenter;

Center of rotation. Values between 0 and 1.

Public property Rotation: Single read GetRotation write SetRotation default 0;

Rotation in degrees.

Public property Color: TCastleColor read GetColor write SetColor;

Color multiplier of the sprite, see TGLImageCore.Color

Public property Frames: Cardinal read FFrames;

Total number of frames. Readonly. Defined at creation.

Public property FramesPerSecond: Single read FFramesPerSecond write SetFramesPerSecond default DefaultFramesPerSecond;

How many frames per second to play. This is used only for the default sprite animation (spanning all frames). For the custom animations (added by AddAnimation), each frame has an explicit (possibly different) duration.

Public property Frame: Cardinal read FFrame write SetFrame;

Current frame.

Public property Playing: Boolean read FPlaying write FPlaying default False;

Does the animation proceeds forward when you call Update method.

Public property TimeLoop: Boolean read FTimeLoop write FTimeLoop;

Does the animation loop, that is display the same animation over and over when time exceeded the animation duration.

Public property Time: TFloatTime read FTime write SetTime;

Current time within the animation, it determines the current frame. Yoy can set this explicity. Alternatively, you can call the Update method continously while the animation is Playing, and then the time will increase automatically.

Public property FrameWidth: Cardinal read FFrameWidth write FFrameWidth;

Width of a single frame. Initial value is set by the constructor.

Public property FrameHeight: Cardinal read FFrameHeight write FFrameHeight;

Height of a single frame. Initial value is set by the constructor.

Public property LeftMargin: Cardinal read FLeftMargin write FLeftMargin default 0;

X margin for frame position on the underlying image. Useful if the first frame doesn't start at X=0.

Public property LeftMarginOffset: Cardinal read FLeftMargin write FLeftMargin default 0; deprecated 'use LeftMargin';

Warning: this symbol is deprecated: use LeftMargin

 
Public property TopMargin: Cardinal read FTopMargin write FTopMargin default 0;

Y margin for frame position on the underlying image. Useful if the first frame doesn't start at Y=0.

Public property TopMarginOffset: Cardinal read FTopMargin write FTopMargin default 0; deprecated 'use TopMargin';

Warning: this symbol is deprecated: use TopMargin

 
Public property HorizontalSpacing: Cardinal read FHorizontalSpacing write FHorizontalSpacing default 0;

Horizontal spacing between frames. Use this if you have an empty space at the right of every frame in your spritesheet. This is useful to avoid frames "bleeding" into each other (due to smooth scaling).

Public property VerticalSpacing: Cardinal read FVerticalSpacing write FVerticalSpacing default 0;

Vertical spacing between frames. Use this if you have an empty space at the top (or bottom, if VerticalSpacingBottom) of every frame in your spritesheet. This is useful to avoid frames "bleeding" into each other (due to smooth scaling).

Public property VerticalSpacingBottom: boolean read FVerticalSpacingBottom write FVerticalSpacingBottom default false;

When True, the VerticalSpacing is assumed to be at the bottom of every frame, not top.

Public property TimeBackwards: Boolean read FTimeBackwards write FTimeBackwards default False;

Should we play the animation backwards after playing it forward. See TVideo.TimeBackwards.

Public property CurrentAnimation: Integer read FCurrentAnimation default -1;

Currently used animation. Equal to -1 when we're using the default animation spanning all sprite frames. Otherwise, indicates a custom animation index (added by AddAnimation and used by SwitchToAnimation).

Call SwitchToAnimation to change this.

Public property HorizontalFlip: Boolean read FHorizontalFlip write FHorizontalFlip default False;

Is sprite horizontal flipped?

Public property VerticalFlip: Boolean read FVerticalFlip write FVerticalFlip default False;

Is sprite vertical flipped?

Public property DiagonalFlip: Boolean read FDiagonalFlip write FDiagonalFlip default False;

Is sprite diagonal flipped?


Generated by PasDoc 0.15.0.