class Scar::Components::AnimatedSprite

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 and B share the frames S in the middle.

State A uses the frames 0-3 and state B uses the frames 2-5.

Defined in:

scar/components/animated_sprite.cr

Constructors

Instance Method Summary

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

def self.new(texture : SF::Texture, size : Tuple(Int32, Int32), states : Hash(String, Tuple(Int32, Int32, Int32))) #

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)

[View source]

Instance Method Detail

def next #

Advances the current animation to its next frame


[View source]
def size : {Int32, Int32} #

Returns the frame size in pixels


[View source]
def state : String #

Returns the name of the current animation state


[View source]
def state=(new_state : String) #

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"

[View source]
def states : Hash(String, {Int32, Int32, Int32}) #

Returns the hash of animation states


[View source]
def update(dt) #

Advances the current animation by the given delta time


[View source]