Skip to content

Instantly share code, notes, and snippets.

@sajmoni
Last active May 28, 2019 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajmoni/300d65e73ff8cc658ffdfb257b0722aa to your computer and use it in GitHub Desktop.
Save sajmoni/300d65e73ff8cc658ffdfb257b0722aa to your computer and use it in GitHub Desktop.
Function to get a texture from Pixi's resource loader
// "app" is an instance of PixiJS Application
export default app => (filename) => {
const {
resources,
} = app.loader;
const texture = Object
.values(resources)
.filter(resource => resource.textures)
.flatMap(resource => Object.entries(resource.textures))
.find(([key]) => key === `${filename}.png`);
if (!texture) throw new Error(`Texture "${filename}" not found.`);
return texture[1];
};
// Example usage, assuming you have an image called 'player':
// const texture = getTexture(‘player’)
// const sprite = new Pixi.Sprite(texture)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment