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

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:
  1. /*____________________________________________________
  2. |______________ register of functions _______________|
  3. |____________________________________________________|
  4. - BuildMap()        load and create the level
  5. extern
  6. - Setup()         create levels
  7. */
  8.  
  9.  
  10. package{                                                //The begin of an .as file
  11.     import flash.display.MovieClip;      //import some libraries
  12.     import flash.events.Event;
  13.     import flash.events.KeyboardEvent;
  14.  
  15.     public class Script extends MovieClip{        // start the script
  16.        
  17.     private var level:Array = new Array();        // create an empty level array
  18.    
  19.     private var Map_data:Data = new Data;            // create a version of the Data.as
  20.    
  21.     private var tiles:Array = new Array();        // create an array for the tiles
  22.    
  23.         public function Script(){                  // the init (will only be runned once)
  24.             BuildMap();         // create map
  25.         }
  26.        
  27. /*
  28. |||||||||||   |||||||||||   |||||||||| ||||||||||
  29. ||||||||||||    ||||||||||||     ||||    ||||  ||||  |||
  30. ||||    ||||| |||||  ||||  ||||||||||||    ||||||||||
  31. ||||      |||||||      ||||    ||||   |||| ||||
  32. ||||        |||   |||| ||||   ||||   ||||
  33. ||||                    ||||    ||||      ||||  ||||
  34. */
  35.        
  36.         private function BuildMap(){
  37.             Map_data.Setup();                     // setup data from extern file
  38.            
  39.             level = Map_data.level1;                    // get data from extern file
  40.            
  41.             for(var t = 0; t <level.length; t++){                     //a simple for loop
  42.                 for(var u = 0; u <level[t].length; u++){                    //"               "
  43.                     if(level[t][u] != 0){                           //if the data is not null
  44.                         var new_tile:platform_tile = new platform_tile;  //than build a tile
  45.                        
  46.                         addChild(new_tile);         //put it on the screen
  47.                        
  48.                         new_tile.gotoAndStop(1);                            //give it the right frame
  49.                         new_tile.x = u * 25;                                //give it coördinate
  50.                         new_tile.y = t * 25;                                //"       "
  51.                        
  52.                         tiles.push(new_tile);                        //put it in an array
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.     }
  58. }

Data.as

ACTIONSCRIPT:
  1. package{
  2.    
  3.     public class Data{
  4.        
  5.     public var level1:Array = new Array();
  6.         public function Setup(){
  7.             level1 = [
  8.                       [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],
  9.                       [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],
  10.                       [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],
  11.                       [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],
  12.                       [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],
  13.                       [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],
  14.                       [1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  15.                       [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],
  16.                       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  17.                       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  18.                       [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],
  19.                       [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  20.                       [1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
  21.                       [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],
  22.                       [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]
  23.                      ]
  24.         }
  25.     }
  26. }

The Player

Script.as

ACTIONSCRIPT:
  1. /*____________________________________________________
  2. |______________ register of functions _______________|
  3. |____________________________________________________|
  4. - create_hero()  creates hero as the var "Hero"
  5. - update_hero()  check collision an move
  6. - BuildMap()        load and create the level
  7. extern
  8. - Setup()         create levels
  9. */
  10.  
  11.  
  12. package{                                                //The begin of an .as file
  13.     import flash.display.MovieClip;      //import some libraries
  14.     import flash.events.Event;
  15.     import flash.events.KeyboardEvent;
  16.  
  17.     public class Script extends MovieClip{        // start the script
  18.    
  19.     private const gravity:int = 1;
  20.     private const max_speed:int = 8;
  21.    
  22.     private const start_x:int = 50;
  23.     private const start_y:int = 50;
  24.    
  25.     private var y_speed:int;
  26.        
  27.     private var level:Array = new Array();
  28.     private var tiles:Array = new Array();
  29.    
  30.     private var Map_data:Data = new Data;            // create a version of the Data.as
  31.    
  32.     private var Hero:hero = new hero;
  33.    
  34.         public function Script(){                  // the init (will only be runned once)
  35.             BuildMap();
  36.             create_hero();
  37.             addEventListener(Event.ENTER_FRAME, main);
  38.         }
  39.        
  40.         private function main(even:Event){
  41.             update_hero();
  42.         }
  43.        
  44. /*
  45. ///    ///    ///////// ///////////    ///////////
  46. ///    ///    ///         ////    ///        ///  ///
  47. ///    ///    ///         ////    ///        ///  ///
  48. //////////    ///////// //////////      ///        ///
  49. //////////    ///////// //// ///    ///      ///
  50. ///    ///    ///         ////   ///        ///  ///
  51. ///    ///    ///         ////   ///        ///  ///
  52. ///    ///    ///////// //// ///    ///////////
  53. */
  54.         private function create_hero(){
  55.             addChild(Hero);
  56.             Hero.x = start_x;
  57.             Hero.y = start_y;
  58.         }
  59.        
  60.         private function update_hero(){
  61.             y_speed += gravity;
  62.            
  63.             if(y_speed> max_speed){
  64.                 y_speed = max_speed;
  65.             }
  66.            
  67.             Hero.y += y_speed;
  68.             for(var t:int; t <tiles.length; t++){
  69.                 while(tiles[t].hitTestPoint(Hero.x,Hero.y + 10,true)){
  70.                     Hero.y -= 1;
  71.                 }
  72.             }
  73.         }
  74.    
  75.    
  76. /*
  77. |||||||||||   |||||||||||   |||||||||| ||||||||||
  78. ||||||||||||    ||||||||||||     ||||    ||||  ||||  |||
  79. ||||    ||||| |||||  ||||  ||||||||||||    ||||||||||
  80. ||||      |||||||      ||||    ||||   |||| ||||
  81. ||||        |||   |||| ||||   ||||   ||||
  82. ||||                    ||||    ||||      ||||  ||||
  83. */
  84.                
  85.         private function BuildMap(){
  86.             Map_data.Setup();                                    // setup data from extern file
  87.            
  88.             level = Map_data.level1;                                        // get data from extern file
  89.            
  90.             for(var t = 0; t <level.length; t++){
  91.                 for(var u = 0; u <level[t].length; u++){
  92.                     if(level[t][u] != 0){                           //if the data is not null
  93.                         var new_tile:platform_tile = new platform_tile;  //than build a tile
  94.                        
  95.                         addChild(new_tile);         //put it on the screen
  96.                        
  97.                         new_tile.gotoAndStop(1);
  98.                         new_tile.x = u * 25;
  99.                         new_tile.y = t * 25;
  100.                        
  101.                         tiles.push(new_tile);                        //put it in an array
  102.                     }
  103.                 }
  104.             }
  105.         }
  106.     }
  107. }

Data.as remains the same

The moving player

ACTIONSCRIPT:
  1. /*____________________________________________________
  2. |______________ register of functions _______________|
  3. |____________________________________________________|
  4. - create_hero()  creates hero as the var "Hero"
  5. - update_hero()  check collision an move
  6. - BuildMap()        load and create the level
  7. extern
  8. - Setup()         create levels
  9. */
  10.  
  11.  
  12. package{                                                //The begin of an .as file
  13.     import flash.display.MovieClip;      //import some libraries
  14.     import flash.events.Event;
  15.     import flash.events.KeyboardEvent;
  16.  
  17.     public class Script extends MovieClip{        // start the script
  18.    
  19.     private const gravity:int = 1;
  20.     private const max_speed:int = 8;
  21.    
  22.     private const start_x:int = 50;
  23.     private const start_y:int = 50;
  24.    
  25.     private var y_speed:int;
  26.     private var x_speed:int;
  27.    
  28.     private var walkspeed:int = 4;
  29.    
  30.     private var left:Boolean;
  31.     private var up:Boolean;
  32.     private var right:Boolean;
  33.     private var space:Boolean;
  34.        
  35.     private var level:Array = new Array();
  36.     private var tiles:Array = new Array();
  37.    
  38.     private var Map_data:Data = new Data;            // create a version of the Data.as
  39.    
  40.     private var Hero:hero = new hero;
  41.    
  42.         public function Script(){                  // the init (will only be runned once)
  43.             BuildMap();
  44.             create_hero();
  45.             addEventListener(Event.ENTER_FRAME, main);
  46.             stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
  47.             stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
  48.         }
  49.        
  50.         private function main(event:Event){
  51.             update_hero();
  52.         }
  53.        
  54.         private function key_down(event:KeyboardEvent){
  55.             if(event.keyCode == 37){
  56.                 left = true;
  57.             }
  58.             if(event.keyCode == 38){
  59.                 up = true;
  60.             }
  61.             if(event.keyCode == 39){
  62.                 right = true;
  63.             }
  64.         }
  65.        
  66.         private function key_up(event:KeyboardEvent){
  67.             if(event.keyCode == 37){
  68.                 left = false;
  69.             }
  70.             if(event.keyCode == 38){
  71.                 up = false;
  72.             }
  73.             if(event.keyCode == 39){
  74.                 right = false;
  75.             }
  76.         }
  77.                                
  78.            
  79.        
  80. /*
  81. ///    ///    ///////// ///////////    ///////////
  82. ///    ///    ///         ////    ///        ///  ///
  83. ///    ///    ///         ////    ///        ///  ///
  84. //////////    ///////// //////////      ///        ///
  85. //////////    ///////// //// ///    ///      ///
  86. ///    ///    ///         ////   ///        ///  ///
  87. ///    ///    ///         ////   ///        ///  ///
  88. ///    ///    ///////// //// ///    ///////////
  89. */
  90.         private function create_hero(){