Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Hero Emblems game, Game development, HTML5, Javascript and Phaser.

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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.