Create an HTML5 level selection screen using a scrollable map like in “Hero Emblems” game using Phaser

Read all posts about "" game

Are you playing Hero Emblems these days?

It’s a nice Match-3 game with RPG elements, but what I really liked – which can also be found in other games like King’s “<random cute name> saga” – is the way you can move around the map and select levels.

“It’s only a draggable image”, you can say. Wrong. If it was only a draggable image, you wouldn’t be able to click on a town AND drag the map, because once you click (or release, according to the listener you are using) a town, you would enter in that town.

The map is made in a way if you press a town and drag, you won’t enter that town. Useful if you don’t want the player to find a “safe” spot on the map, far from towns, to drag the map without accidentally entering a town.

Just like in this prototype I made, which you can play on your mobile device from this link.

The script has been build to work with any map size in any screen size, as long as the map is bigger than the screen.

Have a look at the source code:

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

	// the map itself
	var map;
	// a couple of variables used to save start touch position
	var startX;
	var startY;
	// dummy variable to handle multitouch, if any 
	var moveIndex;
	// map scrolling speed. The higher the number, the fastest 
	// the scrolling. Leaving it at 1, it's like you are dragging the map
	var mapSpeed = 1;
	// group where map and town are placed
     var mapGroup;
     // the town you are about to select
     var candidateTown;

	// preloading graphic assets
	function onPreload() {
		game.load.image("map", "map.png");
          game.load.image("town", "town.png");
	}

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

	// creating the game itself
	function onCreate() {
		goFullScreen();
          mapGroup = game.add.group();
          // placing the map
		map = game.add.image(0, 0, "map");
          mapGroup.add(map);
          // ramdomly placing ten towns
          for(var i = 0;i < 10; i++){
               var town = game.add.image(game.rnd.between(50, map.width - 50), game.rnd.between(50, map.height - 50), "town");
               town.anchor.setTo(0.5);
               // each town is enabled for input and has its own up and down listeners
               town.inputEnabled = true;
               town.events.onInputDown.add(selectTown, this);
               town.events.onInputUp.add(confirmTown, this);
               mapGroup.add(town);
          }
          // centering the map
          mapGroup.x = (game.width - map.width) / 2;
          mapGroup.y = (game.height - map.height) / 2;
          // listener for the touch
		game.input.onDown.add(fingerOnMap, this);		
	}
	
	// the player puts the finger/mouse down on the map
	function fingerOnMap(){
		// saving touch start psoition
		startX = game.input.worldX;
		startY = game.input.worldY;
		mapGroup.saveX = mapGroup.x;
		mapGroup.saveY = mapGroup.y;
		// updating listeners
		game.input.onDown.remove(fingerOnMap);
     	game.input.onUp.add(stopMap);
     	moveIndex = game.input.addMoveCallback(dragMap, this)   
	}
	
	// the player drags the map, we move the map according to finger
	// movement and map speed.
	function dragMap(){
		var currentX = game.input.worldX;
		var currentY = game.input.worldY;
		var deltaX = startX - currentX;
		var deltaY = startY - currentY; 
          if(deltaX * deltaX + deltaY * deltaY > 25){
               candidateTown = null;
          }
		mapGroup.x = mapGroup.saveX - deltaX * mapSpeed;
		mapGroup.y = mapGroup.saveY - deltaY * mapSpeed;
		// this is to limit map movement and always have the
		// stage fully covered by the map
          if(mapGroup.x < - map.width + game.width){
               mapGroup.x = - map.width + game.width;
          }
          if(mapGroup.x > 0){
               mapGroup.x = 0;
          }
          if(mapGroup.y < - map.height + game.height){
               mapGroup.y = - map.height + game.height;
          }
          if(mapGroup.y > 0){
               mapGroup.y = 0;
          }
	}
	
	// the player stops touching the map
	function stopMap(){
		game.input.onDown.add(fingerOnMap);
     	game.input.onUp.remove(stopMap);
     	game.input.deleteMoveCallback(moveIndex);
	}
     
     // the player puts the finger/mouse down on a town
     function selectTown(sprite, pointer) {
          candidateTown = sprite;
     }
     
     // the player stops touching the town
     function confirmTown(sprite, pointer) {
          if(candidateTown == sprite){
               alert("Town selected");
          }
     }
         
}

Now you can create your level selection map in your games. If you prefer the classic level selection, check HTML5 Phaser Tutorial: how to create a level selection screen with locked levels and stars. Also, here’s the source code for you.

PS: I am quite happy with the response of my HTML5 Globez source code published last monday. Thank you.

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

215 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
// Stairs
// 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