Jump to content

Load spritesheet from texture atlas


J4G
 Share

Recommended Posts

I'm using a texture atlas generated by TexturePacker. Within this atlas I have images that are spritesheets, containing multiple frames from an animation. How can I load these animations from a single image properly without using game.load.spritesheet?

Link to comment
Share on other sites

Hi, while having spritesheet nested in atlas is strange and I can not imagine use for it, I see two possibilities:

 

1) not for animation, but for displaying: use crop rectangle on sprite that has that spritesheet as frame to display only the part you need (actually, you can do animation by updating the cropRect, but it means to do the animation handling by yourself),

 

or

 

2) after loading this atlas add additional processing for spritesheets within it. In this processing add additional Frames to framedata of atlas. This code adds frame to framedata for "aKey" atlas. Call it with aKey = atlas, aFrame = you new frame name, aX,aY - top left corner in atlas, aWidth, aHeight - dimensions. Then, you can use these frames in animation or anywhere else.

        private addFrame(aKey:string, aFrame: string, aX: number, aY: number, aWidth: number, aHeight: number) : Phaser.Frame {            var frameData: Phaser.FrameData = this.game.cache.getFrameData(aKey);            var uuid = this.game.rnd.uuid();            var newFrame = frameData.addFrame(new Phaser.Frame(frameData.total,                aX, aY, aWidth, aHeight,                aFrame,                uuid.toString()));            PIXI.TextureCache[uuid] = new PIXI.Texture(PIXI.BaseTextureCache[aKey],                <PIXI.Rectangle> { x: aX, y: aY, width: aWidth, height: aHeight });            return newFrame;        }
Link to comment
Share on other sites

Commonly, I will use separate spritesheets during development as they can be easily updated.  Also they may be supplied from another source (eg gfx supplier) so it is easiest to work with them direct.

 

I use atlas's to group together separate resources into a single file for release version.  This development/release usage is important.

 

Phaser doesn't natively handle spritesheets embedded in an Atlas, it would be nice if it did. As Tom suggests, there are ways around this.  Anotherway would be to deconstruct your spritesheet into separate files and add these into the atlas.  But eitherway, whichever approach you use, there will be extra pre-processing or runtime code.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...