Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Creating a sprite from loaded atlases without defining which atlas


Recommended Posts

I am loading a bunch of texture atlases with many sprites in them, but now, for performance testing I would like to bulge some of these atlases together. Due to my automated gulp tasks this is no issues and only takes a couple of seconds, as to create the new textures, but still all my sprite creation use something like layer.foreground.create(x, y, 'enemies', 'enemy_pig'); which will no longer work if the atlas name has changed.


If I combine the two atlases "enemies" and "pickups" into one, all these initialisations will fail. Since I have named the textures within each atlas with an unique key even across the atlases there will be no mixup. So my question is:


How do I make phaser detect which atlas a texture-key is from, and is this a built in feature, since it seems obviously useful, or do I have to create my own implementation of detecting the atlas with a function like "findAtlas()" that searches the loaded atlases for a texture like:


layer.foreground.create(x, y, findAtlas('enemy_pig').key, 'enemy_pig');


Thanks for any help :)


Link to comment
Share on other sites

For any future reference, here is the solution I ended up coding:

    Phaser.Cache.prototype.getAtlasForTexture = function(texturekey) {        for(var key in game.cache._cache.image) {            if(game.cache._cache.image[key].frameData.getFrameByName(texturekey)) {                return game.cache._cache.image[key].key;            }        }        return null;    };

It returns the key to the atlas containing the texture with the texture key given.


So in all my sprite creation I do this:

layer.foreground.create(x, y, game.cache.getAtlasForTexture("unique_key"), "unique_key");

Since I'm prototyping, this is integrated right into the Phaser cache object.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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