New tile based platform engine – AS3 version

Read all posts about "" game

First, I would like to say the engine is not finished as I have a lot of tile types to add.

This is the AS3 version of part 6.

I am fixing some glitches and preparing another post about the theory of platform games

This is the code

package {
	import flash.display.Sprite;
	import flash.ui.Mouse;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	public class newplat06_as3 extends Sprite {
		var over = "";
		var bonus_speed = 0;
		var press_left = false;
		var press_right = false;
		var press_up = false;
		var press_down = false;
		var press_space = false;
		var x_pos = 0;
		var y_pos = 0;
		var tile_size = 20;
		var ground_acceleration = 1;
		var ground_friction = 0.8;
		var air_acceleration = 0.5;
		var air_friction = 0.7;
		var ice_acceleration = 0.15;
		var ice_friction = 0.95;
		var treadmill_speed = 2;
		var max_speed = 3;
		var xspeed = 0;
		var yspeed = 0;
		var falling = false;
		var gravity = 0.5;
		var jump_speed = 6;
		var climbing = false;
		var climb_speed = 0.8;
		var level = new Array();
		var player = new Array(5,1);
		var h:hero = new hero();
		public function newplat06_as3() {
			level[0] = [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] = [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] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 1, 0, 0, 0, 0, 1];
			level[3] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
			level[4] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
			level[5] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
			level[6] = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 1, 0, 0, 0, 0, 0, 1];
			level[7] = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1];
			level[8] = [1, 1, 1, 1, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1];
			level[9] = [1, 1, 1, 1, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 1, 1];
			create_level(level);
			addEventListener(Event.ENTER_FRAME,on_enter_frame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
		}
		public function key_down(event:KeyboardEvent) {
			if (event.keyCode == 32) {
				press_space = true;
			}
			if (event.keyCode == 37) {
				press_left = true;
			}
			if (event.keyCode == 38) {
				press_up = true;
			}
			if (event.keyCode == 39) {
				press_right = true;
			}
			if (event.keyCode == 40) {
				press_down = true;
			}
		}
		public function key_up(event:KeyboardEvent) {
			if (event.keyCode == 32) {
				press_space = false;
			}
			if (event.keyCode == 37) {
				press_left = false;
			}
			if (event.keyCode == 38) {
				press_up = false;
			}
			if (event.keyCode == 39) {
				press_right = false;
			}
			if (event.keyCode == 40) {
				press_down = false;
			}
		}
		public function create_level(l) {
			var level_container:Sprite = new Sprite();
			var level_height = l.length;
			var level_width = l[0].length;
			addChild(level_container);
			for (j=0; jmax_speed) {
				xspeed = max_speed;
			}
			if (xspeed0) {
				if ((bottom_right != 0 && bottom_right != 6) || (bottom_left != 0 && bottom_left != 6)) {
					if (bottom_right != 5 && bottom_left != 5) {
						y_pos = bottom*tile_size-9;
						yspeed = 0;
						falling = false;
						jumping = false;
					} else {
						if (prev_bottom0) {
				if ((top_right != 0 && top_right != 5 && top_right != 6) || (bottom_right != 0 && bottom_right != 5 && bottom_right != 6)) {
					x_pos = right*tile_size-6;
					xspeed = 0;
				}
			}
			prev_bottom = bottom;
		}
		function get_edges() {
			// right edge
			right = Math.floor((x_pos+5)/tile_size);
			// left edge   
			left = Math.floor((x_pos-6)/tile_size);
			// bottom edge
			bottom = Math.floor((y_pos+8)/tile_size);
			// top edge
			top = Math.floor((y_pos-9)/tile_size);
			// adjacent tiles
			top_right = level[top][right];
			top_left = level[top][left];
			bottom_left = level[bottom][left];
			bottom_right = level[bottom][right];
		}
	}
}

The result is the same, so no need to publish it… just download the source code.

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