Make a sprite from a tileset sub-image
You now know how to make a sprite from a single image file. But, as a game designer, you’ll usually be making your sprites using tilesets (also known as spritesheets.) Pixi has some convenient built-in ways to help you do this. A tileset is a single image file that contains sub-images. The sub-images represent all the graphics you want to use in your game. Here’s an example of a tileset image that contains game characters and game objects as sub-images.
The entire tileset is 192 by 192 pixels. Each image is in its own 32 by 32 pixel grid cell. Storing and accessing all your game graphics on a tileset is a very processor and memory efficient way to work with graphics, and Pixi is optimized for this.
You can capture a sub-image from a tileset by defining a rectangular area that’s the same size and position as the sub-image you want to extract. Here’s an example of the rocket sub-image that’s been extracted from the tileset.
Let’s look at the code that does this. First, load the tileset.png
image with Pixi’s loader
, just as you’ve done in earlier examples.
loader
.add("images/tileset.png")
.load(setup);
Next, when the image has loaded, use a rectangular sub-section of the tileset to create the sprite’s image. Here’s the code that extracts the sub image, creates the rocket sprite, and positions and displays it on the canvas.
function setup() {
//Create the `tileset` sprite from the texture
let texture = TextureCache["images/tileset.png"];
//Create a rectangle object that defines the position and
//size of the sub-image you want to extract from the texture
//(`Rectangle` is an alias for `PIXI.Rectangle`)
let rectangle = new Rectangle(192, 128, 64, 64);
//Tell the texture to use that rectangular section
texture.frame = rectangle;
//Create the sprite from the texture
let rocket = new Sprite(texture);
//Position the rocket sprite on the canvas
rocket.x = 32;
rocket.y = 32;
//Add the rocket to the stage
app.stage.addChild(rocket);
//Render the stage
app.renderer.render(app.stage);
}
How does this work?
Pixi has a built-in Rectangle
object (PIXI.Rectangle
) that is a general-purpose object for defining rectangular shapes. It takes four arguments. The first two arguments define the rectangle’s x
and y
position. The last two define its width
and height
. Here’s the format for defining a new Rectangle
object.
let rectangle = new PIXI.Rectangle(x, y, width, height);
The rectangle object is just a data object; it’s up to you to decide how you want to use it. In our example we’re using it to define the position and area of the sub-image on the tileset that we want to extract. Pixi textures have a useful property called frame
that can be set to any Rectangle
objects. The frame
crops the texture to the dimensions of the Rectangle
. Here’s how to use frame
to crop the texture to the size and position of the rocket.
let rectangle = new Rectangle(192, 128, 64, 64);
texture.frame = rectangle;
You can then use that cropped texture to create the sprite:
let rocket = new Sprite(texture);
And that’s how it works!
Because making sprite textures from a tileset is something you’ll do with great frequency, Pixi has a more convenient way to help you do this - let’s find out what that is next.