melonjs/melonjs/melonjs-sprites-and-animation
Claude Code and agent skill by melonjs/melonjs
- community
- ext:[{"type":"config","description":"Configuration overrides"}]
Checking that the SKILL.md file is still in the repository.
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, onFrame, onComplete, play, pause, flicker, anchorPoint, TextureAt…
Install
npx vskill@latest install melonjs/melonjs/melonjs-sprites-and-animationSemi-extensibleMentions customization, but needs skill-specific knowledge. Read the extensible skills standard
SKILL.md
Sprites, animation and atlases
Creating sprites from images or atlases, driving frame animation, and thepositioning 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.
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:
From a texture atlas region:
framewidth / frameheight define the animation grid on a sheet. Omit them and the whole image is one frame.
Animation
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. The options type also has a crossfade, which only a 3D GLTFModel reads (see melonjs-3d-assets); a sprite ignores it.
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. The exception is a `loop: false` run that has finished: selecting it again plays it again, so a death animation selected every frame replays forever. Select a play-once animation once, on the event that starts it. An unknown id throws.
A frame can carry its own delay, as { name, delay } in place of the index or region name: [{ name: 3, delay: 200 }, { name: 4, delay: 100 }]. play(name?, options?) switches and plays (or, with no name, resumes), pause() freezes the current frame and stop() rewinds to the first one and pauses there; the options are setCurrentAnimation's.
addAnimation with an id that exists replaces it, which is how you change an animation's frames or speed (hero.addAnimation("walk", [4, 5, 6, 7], 200)). Redefining the one that is playing takes effect at once: the playing frame is kept if the new strip still has it, playback restarts on frame 0 if not, a finished loop: false run holds the new last frame, and the setCurrentAnimation options stay. Before 20.13 it kept the old frame count, so a shorter strip threw on the next update() and a longer one never reached its new frames.
Since 20.14, a frame the texture does not have (a number past the end of the sheet, a name the atlas does not hold) is left out with a console warning naming it, and the frames that exist play in order; addAnimation returns how many it kept.
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(name), createAnimationFromName([names]), getAnimationSettings([names]).
Atlases matter for performance: the GPU batchers bind several textures at once (up to renderer.maxTextures), so a handful of separate images still batch — but once a frame crosses that many distinct textures the batch has to flush. Packing into one atlas keeps everything on a single binding.
Visual properties
`scale()` is multiplicative, not absolute. Calling it every frame compounds. For absolute scaling reset first:
This bites hardest with pooled sprites, which arrive carrying their previous life's transform.
NineSliceSprite
For panels and dialogue boxes that stretch without distorting their corners:
width and height are mandatory and the constructor throws without them. The insets default to a quarter of the frame.
Pooling
Frequently spawned sprites should be pooled:
Two consequences:
- A recyclable class's
onResetEventmust reset everything its lifetime
mutates — alpha, tint, scale, animation state. Anything you forget carries over to the next use.
- Pool-recycled objects do not fire
onDestroyEventon removal. Pair event
subscriptions with onActivateEvent / onDeactivateEvent instead.
Symptom → cause
| symptom | cause | |
|---|---|---|
| everything offset by half a sprite | top-left maths against the centred anchorPoint | |
| sprite grows every frame | scale() is multiplicative — reset the transform | |
| a recycled sprite keeps its old look | onResetEvent does not restore every mutated property | |
| event handlers leak on pooled objects | bound in the constructor rather than onActivateEvent | |
| animation never advances | no framewidth/frameheight, so the sheet is one frame | |
| animation plays once and stops | { loop: false }, or a callback that returned false | |
a loop: false animation keeps replaying | setCurrentAnimation called every frame: a finished play-once run is restarted by selecting it again | |
a one-frame animation never calls its onComplete | a looping one-frame animation does not step; play it with loop: false or next (before 20.12 even those never completed) | |
TypeError reading offset or delay in update() after re-adding the playing animation with fewer frames, or a longer one never shows its new frames | before 20.13 redefining the current animation kept its old frame count; upgrade | |
a console warning addAnimation("walk"): no frame … in the texture, or a TypeError reading delay / offset on an older melonJS | a frame number or name the sheet or atlas does not have (often an atlas re-exported without one of its images): fix the frame list or re-export the atlas | |
this is wrong in a flicker(ms, callback) callback, or a flicker started from it stops at once | before 20.13.0 the callback ran on an internal object and the flicker was stopped after it; now this is the sprite and the flicker has already ended | |
| sprites do not batch | more distinct textures in one frame than renderer.maxTextures | |
| nine-slice corners stretch anyway | insetX/insetY instead of insetx/insety |
Related skills
melonjs-renderables— anchors, draw order, custom drawmelonjs-getting-started— preloading images and atlases
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingpackages/melonjs/skills/melonjs-sprites-and-animation/SKILL.md
Popularity
- Repository stars
- 6,422
- Repository forks
- 664
- 7-day trend score
- 44
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Extension points
configConfiguration overrides
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/melonjs/melonjs/melonjs-sprites-and-animation)