Jump to content

Changing the sprite's graphic


scutajar
 Share

Recommended Posts

Just a quick question regarding changing a sprite's graphic while the game is running.

Before, this was done using the sprite's function loadGraphic. This doesn't exist anymore, and unless I'm missing something, I haven't found a suitable replacement in either Phaser.Sprite or PIXI.Sprite.

Any help would be appreciated :D

Link to comment
Share on other sites

  • 2 weeks later...

If PIXI was accessible, it seems you would simply be able to say

sprite.setTexture( PIXI.TextureCache[key] );

But phaser does not expose the PIXI object.

 

In a game I'm working on, I have to change the 'polarity' of an object, and it's represented by a different sprite.

And this can happen many times over and over, it'd be much easier to simply set the texture than to destroy and recreate a new sprite each time I wanted to do this.

 

Seems like a reasonable request

Link to comment
Share on other sites

I think loading a new graphic/change texture just to change sprite image is a bit overkill.

Like hackenstein said, you can load spritesheet image and change the frame of the Sprite as you need.

game.load.spritesheet('character', 'character.png', 60, 60);var character = game.add.sprite(0, 0, 'character');character.frame = 0; //Character idle imagecharacter.frame = 1; //Character jump image
Link to comment
Share on other sites

In addition here is how i change the sprite size:

game.load.atlas('tiles', 'assets/textureAtlas/breakout.png', 'assets/textureAtlas/breakout.json');paddle = game.add.sprite(game.world.centerX, game.world.height - 48, 'tiles', 'paddle_big.png');

and then:

 

        if(isPaddleNerfed) {            paddle.frameName = "paddle_small.png";            //paddle.animations.play('paddle_small', 10, true);        } else {            paddle.frameName = "paddle_big.png";            //paddle.animations.play('paddle_big', 10, true);        }
 
this way you can even change a sprite animation and not only between two frames... and it depends on the need which is better 
Link to comment
Share on other sites

 

I think loading a new graphic/change texture just to change sprite image is a bit overkill.

Like hackenstein said, you can load spritesheet image and change the frame of the Sprite as you need.

game.load.spritesheet('character', 'character.png', 60, 60);var character = game.add.sprite(0, 0, 'character');character.frame = 0; //Character idle imagecharacter.frame = 1; //Character jump image

 

I disagree, since the texture is basically a reference to a PIXI.Texture instance changing which reference any sprite is using doesn't seem like it would be particularly intensive

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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