Previous: Chapter 4: Managing Game Scenes and Screens

Sprites and animations breathe life into your Pixi.js games. In this chapter, we’ll explore how to create and manipulate sprites, bringing your game characters, objects, and effects to life.

Creating and Manipulating Sprites

A sprite is a 2D image or texture that can be displayed and manipulated in Pixi.js. Sprites are the fundamental building blocks for characters, enemies, items, and more in your game.

Creating Sprites

To create a sprite, you need a texture. Textures can be loaded from images, generated dynamically, or created from other sprites. Here’s how to create a sprite using a loaded texture:

// Load a texture (as shown in Chapter 3)
const texture = PIXI.Texture.from('character.png');

// Create a sprite using the texture
const characterSprite = new PIXI.Sprite(texture);

// Position the sprite on the stage
characterSprite.x = 100;
characterSprite.y = 200;

// Add the sprite to the stage
app.stage.addChild(characterSprite);

In this example, we load a texture from an image and create a sprite using that texture.

Manipulating Sprites

You can manipulate sprites in various ways, including:

  • Positioning: Set the x and y properties to change the sprite’s position on the stage.
  • Scaling: Adjust the scale.x and scale.y properties to change the sprite’s size.
  • Rotation: Use the rotation property to rotate the sprite (in radians).
  • Tinting: Apply a color tint using the tint property.
  • Alpha: Change the sprite’s transparency with the alpha property.

Here’s an example that demonstrates these transformations:

const characterSprite = new PIXI.Sprite(texture);

// Position the sprite
characterSprite.x = 100;
characterSprite.y = 200;

// Scale the sprite
characterSprite.scale.x = 0.5;
characterSprite.scale.y = 0.5;

// Rotate the sprite
characterSprite.rotation = Math.PI / 4; // 45-degree rotation

// Apply a tint (in hexadecimal)
characterSprite.tint = 0xff0000; // Red tint

// Adjust the sprite's transparency
characterSprite.alpha = 0.7; // 70% opacity

app.stage.addChild(characterSprite);

Anchor Points

Sprites have an anchor point that serves as the pivot for transformations like scaling and rotation. By default, the anchor point is at (0, 0), which means transformations occur around the sprite’s top-left corner. You can change the anchor point by setting the anchor property:

characterSprite.anchor.set(0.5, 0.5); // Centered anchor point

In this example, we set the anchor point to the center of the sprite.

Animating Sprites

Animations in Pixi.js involve changing the appearance of a sprite over time. You can achieve animations through frame-by-frame animation, sprite sheets, or by manipulating the sprite’s texture.

Frame-by-Frame Animation

Frame-by-frame animation involves switching between different textures to create the illusion of motion. You can achieve this by using an array of textures and updating the sprite’s texture over time.

Here’s an example of frame-by-frame animation:

const textures = [
    PIXI.Texture.from('frame1.png'),
    PIXI.Texture.from('frame2.png'),
    PIXI.Texture.from('frame3.png'),
];

const characterSprite = new PIXI.Sprite(textures[0]);
app.stage.addChild(characterSprite);

let frameIndex = 0;

// Animation loop
app.ticker.add(() => {
    frameIndex = (frameIndex + 1) % textures.length;
    characterSprite.texture = textures[frameIndex];
});

In this example, we create an array of textures and update the sprite’s texture in a loop to create animation.

Sprite Sheets

Sprite sheets are single images containing multiple frames or sprites. You can use Pixi.js’s PIXI.Spritesheet class to load and manage sprite sheets.

const loader = PIXI.Loader.shared;

loader.add('spritesheet', 'spritesheet.json').load((loader, resources) => {
    // Create a sprite from a sprite sheet
    const spriteSheet = new PIXI.Spritesheet(resources.spritesheet.textures);
    spriteSheet.parse(() => {
        const characterSprite = new PIXI.AnimatedSprite(spriteSheet.animations['character']);
        characterSprite.play(); // Start the animation
        app.stage.addChild(characterSprite);
    });
});

In this example, we load a sprite sheet and create an animated sprite from it.

Spine and DragonBones

Pixi.js also supports skeletal animations through libraries like Spine and DragonBones. These libraries allow you to create complex character animations with bones and animations defined in dedicated software.

Conclusion

Sprites and animations are fundamental to creating engaging and interactive games with Pixi.js. In this chapter, you’ve learned how to create and manipulate sprites, apply transformations, and implement animations using frame-by-frame animation, sprite sheets, and even skeletal animations.

With these skills, you can bring your game characters and objects to life, adding depth and immersion to your Pixi.js games. In the next chapter, we’ll explore working with text and fonts to create dynamic text elements in your games and applications.

Next: