Jump to content

Get atlas json?


Sturb
 Share

Recommended Posts

I was wondering if there was a way to get the JSON data for an atlas after it is loaded. I want to make a utility function that will return an array of strings which are the frame names for any given animation. This way I don't need to list [ 'frame01, 'frame02', ..., 'frame99']. Basically you provide it a prefix and it searches the atlas data for all matching keys. Sort of the same way Starling's AssetManager handles it.

Oh, and if there is already a way to do this, then pointing that out would help out too :D. Thanks.

Link to comment
Share on other sites

The atlas json isn't retained once the FrameData has been created, so at the moment there is no 'native' way to do it.

 

I'd say either override Phaser.Cache.addTextureAtlas and then store the atlasData before passing it off to the parsers. Or load the json files using Loader.json instead and the textures with Loader.image and then just call Cache.addTextureAtlas directly, passing in both objects to it.

Link to comment
Share on other sites

Thanks! However I was able to figure something out with what was already provided in the framework. I was able to use this.game.cache.getFrameData, so get all the frames in an atlas which is perfectly fine.

 

So if anyone is interested in something like this, here is my code. It's rather simple but comes in handy if your animations contains a large amount of frames.. (Note: I'm writing my game in Typescript, but you'll get the gist of it.

Utility function:
 

static getFrameKeys(prefix: string, frameData: Phaser.FrameData): Array<string> {        // The return value;    var out: Array<string> = [];    // Search for matching frames.    for (var i = 0; i < frameData.total; i++) {        var name: string = frameData.getFrame(i).name;        if (name.indexOf(prefix) == 0){            out.push(name);        }    }    // Return the list of frame names matching the prefix.    return out;}

Usage:

this.animations.add('flip', Utils.getFrameKeys('callLetterDisplay00', this.game.cache.getFrameData('gameAtlas1')), 30, true);

So in this case I'm returning a rather large animation of roughly 80 frames. It's returning callLetterDisplay0001 to callLetterDisplay0080.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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