Sprites are fundamental building blocks in 2D game development. In Phaser, sprites represent images or animations that can be positioned, moved, and interacted with in your game world. This section will guide you through the process of adding sprites to your game and making them move, both manually and using Phaser’s built-in features.
- What is a Sprite?
- Definition: A sprite is a 2D image or animation integrated into a larger scene.
- Usage: Sprites are used for characters, objects, enemies, collectibles, and more.
- Phaser Sprite Object: In Phaser, sprites are created using the
this.add.sprite
method within a scene.
- Adding a Sprite to the Scene
Step-by-Step Example
Let’s add a simple sprite to your game scene.
1. Load the Sprite Image
Before you can add a sprite, you need to load its image in the preload
method:
Explanation:
this.load.image
tells Phaser to load an image with the key'player'
from the specified path.- The key
'player'
will be used to reference this image later.
2. Add the Sprite in the Scene
Add the sprite in the create
method:
Explanation:
this.add.sprite(x, y, key)
creates a sprite at position(x, y)
using the image loaded with the key'player'
.- The sprite is stored in
this.player
for later use.
- Moving Sprites
There are several ways to move sprites in Phaser:
A. Manual Movement (Changing Position Directly)
You can change the sprite’s position by updating its x
and y
properties in the update
method:
Explanation:
- The
update
method runs every frame. - Increasing
x
moves the sprite right; decreasing moves it left. - Similarly, modify
y
to move up or down.
B. Moving with Keyboard Input
Let’s move the sprite using arrow keys.
1. Set Up Cursor Keys
create() { this.player = this.add.sprite(100, 150, 'player'); this.cursors = this.input.keyboard.createCursorKeys(); }
2. Update Position Based on Input
update() { if (this.cursors.left.isDown) { this.player.x -= 2; } else if (this.cursors.right.isDown) { this.player.x += 2; } if (this.cursors.up.isDown) { this.player.y -= 2; } else if (this.cursors.down.isDown) { this.player.y += 2; } }
Explanation:
this.input.keyboard.createCursorKeys()
creates an object to track arrow key presses.- In
update
, check which key is pressed and adjust the sprite’s position accordingly.
C. Using Physics for Movement
Phaser’s physics system can handle movement and collisions. Here’s a basic example using Arcade Physics:
1. Enable Physics on the Sprite
create() { this.player = this.physics.add.sprite(100, 150, 'player'); this.cursors = this.input.keyboard.createCursorKeys(); }
2. Move with Velocity
update() { this.player.setVelocity(0); if (this.cursors.left.isDown) { this.player.setVelocityX(-160); } else if (this.cursors.right.isDown) { this.player.setVelocityX(160); } if (this.cursors.up.isDown) { this.player.setVelocityY(-160); } else if (this.cursors.down.isDown) { this.player.setVelocityY(160); } }
Explanation:
this.physics.add.sprite
creates a sprite with physics enabled.setVelocityX
andsetVelocityY
control the speed and direction.- Setting velocity to 0 at the start of
update
stops the sprite when no key is pressed.
- Comparison Table: Sprite Movement Methods
Method | Code Example Location | Use Case | Pros | Cons |
---|---|---|---|---|
Manual (x/y update) | update() |
Simple movement, no physics | Easy to implement | No collision/physics |
Keyboard Input | update() |
Player-controlled movement | Interactive | Needs input handling |
Physics-based | create() , update() |
Realistic movement, collisions | Physics, collisions, gravity | More setup required |
- Practical Exercise
Exercise: Add and Move a Sprite with Arrow Keys
Task:
- Load an image called
'star'
from'assets/star.png'
. - Add the sprite to the scene at position (200, 200).
- Allow the player to move the sprite using the arrow keys.
Starter Code:
class MyGame extends Phaser.Scene { preload() { // 1. Load the image here } create() { // 2. Add the sprite and set up input here } update() { // 3. Move the sprite with arrow keys here } }
Solution
class MyGame extends Phaser.Scene { preload() { this.load.image('star', 'assets/star.png'); } create() { this.star = this.add.sprite(200, 200, 'star'); this.cursors = this.input.keyboard.createCursorKeys(); } update() { if (this.cursors.left.isDown) { this.star.x -= 2; } else if (this.cursors.right.isDown) { this.star.x += 2; } if (this.cursors.up.isDown) { this.star.y -= 2; } else if (this.cursors.down.isDown) { this.star.y += 2; } } }
Common Mistakes & Tips:
- Mistake: Forgetting to load the image in
preload
will cause the sprite not to appear. - Tip: Always check the image path and key.
- Mistake: Not storing the sprite in a variable (e.g.,
this.star
) makes it hard to reference later. - Tip: Use
this
to store important game objects for access in other methods.
- Summary
- Sprites are essential for representing objects in your game.
- You add sprites using
this.add.sprite
(orthis.physics.add.sprite
for physics). - Sprites can be moved by directly changing their position, responding to input, or using physics.
- Practice adding and moving sprites to build interactive and dynamic games.
Next Up:
You’ll learn how to animate sprites, bringing your game characters and objects to life!
Phaser - Game Development with JavaScript
Module 1: Introduction to Game Development and Phaser
- What is Game Development?
- Overview of Phaser
- Setting Up Your Development Environment
- Your First Phaser Project
Module 2: Phaser Basics
- Understanding the Game Loop
- Game Configuration and Scenes
- Loading and Displaying Images
- Working with Text
- Handling Input (Keyboard and Mouse)
Module 3: Sprites and Animation
Module 4: Game Physics and Interactivity
- Introduction to Physics in Phaser
- Enabling Physics on Sprites
- Collisions and Overlaps
- Interactive Objects and Events