d13 Posted May 20, 2014 Share Posted May 20, 2014 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? ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
bubamara Posted May 20, 2014 Share Posted May 20, 2014 Best practise is to have one texture atlas with pre-defined regions:texture.png - your texture atlastexture.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); Quote Link to comment Share on other sites More sharing options...
d13 Posted May 21, 2014 Author Share Posted May 21, 2014 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=` ? Quote Link to comment Share on other sites More sharing options...
Gerente Posted January 20, 2018 Share Posted January 20, 2018 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')) } } Quote Link to comment Share on other sites More sharing options...
themoonrat Posted January 21, 2018 Share Posted January 21, 2018 http://pixijs.io/examples/#/demos/texture-swap.js Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted January 21, 2018 Share Posted January 21, 2018 I guess `from` method returns undefined. Please use chrome tools debugger to see whats happenning there. Quote Link to comment Share on other sites More sharing options...
Gerente Posted January 21, 2018 Share Posted January 21, 2018 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). Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted January 21, 2018 Share Posted January 21, 2018 Its ok to do it in constructor. Find the other cause Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.