Create web and mobile HTML5 games with Phaser framework

Do you already know Phaser?

As said in the official site, it’s a fast, free and fun open source game framework for making desktop and mobile browser HTML5 games providing fast 2D Canvas and WebGL rendering.

In other words, it’s absolutely something I want to put my hands on, and which I recommend you.

In only a few minutes I was able to import a level made with Tiled and make it run with a basic platform engine.

Look what we have:

Use LEFT and RIGHTS arrow keys to move, UP to jump. Fast and easy.

And this is the fully commented source code, not more than 40 lines of code I fully commented for you:

<!doctype html>
<html>
	<head>
    		<script src="phaser.min.js"></script>
    		<style>
    			body{margin:0}
    		</style>
    		<script type="text/javascript">
			window.onload = function() {
				// here we define a new 640x480 game, with three core functions:
				// onPreload to be executed when the game preloads
				// onCreate to be executed once the game is firstly created
				// onUpdate to be called every time the game us updated
				var game = new Phaser.Game(640,480,Phaser.AUTO,"",{preload:onPreload, create:onCreate, update:onUpdate});
				
				// some other variables, we'll se later their meaning and how to use them
				var map;
				var tileset;
				var layer;
				var player;
				var cursors;	
				
				// THE GAME IS PRELOADING
				function onPreload() {
					// importing "level.json" file to load the level into "level" instance.
					// this is the file I created and exported with Tiled
					game.load.tilemap("level", "level.json", null, Phaser.Tilemap.TILED_JSON);
					// these are the tiles used in the levels, I am loading "tiles.png" file and I also
					// need to specify tile size (32x32) and the instance ("level").
					// I found tiles file must have at least two tiles horizontally and vertically,
					// or collisions won't work
					game.load.tileset("tiles", "tiles.png", 32, 32);
					// the hero of the game is loaded as an image, here is how I load hero.png and
					// assign it to "hero" instance
					game.load.image("hero", "hero.png");
				}
				
				// THE GAME HAS BEEN CREATED
				function onCreate() {
					// adding the "level" tilemap created at line 27 to the game
					map = game.add.tilemap("level");
					// adding the "tiles" tileset created at line 30 to the game
	                    tileset = game.add.tileset("tiles");
	                    // this is the way we tell the script every tile in our engine needs to be checked
	                    // for collision. This way all tiles are solid
	                    tileset.setCollisionRange(0, tileset.total-1, true, true, true, true);
	                    // now we need to create a game layer, and assign it a tile set and a map
					layer = game.add.tilemapLayer(0, 0, 640, 480, tileset, map, 0);
					// finally we create the player placing "hero" instance at x=32, y=416
					player = game.add.sprite(32, 416, "hero");
	                   	// this is the gravity applied to the player
					player.body.gravity.y = 8;
					// the fastest way to create game controls is "createCursorKeys" method
					// which automatically assigns up, down, left and right movement to
					// arrow keys
	                    cursors = game.input.keyboard.createCursorKeys();
				}
				
				// THE GAME IS GOING TO BE UPDATED
				function onUpdate() {
					// we are looking for collisions between the hero and the tiles
					game.physics.collide(player, layer);
					// setting player horizontal velocity to zero
					player.body.velocity.x = 0;
					// are we moving left?
					if (cursors.left.isDown){
	        				player.body.velocity.x = -150;
	        			}
	        			// are we moving right?
					if (cursors.right.isDown){
	        				player.body.velocity.x = 150;
	        			}
					// are we jumping? 
					if (cursors.up.isDown && player.body.touching.down){
	        				player.body.velocity.y = -250;
	     			}
	    			}
	    		};
		</script>
    </head>
    <body>
    </body>
</html>

And obviously Phaser is not intended just to build platform games, as it features physics, particles, advanced camera management and a lot of other features I suggest you to try.

Download the source code with all required libraries.

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