This lesson was updated. Reset to the new version?
This browser isn't keeping your edits, so they're lost when you leave the page.
Angry-Birds-8 finishes the game. It opens on a title screen with the theme playing, and the ambience track takes over once the player can aim. There’s a pause menu, a debug mode, and a level editor, and the cursor shows a hand over the bird. This is the template your assignment starts from.
The Title Screen
Browsers won’t play sound on a page the player hasn’t interacted with yet. TitleScreenState in src/states/TitleScreenState.js asks for the theme and waits for a key or a click before starting level 1. That first click is what lets every sound after it play, and the theme starts on it.
Sounds and Music
Earlier games played sounds through SoundPool, which kept a few <audio> elements for each sound, so one could start while another copy was still playing. Audio elements have two weaknesses, though. They can start a little late, and they can’t loop part of a file, or start one file the instant another ends without a small gap.
This game’s lib/Sounds.js plays through the Web Audio API instead. Each file is decoded once, and every play() starts a new copy of it straight away, so there’s no pool to keep, and a sound definition no longer needs a size. A looping sound only ever plays one copy at a time. You call it the same way as before, with game.sounds.play(name).
game.music is an instance of lib/Music.js. It keeps one track playing at a time, and src/config/music.js lists the tracks. Each track is one file. The theme’s loopStart is 5.598, so it plays from the start once, then jumps back to 5.598 s every time it reaches the end, so the opening isn’t repeated. A track can also set loopEnd to repeat less than the whole rest of the file. music.play() leaves a track that’s already on alone, so startAiming() in src/objects/Level.js can ask for the ambience whenever aiming starts, and LevelEndState calls music.stop(). A track asked for before the first click waits for it.
Play the last few seconds of the theme to hear it jump back to loopStart:
The Pause Menu
The pause button in the corner, Escape, or P opens PauseMenu in src/objects/PauseMenu.js, which is given. It slides in from the left, and its buttons resume, restart the level, mute, open the editor, and switch debug mode. While it’s open, PlayState updates the menu instead of the level, so the physics stops.
Muting sets SoundSettings.isMuted, which playSound() in src/services/sound.js checks, and passes it to music.setMuted(), which pauses the track and picks it back up on unmute.
isOverButton() and renderIconButton() in src/services/button.js are shared by the pause button and the menu.
Debug Mode
Press Q, use the pause menu, or click the debug button above the preview to switch debug mode. All three show or hide the game’s debug panel, this.game.debug. Debug mode is on whenever the debug panel shows, which render() in PlayState checks. renderFrame() in src/entities/GameEntity.js then also calls renderDebug(), which outlines each body from the corners Matter keeps in body.vertices, so you can see the shape Matter is really colliding, whatever the art looks like. render() in src/objects/Fortress.js also calls renderHealth() from src/entities/Breakable.js on every block and pig, which writes its health above it, like 2/4, so every hit shows even when the damage frame barely changes. It does this after drawing them all, so no block in a stack hides another’s health. The panel lists how many birds, blocks, pigs, and bodies there are, from the this.game.debug.watch() call in PlayState.
Pro tip: Turn it on while you build the assignment’s birds.
The Level Editor
Press E in a level to open the editor, in src/editor/. It pauses the physics, and lets you drag in blocks, pigs, and mounds from a palette, move and turn them, and draw mounds point by point. Test plays your level, and goes back to the editor when it ends. Copy puts the level on the clipboard, ready to paste into src/config/levels.js.
The editor is not part of the tutorial, so you don’t need to read its code. A level being tested is built with isTest, which skips the Preview phase.
Cursors
setCursor() in src/services/cursor.js sets the canvas’s CSS cursor. PlayState calls it every frame with whatever the level says, and the browser ignores a value it already has. Level.getCursor() answers with a hand, open over the loaded bird and closed while dragging it. this.game.input.getMousePosition() is on the canvas, in screen space. Only the sling’s Matter mouse is aligned to the world, so this.camera.toWorld() in src/services/Camera.js turns a canvas position into a world one.
Lend a hand
The cursor never changes, even with the bird in your grip. getCursor() in src/objects/Level.js always answers CursorName.Default.
- Outside the Aiming phase, return
CursorName.Default. - While
this.slingshot.slingMouse.isDragging(), returnCursorName.Grabbing. - Otherwise, pass
this.game.input.getMousePosition()tothis.camera.toWorld()to get the mouse in world space. IfslingMouse.isOverBird()with that point, returnCursorName.Grab, and if not,CursorName.Default.
The Challenge tab gets a ✓ once you’re dragging the bird with a closed hand.
Check the phase first, against LevelPhase.Aiming, so nothing else runs outside it.
Check isDragging() before isOverBird(). A dragged bird is under the mouse too, so the other order would show an open hand.
If the open hand only shows before the camera moves, the mouse is still in screen space. Give isOverBird() the point from this.camera.toWorld().
Replace the TODO and the return under it with:
if (this.phase !== LevelPhase.Aiming) { return CursorName.Default;}
const { slingMouse } = this.slingshot;
if (slingMouse.isDragging()) { return CursorName.Grabbing;}
const mouse = this.camera.toWorld(this.game.input.getMousePosition());
return slingMouse.isOverBird(mouse) ? CursorName.Grab : CursorName.Default;