Jump to content

Is this an efficient way to load sprites?


Recommended Posts

var mySpriteSheetImage  = PIXI.BaseTexture.fromImage("img/mySpriteSheet.png");

var spriteTexture1 = new PIXI.Texture(mySpriteSheetImage, new PIXI.Rectangle(x:0, y:0, width:32, height:32));

var spriteTexture2 = new PIXI.Texture(mySpriteSheetImage, new PIXI.Rectangle(x:32, y:0, width:32, height:32));

var spriteTexture3 = new PIXI.Texture(mySpriteSheetImage, new PIXI.Rectangle(x:64, y:0, width:32, height:32));

var mySprite = new PIXI.Sprite(spriteTexture1);


The code above loads many sprites from a sprite sheet. This code was found here. Is there a better way to get the same functionality?

Link to comment
Share on other sites

If you have a manifest file that describes the rectangles of your spritesheet the loader can automatically create all those textures for you. The loader does some cool things, like loading the image and creating a texture for you.

fromImage is a shortcut that loads the image and creates a texture. The loader is more powerful in that it supports middleware and can do lots of different pre/post processing on loaded resources. Which is why you can pass it a JSON file from TexturePacker, for example, and the loader will auto detect the image, load it, and create a texture for each frame. It does this by running code very much like what you have above, it just does it for you automatically.


Link to comment
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.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...