Animated Sprite

Scar::Components::AnimatedSprite is a drawable which you can assign a spritesheet texture, a frame size and some state info to. Together with the Scar::Systems::AnimateSprites system, it then gets updated every frame and drawn to the screen.

The state info is structured like this: A Hash mapping state names (Strings) to a tuple of three integers: index, framecount and framerate. Index is the offset into the spritesheet, counted in steps of the specified frame size in reading order. Framecount is the amount of successive frames the animation has. Framerate is the frequency at which the frames should cycle.

animated_sprite = Components::AnimatedSprite.new(
  Assets.texture("spritesheet.png"), # Spritesheet
  {128, 128}, # 128x128 texture size
  # idle walk and run are three animations based on the same 4 frames, but in varying speed
  # jump is 8 frames long and runs at 16 FPS
  # the spritesheet is layed out like this:
  #
  # AAAA####
  # BBBBBBBB
  #
  # A = idle/walk/run frames
  # B = jump frames
  {
    "idle" => {0, 4, 4},
    "walk" => {0, 4, 8},
    "run" => {0, 4, 16},
    "jump" => {8, 8, 16}
  }
)

Last updated