New tile based platform engine – AS3 version updated to step 10 PLUS scrolling

Read all posts about "" game

If you are looking for AS3 version of step 10, here it is… made by Robin vd Vleuten from the Netherlands with scrolling included!

Hello Emanuele,

I made it with flex so I use a swc to access the tiles. All the code is now in one actionscript-file, but I’m trying to convert the code in different classes. I will email it also when its finished if you’re interested in it.

Also many thanks for helping me out with the basics of a platform engine. It helped me a lot with a project I’m working on.

Regards,

Robin

P.S. Just another cool thing I had forgot to tell you. I made the game scrollable with just 2 extra lines of code :D

Here it is the source code…

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	
	[SWF(width='500', height='400', frameRate='40', backgroundColor='0xffffff')]
	public class Platform extends Sprite
	{
		private var over:String = "";
		private var bonus_speed:Number = 0;
		private var press_left:Boolean = false;
		private var press_right:Boolean = false;
		private var press_up:Boolean = false;
		private var press_down:Boolean = false;
		private var press_space:Boolean = false;
		private var x_pos:Number = 0;
		private var y_pos:Number = 0;
		private var tile_size:Number = 20;
		private var ground_acceleration:Number = 1;
		private var ground_friction:Number = 0.8;
		private var air_acceleration:Number = 0.5;
		private var air_friction:Number = 0.7;
		private var ice_acceleration:Number = 0.15;
		private var ice_friction:Number = 0.95;
		private var treadmill_speed:Number = 2;
		private var max_speed:Number = 3;
		private var xspeed:Number = 0;
		private var yspeed:Number = 0;
		private var falling:Boolean = false;
		private var gravity:Number = 0.5;
		private var jump_speed:Number = 6;
		private var climbing:Boolean = false;
		private var climb_speed:Number = 0.8;
		private var coins:Array = new Array();
		private var level:Array = new Array();
		private var levelObj:Array = new Array
		private var keys:Array = new Array();
		private var player:Array = new Array();
		private var enemy:Array = new Array();
		private var walkable_tiles:Array = new Array(0, 5, 6, 7);
		private var h:hero = new hero();
		private var level_container:Sprite = new Sprite();
		
		private var bottom:Number;
		private var left:Number;
		private var right:Number;
		private var top:Number;
		private var bottom_left:Number;
		private var bottom_right:Number;
		private var top_left:Number;
		private var top_right:Number;
		
		private var climbdir:Number;
		private var current_tile:Number;
		private var friction:Number;
		
		private var speed:Number;
		private var jumping:Boolean;
		private var walking:Boolean;
		private var prev_bottom:Number;
		
		public function Platform()
		{
			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, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
			level[4] = [1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
			level[5] = [1, 0, 0, 0, 0, 1, 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, 1, 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, 1, 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, 9, 0, 0, 0, 5, 5, 0, 0, 0, 7, 0, 0, 6, 0, 0, 0, 0, 7, 0, 1];
			level[9] = [1, 1, 1, 1, 1, 1, 1, 2, 2, 8, 8, 8, 8, 1, 1, 3, 3, 1, 4, 4, 1, 8, 1, 8, 1];
			
			player = [4,8];
			
			coins[0] = [2, 2];
			coins[1] = [23, 4];
			
			enemy[0] = [10, 3, -2];
			enemy[1] = [3, 3, -2];
			
			keys[0] = [1, 5, 5, 8];
			
			create_level(level);
			
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
		}
		
		public function key_down(event:KeyboardEvent):void
		{
			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):void
		{
			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:Array):void
		{
			var level_height:Number = l.length;
			var level_width:Number = l[0].length;
			
			addChild(level_container);
			
			for (var j:Number = 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) {
						if ((bottom_right == 7 || bottom_left == 7) && (Math.abs(yspeed)>1))
						{
							// trampoline
                    		yspeed *= -1;
                    		jumping = true;
                    		falling = true;
						}else {
							y_pos = bottom*tile_size-9;
							yspeed = 0;
							falling = false;
							jumping = false;
						}
					} else {
						if (prev_bottom0) {
				if (!is_walkable(top_right) || !is_walkable(bottom_right)) {
					x_pos = right * tile_size - 6;
					xspeed = 0;
				}
			}
			
			prev_bottom = bottom;
		}
		
		public function get_edges():void
		{
			right = Math.floor((x_pos+5)/tile_size);
			left = Math.floor((x_pos-6)/tile_size);
			bottom = Math.floor((y_pos+8)/tile_size);
			top = Math.floor((y_pos-9)/tile_size);

			top_right = level[top][right];
			top_left = level[top][left];
			bottom_left = level[bottom][left];
			bottom_right = level[bottom][right];
		}
		
		public function place_player():void
		{
			x_pos = (player[0] * tile_size) + (tile_size / 2);
			y_pos = (player[1] * tile_size) + (tile_size / 2 + 1);
			
			h.x = x_pos;
			h.y = y_pos;
		}
		
		public function is_walkable(tile:int):Boolean {
			var walkable:Boolean = false;
			for (var i:int = 0; i < walkable_tiles.length; i++)
			{
				if (tile == walkable_tiles[i])
				{
					walkable = true;
					break;
				}
			}
			
			return (walkable);
		}
	}
}

And this is the result:

Download the source code and make big kudos to Robin!!

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

215 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
// Stairs
// 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