DEV Community

Yiren Labs
Yiren Labs

Posted on Originally published at atlas-errors.petersoncool.workers.dev

Phaser: TextureManager.SpriteSheetFromAtlas: Invalid frameWidth given.

TextureManager.SpriteSheetFromAtlas: Invalid frameWidth given.

What throws this error

TextureManager.SpriteSheetFromAtlas: Invalid frameWidth given.
Enter fullscreen mode Exit fullscreen mode

This line arrives as a hard Error that stops the current texture registration, not as a recoverable warning. It surfaces in the browser console the moment Phaser parses an atlas-derived spritesheet, typically after load.atlas finishes and your code calls the derived sheet builder. Example: the source atlas holds cells 64 px wide, but the config passes frameWidth: 128, which measures the whole atlas frame rather than one cell, or leaves frameWidth: 0 unset; the parser rejects the value before any rows or columns are computed.

How the parser rejects the value

The SpriteSheetFromAtlas parser lives in src/textures/parsers/SpriteSheetFromAtlas.js, with the guard around line 40: https://raw.githubusercontent.com/photonstorm/phaser/master/src/textures/parsers/SpriteSheetFromAtlas.js#L40

Before deriving the number of columns and rows, the code validates frameWidth against the atlas frame geometry. A width of zero, a negative width, or a width larger than the atlas frame itself fails that check, and the parser throws instead of producing a partial sheet. The TextureManager therefore never registers the derived spritesheet texture, and any sprite built from it later reports a missing texture rather than a readable layout problem.

The most common arithmetic slip is measuring the whole source image where a single cell should be measured. The atlas frame that the sub-sheet crops from already contains margin and spacing; dividing that full frame width by the cell count restores the real cell size, and that restored number is what the guard expects.

Three-step repair

  1. Match frameWidth and frameHeight to one cell in pixels.
this.textures.get('tilesAtlas').addSpriteSheetFromAtlas({
  key: 'tiles',
  atlasFrame: 'tiles_page',
  frameWidth: 64,
  frameHeight: 64,
  margin: 0,
  spacing: 0
});
Enter fullscreen mode Exit fullscreen mode

Open the atlas PNG in an image editor, measure a single cell, and set both dimensions to that measured size. Do not reuse the atlas frame width as the cell width.

  1. Confirm the atlas itself finished parsing before the derived sheet is built.
this.load.atlas('tilesAtlas', 'assets/tiles.png', 'assets/tiles.json');

this.load.once('complete', () => {
  this.textures.get('tilesAtlas').addSpriteSheetFromAtlas({
    key: 'tiles',
    atlasFrame: 'tiles_page',
    frameWidth: 64,
    frameHeight: 64
  });
});

this.load.start();
Enter fullscreen mode Exit fullscreen mode

The SpriteSheetFromAtlas parser reads frame geometry that the atlas JSON parser registered first; calling it too early, before the complete event, leaves the source frame unavailable and surfaces as an invalid-size error.

  1. Recompute margin and spacing from the real cell grid.
const source = this.textures.get('tilesAtlas').getFrame('tiles_page');
const columns = 4;
const frameWidth = Math.floor(source.width / columns);
const frameHeight = Math.floor(source.height / 4);

this.textures.get('tilesAtlas').addSpriteSheetFromAtlas({
  key: 'tiles',
  atlasFrame: 'tiles_page',
  frameWidth,
  frameHeight,
  margin: 0,
  spacing: 0
});
Enter fullscreen mode Exit fullscreen mode

If cells are separated by gutters, subtract gutter width from the available span before dividing; leaving spacing at zero while cells are guttered shrinks the effective cell and trips the width guard on the next frame.

Checklist

Error signal Triggering condition Validation step Correct setting
Invalid frameWidth given. frameWidth left at zero Inspect the config passed to addSpriteSheetFromAtlas Positive pixel width equal to one cell
Parser throws before rows are counted Width exceeds the atlas frame bounds Compare against the atlas frame width from getFrame Width no larger than source frame width
Derived texture missing after reload Atlas JSON parsed after the sheet was requested Build the sheet inside the loader complete handler Atlas registered first, sheet second
Cells look stretched or cut off Margin or spacing copied from another sheet Re-measure gutters in the atlas PNG margin and spacing match the real grid

The free inspector scans your Phaser loader config for frame width, height, margin, and spacing mismatches before the engine throws: https://atlas-errors.petersoncool.workers.dev/inspector/?ref=dev2_free_2

The full repair package includes grid arithmetic helpers, atlas registration order templates, and edge-case configs for guttered tile sheets: https://atlas-errors.petersoncool.workers.dev/checkout/?ref=dev2_buy_2

Top comments (0)