Moving Sprites
You now know how to display sprites, but how do you make them move? That’s easy: create a looping function using Pixi’s ticker
This is called a game loop. Any code you put inside the game loop will update 60 times per second. Here’s some code you could write to make the cat
sprite move to the right at a rate of 1 pixel per frame.
function setup() {
//Start the game loop by adding the `gameLoop` function to
//Pixi's `ticker` and providing it with a `delta` argument.
app.ticker.add(delta => gameLoop(delta));
}
function gameLoop(delta){
//Move the cat 1 pixel
cat.x += 1;
}
If you run this bit of code, you’ll see the sprite gradually move to the right side of the stage.
That’s because each time the gameLoop
runs, it adds 1 to the cat’s x position.
cat.x += 1;
Any function you add to Pixi’s ticker
will be called 60 times per second. You can see that the function is provided a delta
argument - what’s that?
The delta
value represents the amount of fractional lag between frames. You can optionally add it to the cat’s position, to make the cat’s animation independent of the frame rate. Here’s how:
cat.x += 1 + delta;
Whether or not you choose to add this delta
value is largely an aestheic choice. And the effect will only really be noticeable if your animation is struggling to keep up with a consistent 60 frames per second display rate (which might happen, for example, if it’s running on a slow device). The rest of the examples in this tutorial won’t use this delta
value, but feel free to use it in your own work if you wish.
You don’t have to use Pixi’s ticker to create a game loop. If you prefer, just use requestAnimationFrame
, like this:
function gameLoop() {
//Call this `gameLoop` function on the next screen refresh
//(which happens 60 times per second)
requestAnimationFrame(gameLoop);
//Move the cat
cat.x += 1;
}
//Start the loop
gameLoop();
It entirely up to you which style you prefer.
And that’s really all there is to it! Just change any sprite property by small increments inside the loop, and they’ll animate over time. If you want the sprite to animate in the opposite direction (to the left), just give it a negative value, like -1
.
You’ll find this code in the movingSprites.html
file - here’s the complete code:
//Aliases
let Application = PIXI.Application,
Container = PIXI.Container,
loader = PIXI.loader,
resources = PIXI.loader.resources,
TextureCache = PIXI.utils.TextureCache,
Sprite = PIXI.Sprite,
Rectangle = PIXI.Rectangle;
//Create a Pixi Application
let app = new Application({
width: 256,
height: 256,
antialias: true,
transparent: false,
resolution: 1
}
);
//Add the canvas that Pixi automatically created for you to the HTML document
document.body.appendChild(app.view);
loader
.add("images/cat.png")
.load(setup);
//Define any variables that are used in more than one function
let cat;
function setup() {
//Create the `cat` sprite
cat = new Sprite(resources["images/cat.png"].texture);
cat.y = 96;
app.stage.addChild(cat);
//Start the game loop
app.ticker.add(delta => gameLoop(delta));
}
function gameLoop(delta){
//Move the cat 1 pixel
cat.x += 1;
//Optionally use the `delta` value
//cat.x += 1 + delta;
}
(Notice that the cat
variable needs to be defined outside the setup
and gameLoop
functions so that you can access it inside both of them.)
You can animate a sprite’s scale, rotation, or size - whatever! You’ll see many more examples of how to animate sprites ahead.