Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for animated sprite sheets #46

Open
GregVGW opened this issue Nov 7, 2023 · 2 comments
Open

add support for animated sprite sheets #46

GregVGW opened this issue Nov 7, 2023 · 2 comments

Comments

@GregVGW
Copy link

GregVGW commented Nov 7, 2023

Currently when generating sprite sheets, there is no way to mark it as an animated sprite sheet.

This would allow for use in Pixi's AnimatedSprite, as the generated JSON would contain the animations property (listing the textures used as part of the animated sprite).

@hotyes
Copy link

hotyes commented Dec 26, 2023

hello, do you have any plan to support this? thanks

@reececomo
Copy link

reececomo commented Jun 9, 2024

A quick helper utility – to load any spritesheet as an AnimatedSprite:

function makeAnimation(sheetName: string, filter?: string): AnimatedSprite {
  const sheet: Spritesheet = Assets.get(sheetName);
  const frames = (filter
    ? Object.keys(sheet.textures).filter(key => key.includes(filter))
    : Object.keys(sheet.textures))
    .sort().map(key => sheet.textures[key]!);

  return new AnimatedSprite(frames);
}

Tip

Supports any spritesheet where input image names are orderable.

myAnimation{tps}/
  ├─ image0001.png
  ├─ image0002.png
  ├─ (...)
  └─ image0012.png

or many animations in a generic spritesheet:

mySpritesheet{tps}/
  ├─ apple0001.png
  ├─ apple0002.png
  ├─ (...)
  ├─ banana0001.png
  ├─ banana0002.png
  ├─ (...)
  ├─ background.png
  └─ (...other textures)

Just make sure you don't run into the classic image10.png < image2.png and you're golden (or provide a compareFn to sort()).

e.g.

// Load a standalone animation
const myAnimation = makeAnimation("assets/[email protected]");
myAnimation.play();

// Load animation from any sheet
const appleAnimation = makeAnimation("assets/[email protected]", "apple");
const bananaAnimation = makeAnimation("assets/[email protected]", "banana");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants