Step by step AS3 translation of Creation of a platform game with Flash – step 1

Read all posts about "" game

This seems to be the month of AS3 conversions.

Good, this means AS3 community is growing.

Today we’ll see how Bart de Boer translated Creation of a platform game with Flash – step 1 into AS3.

You are invited to read Creation of a platform game with Flash – step 1 to know all theory behind this engine.

All files have a main class called Script.as and the level stored in the Data.as file.

Like all good coders, Bart commented the code for our understanding.

You can also find more informations at this page in the forum

Level creation

Script.as

/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|

- BuildMap()		load and create the level


extern

- Setup()			create levels 

*/


package{												//The begin of an .as file
	import flash.display.MovieClip;						//import some libraries
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class Script extends MovieClip{				// start the script
		
	private var level:Array = new Array();				// create an empty level array
	
	private var Map_data:Data = new Data;				// create a version of the Data.as
	
	private var tiles:Array = new Array();				// create an array for the tiles
	
		public function Script(){						// the init (will only be runned once)
			BuildMap();									// create map
		}
		
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
		
		private function BuildMap(){
			Map_data.Setup();							// setup data from extern file
			
			level = Map_data.level1;					// get data from extern file
			
			for(var t = 0; t < level.length; t++){							//a simple for loop
				for(var u = 0; u < level[t].length; u++){					//"               "
					if(level[t][u] != 0){									//if the data is not null
						var new_tile:platform_tile = new platform_tile;		//than build a tile
						
						addChild(new_tile);									//put it on the screen
						
						new_tile.gotoAndStop(1);							//give it the right frame
						new_tile.x = u * 25;								//give it coördinate
						new_tile.y = t * 25;								//"				   "
						
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

Data.as

package{
	
	public class Data{
		
	public var level1:Array = new Array();
		public function Setup(){
			level1 = [
					  [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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 0, 0, 0, 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, 1]
					 ]
		}
	}
}

The Player

Script.as

/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|

- create_hero()		creates hero as the var "Hero"

- update_hero()		check collision an move

- BuildMap()		load and create the level


extern

- Setup()			create levels 

*/


package{												//The begin of an .as file
	import flash.display.MovieClip;						//import some libraries
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class Script extends MovieClip{				// start the script
	
	private const gravity:int = 1;
	private const max_speed:int = 8;
	
	private const start_x:int = 50;
	private const start_y:int = 50;
	
	private var y_speed:int;
		
	private var level:Array = new Array();
	private var tiles:Array = new Array();
	
	private var Map_data:Data = new Data;				// create a version of the Data.as
	
	private var Hero:hero = new hero;
	
		public function Script(){						// the init (will only be runned once)
			BuildMap();
			create_hero();
			addEventListener(Event.ENTER_FRAME, main);
		}
		
		private function main(even:Event){
			update_hero();
		}
		
/*
///    ///		/////////	///////////		///////////
///    ///		///			////    ///		///		///
///    ///		///			////    ///		///		///
//////////		/////////	//////////		///		///
//////////		/////////	////	///		///		///
///    ///		///			////	///		///		///
///    ///		///			////	///		///		///
///    ///		/////////	////	///		///////////
*/
		private function create_hero(){
			addChild(Hero);
			Hero.x = start_x;
			Hero.y = start_y;
		}
		
		private function update_hero(){
			y_speed += gravity;
			
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
			
			Hero.y += y_speed;
			for(var t:int; t < tiles.length; t++){
				while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
					Hero.y -= 1;
				}
			}
		}
	
	
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
				
		private function BuildMap(){
			Map_data.Setup();												// setup data from extern file
			
			level = Map_data.level1;										// get data from extern file
			
			for(var t = 0; t < level.length; t++){
				for(var u = 0; u < level[t].length; u++){
					if(level[t][u] != 0){									//if the data is not null
						var new_tile:platform_tile = new platform_tile;		//than build a tile
						
						addChild(new_tile);									//put it on the screen
						
						new_tile.gotoAndStop(1);
						new_tile.x = u * 25;
						new_tile.y = t * 25;
						
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

Data.as remains the same

The moving player

/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|

- create_hero()		creates hero as the var "Hero"

- update_hero()		check collision an move

- BuildMap()		load and create the level


extern

- Setup()			create levels 

*/


package{												//The begin of an .as file
	import flash.display.MovieClip;						//import some libraries
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class Script extends MovieClip{				// start the script
	
	private const gravity:int = 1;
	private const max_speed:int = 8;
	
	private const start_x:int = 50;
	private const start_y:int = 50;
	
	private var y_speed:int;
	private var x_speed:int;
	
	private var walkspeed:int = 4;
	
	private var left:Boolean;
	private var up:Boolean;
	private var right:Boolean;
	private var space:Boolean;
		
	private var level:Array = new Array();
	private var tiles:Array = new Array();
	
	private var Map_data:Data = new Data;				// create a version of the Data.as
	
	private var Hero:hero = new hero;
	
		public function Script(){						// the init (will only be runned once)
			BuildMap();
			create_hero();
			addEventListener(Event.ENTER_FRAME, main);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
		}
		
		private function main(event:Event){
			update_hero();
		}
		
		private function key_down(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = true;
			}
			if(event.keyCode == 38){
				up = true;
			}
			if(event.keyCode == 39){
				right = true;
			}
		}
		
		private function key_up(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = false;
			}
			if(event.keyCode == 38){
				up = false;
			}
			if(event.keyCode == 39){
				right = false;
			}
		}
								
			
		
/*
///    ///		/////////	///////////		///////////
///    ///		///			////    ///		///		///
///    ///		///			////    ///		///		///
//////////		/////////	//////////		///		///
//////////		/////////	////	///		///		///
///    ///		///			////	///		///		///
///    ///		///			////	///		///		///
///    ///		/////////	////	///		///////////
*/
		private function create_hero(){
			addChild(Hero);
			Hero.x = start_x;
			Hero.y = start_y;
		}
		
		private function update_hero(){
			y_speed += gravity;
			if(left){
				x_speed -= walkspeed;
			}
			if(right){
				x_speed += walkspeed;
			}
			
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
			
			
			
			Hero.y += y_speed;
			Hero.x += x_speed;
			for(var t:int; t < tiles.length; t++){
				while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
					Hero.y -= 1;
				}
				while(tiles[t].hitTestPoint(Hero.x,Hero.y - 10,true)){
					Hero.y += 1;
				}
				while(tiles[t].hitTestPoint(Hero.x + 5,Hero.y,true)){
					Hero.x -= 1;
				}
				while(tiles[t].hitTestPoint(Hero.x - 5,Hero.y,true)){
					Hero.x += 1;
				}
			}
			x_speed = 0;
		}
	
	
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
				
		private function BuildMap(){
			Map_data.Setup();												// setup data from extern file
			
			level = Map_data.level1;										// get data from extern file
			
			for(var t = 0; t < level.length; t++){
				for(var u = 0; u < level[t].length; u++){
					if(level[t][u] != 0){									//if the data is not null
						var new_tile:platform_tile = new platform_tile;		//than build a tile
						
						addChild(new_tile);									//put it on the screen
						
						new_tile.gotoAndStop(1);
						new_tile.x = u * 25;
						new_tile.y = t * 25;
						
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

Data.as remains the same

The jumping player

/*____________________________________________________
|______________	register of functions _______________|
|____________________________________________________|

- create_hero()		creates hero as the var "Hero"

- update_hero()		check collision an move

- BuildMap()		load and create the level


extern

- Setup()			create levels 

*/


package{												//The begin of an .as file
	import flash.display.MovieClip;						//import some libraries
	import flash.events.Event;
	import flash.events.KeyboardEvent;

	public class Script extends MovieClip{				// start the script
	
	private const gravity:int = 1;
	private const max_speed:int = 8;
	
	private const walkspeed:int = 4;
	private const jumpspeed:int = 20;
	
	private const start_x:int = 50;
	private const start_y:int = 50;
	
	private var y_speed:int;
	private var x_speed:int;
	
	private var left:Boolean;
	private var up:Boolean;
	private var right:Boolean;
	private var space:Boolean;
	private var can_jump:Boolean;
		
	private var level:Array = new Array();
	private var tiles:Array = new Array();
	
	private var Map_data:Data = new Data;				// create a version of the Data.as
	
	private var Hero:hero = new hero;
	
		public function Script(){						// the init (will only be runned once)
			BuildMap();
			create_hero();
			addEventListener(Event.ENTER_FRAME, main);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
			stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
		}
		
		private function main(event:Event){
			update_hero();
		}
		
		private function key_down(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = true;
			}
			if(event.keyCode == 38){
				up = true;
			}
			if(event.keyCode == 39){
				right = true;
			}
		}
		
		private function key_up(event:KeyboardEvent){
			if(event.keyCode == 37){
				left = false;
			}
			if(event.keyCode == 38){
				up = false;
			}
			if(event.keyCode == 39){
				right = false;
			}
		}
								
			
		
/*
///    ///		/////////	///////////		///////////
///    ///		///			////    ///		///		///
///    ///		///			////    ///		///		///
//////////		/////////	//////////		///		///
//////////		/////////	////	///		///		///
///    ///		///			////	///		///		///
///    ///		///			////	///		///		///
///    ///		/////////	////	///		///////////
*/
		private function create_hero(){
			addChild(Hero);
			Hero.x = start_x;
			Hero.y = start_y;
		}
		
		private function update_hero(){
			y_speed += gravity;
			if(left){
				x_speed -= walkspeed;
			}
			if(right){
				x_speed += walkspeed;
			}
			if(up && can_jump){
				y_speed -= jumpspeed;
				can_jump = false;
			}
			
			if(y_speed > max_speed){
				y_speed = max_speed;
			}
			
			
			Hero.y += y_speed;
			Hero.x += x_speed;
			for(var t:int; t < tiles.length; t++){
				while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
					Hero.y -= 1;
					if(y_speed >= 0){									//my try to dont let it jump out of stage without changing the hittest
						can_jump = true;
					}
				}
				while(tiles[t].hitTestPoint(Hero.x,Hero.y - 10,true)){
					Hero.y += 1;
					y_speed = 0;
				}
				while(tiles[t].hitTestPoint(Hero.x + 5,Hero.y,true)){
					Hero.x -= 1;
				}
				while(tiles[t].hitTestPoint(Hero.x - 5,Hero.y,true)){
					Hero.x += 1;
				}
			}
			x_speed = 0;
		}
	
	
/*
|||||||||||		 |||||||||||	  ||||||||||	||||||||||
||||||||||||	||||||||||||	 ||||	 ||||	||||	|||
||||	||||| |||||		||||	 ||||||||||||	||||||||||
||||	  |||||||		||||	 ||||	 ||||	||||
||||	    |||			||||	||||	  ||||	||||
||||					||||	||||      ||||	||||
*/
				
		private function BuildMap(){
			Map_data.Setup();												// setup data from extern file
			
			level = Map_data.level1;										// get data from extern file
			
			for(var t = 0; t < level.length; t++){
				for(var u = 0; u < level[t].length; u++){
					if(level[t][u] != 0){									//if the data is not null
						var new_tile:platform_tile = new platform_tile;		//than build a tile
						
						addChild(new_tile);									//put it on the screen
						
						new_tile.gotoAndStop(1);
						new_tile.x = u * 25;
						new_tile.y = t * 25;
						
						tiles.push(new_tile);								//put it in an array
					}
				}
			}
		}
	}
}

And Data.as remains the same

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