Skip to content

Instantly share code, notes, and snippets.

@sajmoni
Last active May 28, 2019
Embed
What would you like to do?
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