d13

[PIXI] Changing sprite textures

Recommended Posts

Hello!

 

Could any experienced Pixi users out there please tell me if what I'm doing is really bad?

I'm changing sprite textures using some code that looks a bit like this:

var section = new PIXI.Rectangle(  sourceX, sourceY, sprite._sourceWidth, sprite._sourceHeight);sprite.texture = new PIXI.Texture(sprite._baseTexture, section);

`_baseTexture`, `_sourceWidth` and `_sourceHeight` are all what you think they are.

 

Is this OK or am I entering some kind of garbage collection death-spiral that I don't know about?

Is there a better way or more "best-practice" way to change sprite textures? 

 

Also, I'm getting a slight flicker when the texture changes using the WebGL renderer. The canvas renderer is fine... I don't know if this is a Pixi bug or because I'm doing something really wrong?

 

 

 

 

 

Share this post


Link to post
Share on other sites

Best practise is to have one texture atlas with pre-defined regions:
texture.png - your texture atlas

texture.json - file describing regions

 

First define textures you want to use :

var texture01 = PIXI.Texture.fromFrame('frame01.png');var texture02 = PIXI.Texture.fromFrame('frame02.png');

Assign default texture to sprite :

var sprite = new PIXI.Sprite(texture01);

Later you use can easily assign other texture:

sprite.setTexture(texture02);

Share this post


Link to post
Share on other sites

Thanks for posting that!

I have a few more questions:

 

Is the texture atlas a single PNG file with individual regions described as ".png"?

Is there a technical reason why using a texture atlas and JSON file is better than shifting x/y source points on a base texture?

Is there any reason why `setTexture()` is better to use than `texture=` ? 

Share this post


Link to post
Share on other sites

Up!, how to switch a texture of a Sprite already loaded?

I get the error "Cannot read property 'hasLoaded' of undefined"

class Player extends PIXI.Sprite {
 constructor() {
 super()

 this.setTexture(PIXI.Texture.from('shaman1'))

 }
}

 

Share this post


Link to post
Share on other sites

Well, I think that is because the PIXI.Sprite class need to instantiated before to be able to use the .texture or .setTexture properties, I cannot set the texture on the constructor of "Player". The texture need to be assigned AFTER instantiate the Sprite (Player).

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...

  • Recently Browsing   0 members

    No registered users viewing this page.