Imaginez ceci : une journée d’automne, tout le monde essaie de trouver une place de parking dans cette rue bizarre près de la poste à Fontana. Moi, bien sûr, en conductrice expérimentée, je décide de tourner dans une petite ruelle pour chercher une place. Oups, pas de place ! Rien de grave, je tourne, mais… dans le rétroviseur, juste derrière moi, apparaît une grosse voiture. Et pendant qu’elle roule, je commence un peu à m’énerver, parce que quelqu’un me suit sans cesse – qu’est-ce que je suis, un GPS ?
Au moment où ça m’énervait vraiment, je tourne le volant, je m’arrête et je me dis : “Allez, je vais lui laisser la chance de passer.” Et bien sûr, il passe, mais je n’ai pas abandonné ! Et voilà, je trouve deux places de parking ! Ravie, je m’engouffre dans l’une d’elles, pensant que c’est ma place chanceuse.
Et bien sûr, pendant que je manœuvre pour me garer, cette même voiture se gare juste derrière moi. Je me dis : “Ah, c’est sûrement un conducteur courtois, il va être poli.” Mais non ! La femme dans cette voiture sort et me regarde comme si je venais tout droit de la jungle.
“Vous faites l’idiote, ou vous êtes vraiment idiote ?” me dit-elle. Ce à quoi, je dois avouer, je ne m’attendais pas vraiment dans ce coin tranquille de la ville.
Moi, bien sûr, je réponds avec tout mon charme : “Je suis idiote”, comme si je me plaignais de ne pas avoir d’autres répliques drôles prêtes pour elle. Et là, commence le véritable drame ! Puis elle ouvre son coffre, et je me dis : “Qu’est-ce qui va se passer maintenant ? On dirait une scène de film d’action !” Une batte de baseball toute neuve, encore dans son emballage ! Et puis, comme dans une scène de thriller, elle me dit :
“Je vais te défoncer la voiture !”
Je lui réponds : “Je ne vais pas me disputer pour une place de parking !” Parce que sérieusement, pour se disputer à propos d’une place de parking, il faudrait d’abord devenir un pro dans ce domaine. Et elle se déplace, je sors de la voiture comme si je faisais une danse élégante, sans abîmer la voiture.
In this blog post, I’ll walk you through the process I used to create this parallax effect in Phaser 3, along with the code snippets to bring it all together.
All the illustrations and artwork in the game are entirely my original work, created both digitally and on paper. These illustrations have been with me for about 15 years, and they’ve finally found their place in this interactive world.
For my Halloween-themed game Witch's Night, I wanted to achieve a dynamic parallax effect that would create depth and movement in the scene. The effect would consist of three layers—foreground, midground, and background—each scrolling at different speeds. I also integrated interactive objects within the foreground layer, making the scene come alive with animations and sound effects.
The Setup
I structured the parallax effect using Phaser’s tileSprite objects, which are ideal for scrolling backgrounds. Here’s a quick overview of how the layers work:
Background: Moves the slowest, giving the impression of distant scenery.
Midground: Moves slightly faster than the background.
Foreground: Moves the fastest and includes interactive objects that respond to user inputs.
Let’s take a look at the core function responsible for setting up the parallax:
To create the scrolling effect, I update the x-coordinates of each layer in the update method, where the speed varies depending on the depth of the layer:
This results in a smooth parallax effect where each layer moves at a different speed, enhancing the sense of depth.
Adding Interactive Elements
I wanted the player to feel engaged with the scene, so I added various interactive objects—witches, trees, a piano, and more—that respond to user actions like clicks and hovers. Using a custom utility function, setupInteractiveAnimation, I ensured that each object has unique animations and sounds.
Example: Interactive Piano
For the interactive piano, I created an animation and played a sound effect whenever the player clicks on it. Here’s the setup:
// Create static piano image and set interactivity
this.staticPiano = this.add.image(970, 600, "staticPiano").setInteractive({ cursor: "pointer" }).setDepth(10);
// Define piano animation
this.anims.create({
key: "pianoAnim",
frames: this.anims.generateFrameNumbers("pianoAnim", { start: 0, end: 4 }),
frameRate: 3,
repeat: 3
});
// Use utility function to handle interactivity and animation
setupInteractiveAnimation(
this,
this.staticPiano,
"pianoAnim",
"pianoSound",
this.staticPiano.x,
this.staticPiano.y,
10,
1 // scale
);
The setupInteractiveAnimation function simplifies setting up animations and sounds for each object, making it easy to add interactive elements with minimal code repetition.
Interactive Lantern
I also added a glowing lantern in the foreground. It gently sways back and forth, and when clicked, it reveals a hidden clue. This small interaction adds an element of mystery:
this.lantern = this.add.image(100, 0, "lantern")
.setOrigin(0.5, 0)
.setDepth(19)
.setInteractive({ cursor: "pointer" });
this.tweens.add({
targets: this.lantern,
angle: { from: -7, to: 7 },
duration: 2000,
yoyo: true,
repeat: -1,
ease: "Sine.easeInOut"
});
// Show clue on click
this.lantern.on("pointerdown", () => {
this.showClue("yellow"); // Custom function to show clues
});
The combination of parallax and interactivity resulted in a rich and engaging visual experience for the player.
Conclusion
The parallax effect in Witch's Night, not only brings depth to the scene but also sets the stage for an interactive environment filled with surprises. With a few tweaks, you can create similar effects for your own projects using Phaser 3!
If you’re interested in trying out the game or have any questions about the code, feel free to drop a comment below or check out the live demo here.
Something there is that doesn’t love a wall, That sends the frozen-ground-swell under it, And spills the upper boulders in the sun; And makes gaps even two can pass abreast. The work of hunters is another thing: I have come after them and made repair Where they have left not one stone on a stone, But they would have the rabbit out of hiding, To please the yelping dogs. The gaps I mean, No one has seen them made or heard them made, But at spring mending-time we find them there. I let my neighbor know beyond the hill; And on a day we meet to walk the line And set the wall between us once again. We keep the wall between us as we go. To each the boulders that have fallen to each. And some are loaves and some so nearly balls We have to use a spell to make them balance: ‘Stay where you are until our backs are turned!’ We wear our fingers rough with handling them. Oh, just another kind of out-door game, One on a side. It comes to little more: There where it is we do not need the wall: He is all pine and I am apple orchard. My apple trees will never get across And eat the cones under his pines, I tell him. He only says, ‘Good fences make good neighbors.’ Spring is the mischief in me, and I wonder If I could put a notion in his head: ‘Why do they make good neighbors? Isn’t it Where there are cows? But here there are no cows. Before I built a wall I’d ask to know What I was walling in or walling out, And to whom I was like to give offense. Something there is that doesn't love a wall, That wants it down.’ I could say ‘Elves’ to him, But it’s not elves exactly, and I’d rather He said it for himself. I see him there Bringing a stone grasped firmly by the top In each hand, like an old-stone savage armed. He moves in darkness as it seems to me, Not of woods only and the shade of trees. He will not go behind his father’s saying, And he likes having thought of it so well He says again, ‘Good fences make good neighbors.’
In the Northern Hemisphere, the December Solstice is the winter solstice and the shortest day of the year. In the Southern Hemisphere, it is summer solstice and the longest day of the year.
The term solstice comes from the Latin word solstitium, meaning ‘the Sun stands still’. This is because on this day, the Sun reaches its southern-most position as seen from the Earth. The Sun seems to stand still at the Tropic of Capricorn and then reverses its direction.
It’s also common to call it the day the Sun turns around.