본문으로 건너뛰기
Skillpedia
일반 코딩6.4k · #14215

melonjs-sprites-and-animation

하는 일

스프라이트를 만들고, 프레임 애니메이션을 설정하며, 일반적인 스프라이트 도구의 텍스처 아틀라스를 사용하는 방법을 설명합니다. 게임에 캐릭터와 오브젝트를 배치하는 게임 개발자를 위한 스킬입니다.

사용하는 경우

  • ✓개발자가 캐릭터가 대기나 걷기 애니메이션을 재생하게 하고 싶을 때.
  • ✓개발자의 스프라이트가 배치한 위치에서 어긋나 보일 때.
  • ✓개발자가 TexturePacker나 Aseprite 같은 도구에서 내보낸 스프라이트 시트를 사용하려 할 때.

스킬 링크

https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-sprites-and-animation/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-sprites-and-animation/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“제 주인공 스프라이트가 스프라이트 시트에서 대기 애니메이션을 재생하고, 화면 왼쪽 위에 놓이도록 해 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

스킬은 스프라이트 위치가 기본적으로 가운데 기준이라고 설명하고, 이미지나 아틀라스 영역으로 스프라이트를 만드는 방법을 보여 준 뒤, 프레임 목록으로 이름 있는 애니메이션을 설정합니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.

Sprites, animation and atlases

Creating sprites from images or atlases, driving frame animation, and the positioning trap that catches almost everyone.

pos is the centre, not the top-left

Renderable.anchorPoint defaults to (0.5, 0.5), so a sprite's pos is its centre. Container forces (0, 0) instead, which is why containers and sprites appear to disagree.

// a 64×64 sprite at (100, 100) covers 68..132, not 100..164
const s = new Sprite(100, 100, { image: "hero", framewidth: 64, frameheight: 64 });

s.anchorPoint.set(0, 0);   // switch to top-left positioning if you prefer

Writing top-left maths against the centred default puts everything half a sprite off, with no error. It is the most common source of "close but subtly wrong" placement.

Constructing a sprite

From a plain image:

const hero = new Sprite(x, y, {
    image: "hero",          // preloaded asset name
    framewidth: 32,
    frameheight: 32,
});

From a texture atlas region:

const atlas = new TextureAtlas(loader.getJSON("sprites"), loader.getImage("sprites"));
const coin = new Sprite(x, y, { image: atlas, region: "coin_01.png" });

framewidth / frameheight define the animation grid on a sheet. Omit them and the whole image is one frame.

Animation

hero.addAnimation("idle", [0, 1, 2, 3], 100);   // frames, ms per frame
hero.addAnimation("walk", [4, 5, 6, 7]);
hero.addAnimation("die",  [8, 9], 150);

hero.setCurrentAnimation("walk");
hero.setCurrentAnimation("die", "idle");                          // chain to idle when it ends
hero.setCurrentAnimation("die", { loop: false });                 // play once, hold the last frame
hero.setCurrentAnimation("die", { loop: false, onComplete: fn }); // …and call fn when it does
hero.setCurrentAnimation("use", { next: "idle", onComplete: fn }); // call fn, then chain to idle
hero.setCurrentAnimation("run", { onFrame: (frame) => {            // each frame playback reaches
    if (frame === 3 || frame === 8) footstep();
} });

if (hero.isCurrentAnimation("walk")) { /* … */ }

Frame indices are into the framewidth/frameheight grid. With an atlas, use atlas.getAnimationSettings([regionNames]) and spread it into the Sprite settings, or atlas.createAnimationFromName([regionNames]), which returns a ready-made Sprite. On a fixed-cell spritesheet only numeric indices are accepted — passing region names throws.

The second argument to setCurrentAnimation is polymorphic. A string chains to that animation when this one ends. An options object takes { loop, next, speed, onComplete, onFrame }, and only an explicit loop: false stops the looping. A bare function is the legacy form: it fires at every cycle end, and only holds the last frame if it returns false. Omit the argument entirely and the animation loops forever.

onFrame(frame) is called with frame 0 when the animation starts, then with each frame playback reaches, so a footstep or a hit lands on its frame without polling getCurrentAnimationFrame(). A long tick reports every frame it goes past. It is not called for a frame set with setAnimationFrame, nor for the frame 0 a play-once animation would wrap to. Keep the callback in a field when you call setCurrentAnimation every frame: re-selecting keeps the first one anyway.

With both next and onComplete, the callback runs first and the chained animation starts with none of its own, so a one-off action (a pickup, an attack) needs no manual switch back to idle.

Re-selecting the animation that is already current is a no-op, so calling setCurrentAnimation every frame is safe, and an isCurrentAnimation check around it adds nothing. An unknown id throws.

Texture atlases

TextureAtlas reads several formats:

source how
TexturePacker / Free Texture Packer JSON new TextureAtlas(loader.getJSON(name), loader.getImage(name))
Aseprite JSON export same as TexturePacker — the format is detected from meta.app
Aseprite .aseprite / .ase binary loader type "aseprite", which stores the composited image and a JSON sidecar under the one asset name
ShoeBox JSON export using the melonJS exporter settings file; same call as TexturePacker
fixed-cell spritesheet new TextureAtlas({ framewidth, frameheight, anchorPoint }, loader.getImage(name))
multi-page pass an array of atlas JSON objects as the first argument

Helpers: `createSpriteFromName(nam

자주 묻는 질문

스프라이트가 크기의 절반만큼 어긋나 보이는 이유는 무엇인가요?

스프라이트는 기본적으로 가운데가 기준이므로, 위치는 모서리가 아니라 중심이라고 스킬은 말합니다.

아틀라스란 무엇인가요?

많은 스프라이트 그림을 담고 있는 하나의 이미지이며, 각 그림의 위치를 설명하는 데이터 파일이 함께 있습니다.

애니메이션 프레임은 어떻게 설정하나요?

프레임 번호 목록과 밀리초 단위 시간으로 이름 있는 애니메이션을 추가하는 방법을 보여 준다고 스킬은 말합니다.

상세 정보

작성자의 원본 설명

Use this skill when creating sprites, playing frame animations, or working with texture atlases in melonJS. Covers Sprite construction from a plain image or an atlas region, addAnimation/setCurrentAnimation, TextureAtlas for TexturePacker/ShoeBox/Aseprite, tint, flip, NineSliceSprite, and the centred anchorPoint that offsets naive positioning. Triggers on: Sprite, new Sprite, addAnimation, setCurrentAnimation, isCurrentAnimation, TextureAtlas, TexturePacker, Aseprite, ShoeBox, atlas, region, framewidth, frameheight, tint, flipX, flipY, NineSliceSprite, animation.

repo: melonjs/melonJS

인기도

GitHub 스타
6.4k
순위
#14215
라이선스
MIT
업데이트
2026. 11. 2.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬