Class Animations


public final class Animations extends ResourcesContainer<Animation>

Resource container for Animation resources.

The container can import animations from the JSON format exported by Aseprite and export existing animations back to that format. This makes it possible to author animations in Aseprite and load them directly at runtime via Resources.animations().

Aseprite exports a sprite sheet image together with a JSON sidecar describing each frame's location and display duration. When loading, the container expects the sprite sheet image to live next to the JSON file (as referenced by the meta.image field). All frames must have the same width and height; the engine's Spritesheet model is grid-based and does not support frames of arbitrary sizes.

  • Field Details

    • ASEPRITE_FILE_EXTENSION

      public static final String ASEPRITE_FILE_EXTENSION
      File extension recognised for Aseprite JSON sidecar files.
      See Also:
  • Method Details

    • load

      protected Animation load(URL resourceName) throws IOException
      Description copied from class: ResourcesContainer

      Loads the resource with the specified URL.

      This method must be implemented by subclasses to define how a resource is loaded from the given URL.

      Specified by:
      load in class ResourcesContainer<Animation>
      Parameters:
      resourceName - The URL of the resource to be loaded.
      Returns:
      The loaded resource.
      Throws:
      IOException
    • importAseprite

      public Animation importAseprite(Path asepriteJsonPath) throws IOException

      Imports an Aseprite-exported animation from the given JSON file.

      The loaded animation is registered with this container under the file name (without extension) of the JSON file. The associated sprite sheet is registered with Resources.spritesheets().

      Parameters:
      asepriteJsonPath - The path to the Aseprite JSON sidecar file.
      Returns:
      The imported animation.
      Throws:
      IOException - If the file cannot be read or parsed, or if the referenced image cannot be loaded.
    • exportAseprite

      public boolean exportAseprite(Animation animation, Path destinationJson)

      Exports the specified animation to the Aseprite JSON format.

      The sprite sheet image referenced by the animation is written next to the JSON file using the sprite sheet's name and the .png file extension. The resulting .json file follows the layout produced by the Aseprite CLI when exported with the --format json-hash option.

      Parameters:
      animation - The animation to export.
      destinationJson - The path of the JSON file to write.
      Returns:
      true if the export was successful; false otherwise.
    • toAseprite

      public static AsepriteFormat toAseprite(Animation animation)
      Builds an Aseprite JSON model from the given animation.
      Parameters:
      animation - The animation to describe.
      Returns:
      The Aseprite JSON model.
    • fromAseprite

      public static Animation fromAseprite(AsepriteFormat format, String animationName, URL baseUrl) throws IOException

      Builds an Animation (and its backing Spritesheet) from the given Aseprite model.

      The image referenced by AsepriteFormat.getImage() is resolved relative to the supplied baseUrl and loaded via the engine's image resource container.

      Parameters:
      format - The Aseprite model to convert.
      animationName - The name to assign to the resulting animation.
      baseUrl - The URL used to resolve the referenced image. May be null if the image is referenced by an absolute path.
      Returns:
      The created animation.
      Throws:
      IOException - If the referenced image cannot be loaded or if the frames are not uniform in size.
    • getAllByName

      public Map<String, Animation> getAllByName()
      Convenience method: returns all loaded animations indexed by name.
      Returns:
      An ordered map of loaded animation name to Animation instance.
    • importAseprite

      public Animation importAseprite(String asepriteJsonPath) throws IOException
      Convenience overload that loads an Aseprite JSON file from the runtime classpath or file system.
      Parameters:
      asepriteJsonPath - The path to the Aseprite JSON sidecar file.
      Returns:
      The imported animation.
      Throws:
      IOException - If the file cannot be read or parsed, or if the referenced image cannot be loaded.