Sign in to follow this  
scutajar

Changing the sprite's graphic

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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

Share this post


Link to post
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 

Share this post


Link to post
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

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.