Creation of a platform game with Flash – step 2

Read all posts about "" game

This is a quick update to the tutorial/engine started with Creation of a platform game with Flash – step 1.

I fixed some bugs and added a ladder object (the one defined in the array with “2” and painted in light purple).

Use arrows to move and space to jump.

I won’t explain the script line by line at the moment because I want to add more features before writing a complete tutorial.

But the source code is someway commented

// player default speeds
xspeed = 0;
yspeed = 0;
max_yspeed = 10;
walk_speed = 4;
climb_speed = 2;
// am I climbing?
climbing = false;
// am I jumping?
jumping = false;
// can I jump?
can_jump = true;
// gravity & jump settings
gravity = 1;
jump_power = 10;
walking_while_jumping = true;
// level creation
level = new Array();
_root.createEmptyMovieClip("lev",_root.getNextHighestDepth());
_root.createEmptyMovieClip("lad",_root.getNextHighestDepth());
level[0] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
level[1] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[2] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[3] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[4] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[5] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1);
level[6] = new Array(1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[7] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[8] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[9] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[10] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[11] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[12] = new Array(1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[13] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[14] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
for (y=0; y<=14; y++) {
	for (x=0; x<=24; x++) {
		if (level[y][x] == 1) {
			place_brick = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
			place_brick.gotoAndStop(level[y][x]);
		}
		if (level[y][x] == 2) {
			place_brick = lad.attachMovie("block", "block_"+lad.getNextHighestDepth(), lad.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
			place_brick.gotoAndStop(level[y][x]);
		}
	}
}
_root.attachMovie("player","player",_root.getNextHighestDepth(),{_x:40, _y:40});
// end of level creation
player.onEnterFrame = function() {
	if (Key.isDown(Key.LEFT)) {
		if (climbing) {
			xspeed = -climb_speed;
		}
		else {
			if (walking_while_jumping or can_jump) {
				xspeed = -walk_speed;
			}
		}
	}
	if (Key.isDown(Key.RIGHT)) {
		if (climbing) {
			xspeed = climb_speed;
		}
		else {
			if (walking_while_jumping or can_jump) {
				xspeed = walk_speed;
			}
		}
	}
	if (!feet_on_ladder()) {
		climbing = false;
	}
	if (Key.isDown(Key.UP)) {
		if (feet_on_ladder()) {
			yspeed = -climb_speed;
			climbing = true;
			jumping = false;
		}
	}
	if (Key.isDown(Key.DOWN)) {
		if (feet_on_ladder() or ladder_under_my_feet()) {
			yspeed = climb_speed;
			climbing = true;
			jumping = false;
		}
	}
	if ((Key.isDown(Key.SPACE)) and can_jump and !jumping and !climbing) {
		yspeed -= jump_power;
		jumping = true;
	}
	// adjusting y speed           
	if (!climbing) {
		yspeed += gravity;
	}
	if (yspeed>max_yspeed) {
		yspeed = max_yspeed;
	}
	if (level_under_my_feet() and !jumping and !climbing) {
		yspeed = 0;
	}
	if (ladder_under_my_feet() and !jumping and !climbing) {
		yspeed = 0;
	}
	forecast_x = this._x+xspeed;
	forecast_y = this._y+yspeed;
	// floor control 
	while (_root.lev.hitTest(forecast_x, forecast_y+this._height/2-1, true)) {
		forecast_y--;
		xspeed = 0;
		yspeed = 0;
		jumping = false;
	}
	// ceiling control
	while (_root.lev.hitTest(forecast_x, forecast_y-this._height/2, true)) {
		forecast_y++;
		yspeed = 0;
	}
	// left wall control
	while (_root.lev.hitTest(forecast_x-this._width/2+1, forecast_y, true)) {
		forecast_x++;
		xspeed = 0;
	}
	// right wall control
	while (_root.lev.hitTest(forecast_x+this._width/2, forecast_y, true)) {
		forecast_x--;
		xspeed = 0;
	}
	this._x = forecast_x;
	this._y = forecast_y;
	// adjusting speeds for next frame
	xspeed = 0;
	if (climbing) {
		yspeed = 0;
	}

};
function feet_on_ladder() {
	return _root.lad.hitTest(player._x, player._y+player._height/2-1, true);
}
function level_under_my_feet() {
	return _root.lev.hitTest(player._x, player._y+player._height/2, true);
}
function ladder_under_my_feet() {
	return _root.lad.hitTest(player._x, player._y+player._height/2, true);
}

This is the result you will get:

And this is the source code to download.

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