HTML5 Phaser Tutorial: how to create a level selection screen with locked levels and stars

If you are planning to create a game with levels, you should also provide a level select screen. Also, in modern games, beating the level is not the main aim of the game, you will need to complete the level earning three stars if you want to say you really completed the level.

I am going to show you a Phaser prototype to create a multiple pages level select screen with scrolling and a shake effect if you try to play locked levels.

This is what we are going to build, the level select screen of a 60 levels game, with levels split on three 20-levels page:

It’s just a matter of managing buttons and sprite sheet, these are the graphic assets I used:

for the arrows, and

for level thumbnails.

Then, the fully commented script is very easy to understand and to expand:

var game = new Phaser.Game(320, 480, Phaser.AUTO, "", {preload: onPreload, create: onCreate});

// number of thumbnail rows
var thumbRows = 5;
// number of thumbnail cololumns
var thumbCols = 4;
// width of a thumbnail, in pixels
var thumbWidth = 64;
// height of a thumbnail, in pixels
var thumbHeight = 64;
// space among thumbnails, in pixels
var thumbSpacing = 8;
// array with finished levels and stars collected.
// 0 = playable yet unfinished level
// 1, 2, 3 = level finished with 1, 2, 3 stars
// 4 = locked
var starsArray = [1,2,1,2,3,3,3,2,2,1,3,1,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4];
// how many pages are needed to show all levels?
// CAUTION!! EACH PAGE SHOULD HAVE THE SAME AMOUNT OF LEVELS, THAT IS
// THE NUMBER OF LEVELS *MUST* BE DIVISIBLE BY THUMBCOLS*THUMBROWS
var pages = starsArray.length/(thumbRows*thumbCols);
// group where to place all level thumbnails
var levelThumbsGroup;
// current page
var currentPage = 0;
// arrows to navigate through level pages
var leftArrow;
var rightArrow;

// preloading graphic assets
function onPreload() {
	game.load.spritesheet("levels", "levels.png", 64, 64);
	game.load.spritesheet("level_arrows", "level_arrows.png", 48, 48);
}

// going fullscreen
function goFullScreen(){
	game.scale.pageAlignHorizontally = true;
	game.scale.pageAlignVertically = true;
	game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
	game.scale.setScreenSize(true);
}

function onCreate() {
	goFullScreen();
	// placing left and right arrow buttons, will call arrowClicked function when clicked
	leftArrow = game.add.button(50,420,"level_arrows",arrowClicked);
	leftArrow.anchor.setTo(0.5);
	leftArrow.frame = 0;
	leftArrow.alpha = 0.3;
	rightArrow = game.add.button(270,420,"level_arrows",arrowClicked);
	rightArrow.anchor.setTo(0.5);
	rightArrow.frame = 1;
	// creation of the thumbails group
	levelThumbsGroup = game.add.group();
	// determining level thumbnails width and height for each page
	var levelLength = thumbWidth*thumbCols+thumbSpacing*(thumbCols-1);
	var levelHeight = thumbWidth*thumbRows+thumbSpacing*(thumbRows-1);
	// looping through each page
	for(var l = 0; l < pages; l++){
		// horizontal offset to have level thumbnails horizontally centered in the page
		var offsetX = (game.width-levelLength)/2+game.width*l;
		// I am not interested in having level thumbnails vertically centered in the page, but
		// if you are, simple replace my "20" with
		// (game.height-levelHeight)/2
		var offsetY = 20;
		// looping through each level thumbnails
	     for(var i = 0; i < thumbRows; i ++){
	     	for(var j = 0; j < thumbCols; j ++){  
	     		// which level does the thumbnail refer?
				var levelNumber = i*thumbCols+j+l*(thumbRows*thumbCols);
				// adding the thumbnail, as a button which will call thumbClicked function if clicked   		
				var levelThumb = game.add.button(offsetX+j*(thumbWidth+thumbSpacing), offsetY+i*(thumbHeight+thumbSpacing), "levels", thumbClicked, this);	
				// shwoing proper frame
				levelThumb.frame=starsArray[levelNumber];
				// custom attribute 
				levelThumb.levelNumber = levelNumber+1;
				// adding the level thumb to the group
				levelThumbsGroup.add(levelThumb);
				// if the level is playable, also write level number
				if(starsArray[levelNumber]<4){
					var style = {
						font: "18px Arial",
						fill: "#ffffff"
					};
					var levelText = game.add.text(levelThumb.x+5,levelThumb.y+5,levelNumber+1,style);
					levelText.setShadow(2, 2, 'rgba(0,0,0,0.5)', 1);
					levelThumbsGroup.add(levelText);
				}
			}
		}
	}
}

function thumbClicked(button){
	// the level is playable, then play the level!!
	if(button.frame < 4){
		alert("playing level " + button.levelNumber);
	}
	// else, let's shake the locked levels
	else{
		var buttonTween = game.add.tween(button)
		buttonTween.to({
			x: button.x+thumbWidth/15
		}, 20, Phaser.Easing.Cubic.None);
		buttonTween.to({
			x: button.x-thumbWidth/15
		}, 20, Phaser.Easing.Cubic.None);
		buttonTween.to({
			x: button.x+thumbWidth/15
		}, 20, Phaser.Easing.Cubic.None);
		buttonTween.to({
			x: button.x-thumbWidth/15
		}, 20, Phaser.Easing.Cubic.None);
		buttonTween.to({
			x: button.x
		}, 20, Phaser.Easing.Cubic.None);
		buttonTween.start();
	}
}


function arrowClicked(button){
	// touching right arrow and still not reached last page
	if(button.frame==1 && currentPage<pages-1){
		leftArrow.alpha = 1;
		currentPage++;
		// fade out the button if we reached last page
		if(currentPage == pages-1){
			button.alpha = 0.3;
		}
		// scrolling level pages
		var buttonsTween = game.add.tween(levelThumbsGroup);
		buttonsTween.to({
			x: currentPage * game.width * -1
		}, 500, Phaser.Easing.Cubic.None);
		buttonsTween.start();
	}
	// touching left arrow and still not reached first page
	if(button.frame==0 && currentPage>0){
		rightArrow.alpha = 1;
		currentPage--;
		// fade out the button if we reached first page
		if(currentPage == 0){
			button.alpha = 0.3;
		}
		// scrolling level pages
		var buttonsTween = game.add.tween(levelThumbsGroup);
		buttonsTween.to({
			x: currentPage * game.width * -1
		}, 400, Phaser.Easing.Cubic.None);
		buttonsTween.start();
	}		
}

Now, you just have to locally save completed levels to allow players to continue playing starting from where they left. You can also download the source code of the entire project.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers