HTML5 Down The Mountain game prototype made with Phaser – making hops more realistic

Read all posts about "" game
Do you remember my tutotial series about Down The Mountain game? The prototype works using hexagons to simulate isometric cubes – read this post for more information – but if you play at the latest prototype I published you will notice characther movement does not give the feeling of something actually jumping down, because it moves straight to its destination because of the tween I’ve set. So we are going to use a Bezier curve – more information at this post – to give the tween a fake idea of an isometric jump, the result is exactly what you would expect:
Play with left and right arrow keys to make the character jump down the mountain block by block. I modified a bit the source code, also added a fade effect when blocks disappear and next time I’ll update everything to the latest Phaser version and add some comments.
var game;
var hexagonWidth = 70;
var hexagonHeight = 80;
var minRow = 0;
var gridSizeX = 5;
var gridSizeY = 9;
var marker;
var hexagonGroup;
var hexagonArray = [];
var cursors;
var playerCol = 2;
var playerRow = 0;
var playerMove = true;

window.onload = function() {	
	game = new Phaser.Game(480, 480);
     game.state.add("PlayGame", playGame);
     game.state.start("PlayGame");
}
var playGame = function(game){}
playGame.prototype = {
     preload: function(){
     	game.load.image("hexagon", "hexagon.png");
          game.load.image("marker", "marker.png");          
     },
     create: function(){
          hexagonGroup = game.add.group();
          game.stage.backgroundColor = "#ffffff";
          cursors = game.input.keyboard.createCursorKeys();
          for(var i = 0; i < gridSizeY; i ++){
               addHexagonRow(i);
          }
          hexagonGroup.x = (game.width - hexagonWidth * gridSizeX) / 2;
          hexagonGroup.y = 20;
          marker = game.add.sprite(hexagonGroup.width / 2, 20, "marker");
          marker.anchor.setTo(0.5);
          hexagonGroup.add(marker);      
     },
     update: function(){
          if(playerMove){
			if(cursors.left.isDown &amp;&amp; cursors.right.isUp &amp;&amp; (playerCol &gt; 0 || (playerRow % 2 == 1))){	
                    placeMarker(playerCol - (1 - playerRow % 2), playerRow + 1);
			}
			if(cursors.right.isDown  &amp;&amp; cursors.left.isUp &amp;&amp; playerCol < gridSizeX - 1){				
                    placeMarker(playerCol + (playerRow % 2), playerRow + 1);
			}
               if(gridSizeY - playerRow < 8){
                    addHexagonRow(gridSizeY);
                    gridSizeY ++;
               }
		}
          if(marker.world.y &gt; 60){
               hexagonGroup.y -= 1;
          }
          if(marker.world.y &gt; 240){
               hexagonGroup.y -= (marker.world.y - 240);     
          }
          var destroyedRow = false;
          for(var i = minRow; i < gridSizeY; i ++){
			for(var j = 0; j < gridSizeX; j ++){
				if((i % 2 == 0 || j < gridSizeX - 1) &amp;&amp; hexagonArray[i][j].world.y < 0){
                         var destroyTween = game.add.tween(hexagonArray[i][j]).to({
                              alpha:0,
                              y: hexagonArray[i][j].y + hexagonHeight / 2
                         }, 200,  Phaser.Easing.Quadratic.Out, true);
                         destroyTween.onComplete.add(function(e){
                              e.destroy();     
                         })
                         destroyedRow = true;                        
				}
			}
		}
          if(destroyedRow){
               minRow ++;
          }     
     }
}                

function placeMarker(posX, posY){	
     playerRow = posY;
     playerCol = posX;	          
     var nextX = hexagonWidth * (2 * posX + 1 + posY % 2) / 2;
	var nextY = hexagonHeight * (3 * posY + 1) / 4;		       
     playerMove = false;
     var bezierX = hexagonWidth;
     if(marker.x &gt; nextX){
          bezierX *= -1;
     }
     var playerTween = game.add.tween(marker).to({
          x: [marker.x, marker.x + bezierX, nextX, nextX],
          y: [marker.y, marker.y, nextY, nextY]
     }, 500, Phaser.Easing.Quadratic.InOut, true).interpolation(function(v, k){
          return Phaser.Math.bezierInterpolation(v, k);
     }); 
     playerTween.onComplete.add(function(){
          playerMove = true;           
     });
     marker.bringToTop();
}     

function addHexagonRow(i){
     hexagonArray[i] = [];
	for(var j = 0; j < gridSizeX - i % 2; j ++){
          var hexagonX = hexagonWidth * j + (hexagonWidth / 2) * (i % 2);
          var hexagonY = hexagonHeight * i / 4 * 3;	
          var hexagon = game.add.sprite(hexagonX, hexagonY, "hexagon");
          var hexagonText = game.add.text(0 + hexagonWidth / 3 + 5, 0 + 15, i + "," + j);
		hexagonText.font = "arial";
          hexagonText.align = "center";
		hexagonText.fontSize = 10;
          hexagon.addChild(hexagonText);   
		hexagonGroup.add(hexagon);
		hexagonArray[i][j] = hexagon;	
	}
}
I also highlighted the lines which simulate the isometric jump using Bezier curves in tweens. You can play with these values to get a different kind of movement. As usual, download the source code and experiment!

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