HTML5 Drag and Match engine made with Phaser updated to Phaser 2.5.0

Read all posts about "" game
One of the things I love about Phaser is it gets updated extremely often, always looking for new features to add and new stuff to optimize. About two years ago I blogged about a Drag and Match engine, and since during these days I am making some experiments with Match 3 engines like the Bejeweled engine I blogged about some days ago, it’s time to update the prototype to Phaser 2.5.0 as I am going to add new features during next days. So here it is:
You should know how to play with it, just select a square and drag horizontally or vertically to move the entire row or column. And this is the source code, still uncommented because I will add full comments to the code when new features will be added, at least to make the prototype a little more playable.
var game;
var tileSize = 50;
var fieldSize = 6;
var tileTypes = 6
var movingRow;
var movingCol;
var tileArray = [];
var startX;
var startY;
var distX;
var distY;
var dragDirection = "";
var tempTile;

window.onload = function() {	        
	game = new Phaser.Game(300, 300);
	game.state.add("PlayGame", playGame)
	game.state.start("PlayGame");
}

var playGame = function(game){}
playGame.prototype = {
	preload: function(){
          game.load.spritesheet("tiles", "tiles.png", tileSize, tileSize);
          game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
     	game.scale.setScreenSize = true;
          game.scale.pageAlignHorizontally = true;
          game.scale.pageAlignVertically = true;
	},
	create: function(){
          for(var i = 0; i < fieldSize; i++){
			tileArray[i] = [];
			for(j = 0; j < fieldSize; j++){
				var randomTile = Math.floor(Math.random() * tileTypes);
				theTile = game.add.sprite(j * tileSize, i * tileSize, "tiles");
				theTile.frame = randomTile;
				theTile.value = randomTile;
				tileArray[i][j] = theTile;	
			}
		}
		tempTile = game.add.sprite(0, 0, "tiles");
		tempTile.visible = false;
		game.input.onDown.add(pickTile, this);
	}
}

function pickTile(){
	startX = game.input.worldX;
	startY = game.input.worldY;
	movingRow = Math.floor(startY / tileSize);
	movingCol = Math.floor(startX / tileSize);
	dragging = true;
	game.input.onDown.remove(pickTile, this);
	game.input.onUp.add(releaseTile, this);
     game.input.addMoveCallback(moveTile, this);
}

function moveTile(){
	distX = game.input.worldX - startX;
	distY = game.input.worldY - startY;
     switch(dragDirection){
		case "":
			var dist = distX * distX + distY * distY;
			if(dist &gt; 25) {
				var dragAngle = Math.abs(Math.atan2(distY, distX));
				if((dragAngle &gt; Math.PI / 4 &amp;&amp; dragAngle < 3 * Math.PI / 4)) {
   					dragDirection = "vertical";
				}
				else {
   					dragDirection = "horizontal";
				}
			}
		break;
		case "horizontal":
			tempTile.visible = false;
			tempTile.y = movingRow * tileSize;
			var deltaX = (Math.floor(distX / tileSize) % fieldSize);
			if (deltaX &gt;= 0) {									
				tempTile.frame = tileArray[movingRow][fieldSize - 1 - deltaX].value;
			}
			else{
				deltaX=deltaX * -1 -1;
				tempTile.frame = tileArray[movingRow][deltaX].value;		
			}
			for(var i = 0; i < fieldSize; i++){
				tileArray[movingRow][i].x = (i * tileSize + distX) % (tileSize * fieldSize);
				if (tileArray[movingRow][i].x < 0) {
   					tileArray[movingRow][i].x += tileSize * fieldSize;
				}
                    if(distX % tileSize &gt; 0){
                    	tempTile.visible = true;
                         tempTile.x = distX % tileSize - tileSize;
                    }
                    if(distX % tileSize < 0){
                    	tempTile.visible = true;
                         tempTile.x = distX % tileSize;
                    } 
			}
		break;
		case "vertical":
			tempTile.visible = false;
			tempTile.x = movingCol * tileSize;
			var deltaY = (Math.floor(distY / tileSize) % fieldSize);
			if (deltaY &gt;= 0) {									
				tempTile.frame = tileArray[fieldSize - 1 - deltaY][movingCol].value;
			}
			else{
				deltaY = deltaY * -1 -1;
				tempTile.frame = tileArray[deltaY][movingCol].value;		
			}
			for(var i = 0; i < fieldSize; i++){
				tileArray[i][movingCol].y = (i * tileSize + distY) % (tileSize * fieldSize);
				if (tileArray[i][movingCol].y < 0) {
   					tileArray[i][movingCol].y += tileSize * fieldSize;
				}
                    if(distY % tileSize &gt; 0){
                    	tempTile.visible = true;
                         tempTile.y = distY % tileSize - tileSize;
                    }
                    if(distY % tileSize < 0){
                    	tempTile.visible = true;
                         tempTile.y = distY % tileSize;
                    }   
			}
		break;
	} 
}
	
	
function releaseTile(){
	switch(dragDirection){
		case "horizontal":
			var shiftAmount = Math.floor(distX / (tileSize / 2));
			shiftAmount = Math.ceil(shiftAmount / 2) % fieldSize;
			var tempArray = [];
			if(shiftAmount &gt; 0){
				for(var i = 0; i < fieldSize; i++){
					tempArray[(shiftAmount + i) % fieldSize] = tileArray[movingRow][i].value;
				}
			}
			else{
				shiftAmount *= -1;
				for(var i = 0; i < fieldSize; i++){
					tempArray[i] = tileArray[movingRow][(shiftAmount + i) % fieldSize].value;
				}
			}
			for(i = 0; i < fieldSize; i++){
				tileArray[movingRow][i].value = tempArray[i];
				tileArray[movingRow][i].frame = tempArray[i];
				tileArray[movingRow][i].x = i * tileSize;
			}
		break;
		case "vertical":
			var shiftAmount = Math.floor(distY / (tileSize / 2));
				shiftAmount = Math.ceil(shiftAmount / 2) % fieldSize;
				var tempArray = [];
				if(shiftAmount &gt; 0){
					for(var i = 0; i < fieldSize; i++){
						tempArray[(shiftAmount + i) % fieldSize] = tileArray[i][movingCol].value;
					}
				}
				else{
					shiftAmount *= -1;
					for(var i = 0; i < fieldSize; i++){
						tempArray[i] = tileArray[(shiftAmount + i) % fieldSize][movingCol].value;
					}
				}
				for(var i = 0; i < fieldSize; i++){
					tileArray[i][movingCol].value = tempArray[i];
					tileArray[i][movingCol].frame = tempArray[i];
					tileArray[i][movingCol].y= i * tileSize;
			}
		break;	
	}
	dragDirection = "";
	tempTile.visible = false;
	game.input.onUp.remove(releaseTile, this);
     game.input.deleteMoveCallback(moveTile, this);  
	game.input.onDown.add(pickTile, this);
}
While I am turning it into a fully featured prototype you can download the source code and let me know if you manage to make something interesting out of it.

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