Step by step AS3 translation of Creation of a platform game with Flash - step 1
June 14, 2008 by Emanuele Feronato
Filed Under Actionscript 3, Flash, Game design, Users contributions •
Filed Under Actionscript 3, Flash, Game design, Users contributions •
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
ACTIONSCRIPT:
-
/*____________________________________________________
-
|______________ 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
-
}
-
}
-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 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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],
-
[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
ACTIONSCRIPT:
-
/*____________________________________________________
-
|______________ 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
ACTIONSCRIPT:
-
/*____________________________________________________
-
|______________ 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(){