HTML5 Phaser Tutorial: how to create a level selection screen with locked levels and stars – finished prototype
Talking about Game development, HTML5, Javascript and Phaser.
Some days ago I showed you how to create an HTML5 level selection screen with locked levels and stars with Phaser.
In the original example, completed level and stars situation was hardcoded, this time we will let the player “solve” the levels, unlock new levels, gain stars and so on.
Also, the prototype will be coded using states rather than having all code in one single file. This will allow to reuse the code more easily. If you aren’t familiar with Phaser states, check Phaser Tutorial: understanding Phaser states.
First, let’s have a look at the result, running fine using the new Phaser 2.2.1 version:
Play levels, complete them to earn as much stars as you can and unlock new levels as you progress in the game.
Here are the files I am using:

There isn’t that much to say about the images, let’s have a look at the files, bearing in mind most of the mechanic has already been explained in previous post.
index.html is the HTML page our game will run in:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body{
background: #000000;
padding:0px;
margin:0px;
}
</style>
<script src="phaser.min.js"></script>
<script src="loading.js"></script>
<script src="levelselect.js"></script>
<script src="playlevel.js"></script>
<script src="game.js"></script>
</head>
<body>
</body>
</html>
game.js contains game declaration, global variables and states:
var game = new Phaser.Game(320, 480, Phaser.AUTO, "");
game.global = {
thumbRows : 5,
// number of thumbnail cololumns
thumbCols : 4,
// width of a thumbnail, in pixels
thumbWidth : 64,
// height of a thumbnail, in pixels
thumbHeight : 64,
// space among thumbnails, in pixels
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
starsArray : [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,4,4,4,4,4,4,4,4,4,4,4,4],
// level currently playing
level : 0
}
// game states
game.state.add("Loading", loading);
game.state.add("LevelSelect", levelSelect);
game.state.add("PlayLevel", playLevel);
// we'll start loading
game.state.start("Loading");
loading.js preloads graphic assets:
loading = {
init: function(){
// going fullscreen
game.scale.pageAlignHorizontally = true;
game.scale.pageAlignVertically = true;
game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
game.scale.setScreenSize(true);
},
preload: function(){
// preloading various assets
game.load.spritesheet("levels", "levels.png", game.global.thumbWidth, game.global.thumbHeight);
game.load.spritesheet("level_arrows", "level_arrows.png", 48, 48);
game.load.spritesheet("game", "game.png", 200, 80);
},
create: function(){
// going to level select state
game.state.start("LevelSelect");
}
}
levelselect.js is the core of the prototype and manages level selection screen:
// how many pages are needed to show all levels?
var pages;
// group where to place all level thumbnails
var levelThumbsGroup;
// current page
var currentPage;
// arrows to navigate through level pages
var leftArrow;
var rightArrow;
levelSelect = {
create: function(){
// 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
pages = game.global.starsArray.length/(game.global.thumbRows*game.global.thumbCols);
// current page according to last played level, if any
currentPage = Math.floor(game.global.level/(game.global.thumbRows*game.global.thumbCols));
if(currentPage>pages-1){
currentPage = pages-1;
}
// left arrow button, to turn one page left
leftArrow = game.add.button(50,420,"level_arrows",this.arrowClicked,this);
leftArrow.anchor.setTo(0.5);
leftArrow.frame = 0;
// can we turn one page left?
if(currentPage==0){
leftArrow.alpha = 0.3;
}
// right arrow button, to turn one page right
rightArrow = game.add.button(270,420,"level_arrows",this.arrowClicked,this);
rightArrow.anchor.setTo(0.5);
rightArrow.frame = 1;
// can we turn one page right?
if(currentPage==pages-1){
rightArrow.alpha = 0.3;
}
// creation of the thumbails group
levelThumbsGroup = game.add.group();
// determining level thumbnails width and height for each page
var levelLength = game.global.thumbWidth*game.global.thumbCols+game.global.thumbSpacing*(game.global.thumbCols-1);
var levelHeight = game.global.thumbWidth*game.global.thumbRows+game.global.thumbSpacing*(game.global.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 < game.global.thumbRows; i ++){
for(var j = 0; j < game.global.thumbCols; j ++){
// which level does the thumbnail refer?
var levelNumber = i*game.global.thumbCols+j+l*(game.global.thumbRows*game.global.thumbCols);
// adding the thumbnail, as a button which will call thumbClicked function if clicked
var levelThumb = game.add.button(offsetX+j*(game.global.thumbWidth+game.global.thumbSpacing), offsetY+i*(game.global.thumbHeight+game.global.thumbSpacing), "levels", this.thumbClicked, this);
// shwoing proper frame
levelThumb.frame=game.global.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(game.global.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);
}
}
}
}
// scrolling thumbnails group according to level position
levelThumbsGroup.x = currentPage * game.width * -1
},
arrowClicked:function(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();
}
},
thumbClicked:function(button){
// the level is playable, then play the level!!
if(button.frame < 4){
game.global.level = button.levelNumber;
game.state.start("PlayLevel");
}
// else, let's shake the locked levels
else{
var buttonTween = game.add.tween(button)
buttonTween.to({
alpha: 0.5
}, 20, Phaser.Easing.Cubic.None);
buttonTween.to({
alpha: 1
}, 20, Phaser.Easing.Cubic.None);
buttonTween.to({
alpha: 0.5
}, 20, Phaser.Easing.Cubic.None);
buttonTween.to({
alpha: 1
}, 20, Phaser.Easing.Cubic.None);
buttonTween.start();
}
}
}
playlevel.js just makes you play a fake level:
playLevel = {
create: function(){
// showing level title
var style = {
font: "32px Arial",
fill: "#ffffff"
};
var levelTitle = game.add.text(0,0,"PLAYING LEVEL "+game.global.level,style);
levelTitle.align = "center";
levelTitle.x = (game.width - levelTitle.width) / 2;
// showing game thumbnails
for(var i=0; i<=3; i++){
var gameThumb = game.add.button(game.width/2, 90*(i+1), "game", this.levelFinished, this);
gameThumb.anchor.setTo(0.5);
gameThumb.frame = i;
}
},
levelFinished: function(button){
// did we improved our stars in current level?
if(game.global.starsArray[game.global.level-1]<button.frame){
game.global.starsArray[game.global.level-1] = button.frame;
}
// if we completed a level and next level is locked - and exists - then unlock it
if(button.frame>0 && game.global.starsArray[game.global.level]==4 && game.global.level<game.global.starsArray.length){
game.global.starsArray[game.global.level] = 0;
}
// back to level selection
game.state.start("LevelSelect");
}
}
And that’s it, you should be able to understand everything thanks to the comments in the code, leave a comment if something is unclear and download the full project source code.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.