Jump to content

Swapping atlases at runtime


lewster32
 Share

Recommended Posts

I'm trying to put together an effect in a game where every sprite and image gets changed to a 'corrupted' version. I've created two texture atlases (with the keys 'sprites' and 'sprites-hacked'); both have identical data, but one has its source image modified. Without delving deep into the code, does anyone know if it's possible to quickly swap out all of the textures to use a new atlas? I've got so far as to change all non-animating sprites and images by simply using loadTexture, however if any of them are animating, they revert to the old atlas on the next frame.

    Main.prototype.setHacked = function (group, val) {        var h = (val === true) ? '-hacked' : '';        for (var i = 0, len = group.children.length; i < len; i++) {            if (group.children[i] instanceof Phaser.Sprite || group.children[i] instanceof Phaser.Image || group.children[i] instanceof Phaser.TileSprite) {                if (group.children[i].frameName) {                    group.children[i].loadTexture('sprites' + h, group.children[i].frameName || 0);                }            }            else if (group.children[i] instanceof Phaser.Group) {                this.setHacked(group.children[i], val);            }        }    };

Ideally, it'd be nice if there was something I could use at runtime to simply update the source image for the atlas lookups, however from testing I can only manage to do that before I create any of the sprites - it has no effect after they've been created.

 

If there is no current way to do this, I reckon would be a really handy feature to have to enable 'palette swap' type effects like you see in a lot of old games.

Link to comment
Share on other sites

Looks like I may have figured this out (to a degree anyway). For sprites with animation, I've used the following (took a bit of digging into pixi's sources to find it):

if (group.children[i].animations && group.children[i].animations.currentAnim) {    group.children[i].texture.baseTexture.source = this.game.cache.getImage('sprites' + h);}
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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