class Scar::Components::AnimatedSprite
- Scar::Components::AnimatedSprite
- Scar::Components::Sprite
- Scar::Components::Drawable
- Scar::Component
- Reference
- Object
Overview
This component is a more advanced wrapper around SF::Sprite
.
It supports multi-state animation based on a spritesheet. It is supposed to be used in conjunction with Systems::AnimateSprites
.
The spritesheet must such a layout that each animation state can be described as an index range inside the spritesheet. This means that each frame must have the same pixel size and all frames of an animation state must appear in sequence in the spritesheet (right-down order). See Scar examples for real-world examples.
Example usage:
# The example spritesheet could be lay out like this:
#
# AAAABB
# BBCCCC
#
sprite = Scar::Components::AnimatedSprite.new(
Assets.texture("textures/spritesheet.png"), # Get the spritesheet from the asset manager
{32, 32}, # Each frame has a size of 32x32 pixels
{
"A" => {0, 4, 2}, # Start index: 0, framecount: 4, fps: 2
"B" => {4, 4, 5}, # Start index: 4, framecount: 4, fps: 5
"C" => {8, 4, 8}, # Start index: 8, framecount: 4, fps: 8
}
)
sprite.state = "A" # Necessary, there is no default animation state
If you specify 0
fps, the animation will not advance by itself. It can be advanced manually by calling #next
.
Note that animation states can share frames, but only if both states' frames remain in sequence.
Example:
Spritesheet layout:
AASSBB
.
Animation states
A
andB
share the framesS
in the middle.
State
A
uses the frames 0-3 and stateB
uses the frames 2-5.
Defined in:
scar/components/animated_sprite.crConstructors
-
.new(texture : SF::Texture, size : Tuple(Int32, Int32), states : Hash(String, Tuple(Int32, Int32, Int32)))
Creates a new animated sprite
Instance Method Summary
-
#next
Advances the current animation to its next frame
-
#size : {Int32, Int32}
Returns the frame size in pixels
-
#state : String
Returns the name of the current animation state
-
#state=(new_state : String)
Sets the current animation state
-
#states : Hash(String, {Int32, Int32, Int32})
Returns the hash of animation states
-
#update(dt)
Advances the current animation by the given delta time
Instance methods inherited from class Scar::Components::Sprite
drawable : SF::Sprite
drawable,
drawable=(drawable : SF::Sprite)
drawable=
Constructor methods inherited from class Scar::Components::Sprite
new(texture : SF::Texture, rect : SF::IntRect | Nil = nil)
new
Instance methods inherited from module Scar::Drawable
blend_mode : SF::BlendMode
blend_mode,
blend_mode=(blend_mode : SF::BlendMode)
blend_mode=,
drawable : SF::Drawable
drawable,
shader : SF::Shader
shader,
shader=(shader : SF::Shader)
shader=,
texture : SF::Texture
texture,
texture=(texture : SF::Texture)
texture=,
visible=(visible)
visible=,
visible?
visible?
Constructor Detail
Creates a new animated sprite
Parameters:
- texture: Source spritesheet
- size: size of a frame in pixels {width, height}
- states: list of animation states {start-index, framecount, framerate} (see overview for an example)
Instance Method Detail
Sets the current animation state
Takes the name of an animation state specified in the states parameter of the constructor
Example usage:
an_sprite = Scar::Components::AnimatedSprite.new(... "idle" => {...} ...)
an_sprite.state = "idle"