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

According to Bart de Boer, this is the step 1.5 of the Step by step AS3 translation of Creation of a platform game with Flash.

He fixed the jump issue and improved the collision engine.

As in the previous example, the file Script.as is the main class:

ACTIONSCRIPT:
  1. /*____________________________________________________
  2. |______________ register of functions _______________|
  3. |____________________________________________________|
  4. - main()            only calling to update_hero() (every frame)
  5. - create_hero()  creates hero as the var "Hero"
  6. - update_hero()  check collision an move
  7. - BuildMap()        load and create the level
  8. extern
  9. Data.as
  10. - Setup()         create levels
  11. collision_manager.as
  12. - Setup(size,map,hero)    setup the map
  13. - Solve_all(forecastx,forecasty)        solves the collsions and checks for a jump
  14. */
  15.  
  16.  
  17. package{                                                //The begin of an .as file
  18.     import flash.display.MovieClip;      //import some libraries
  19.     import flash.events.Event;
  20.     import flash.events.KeyboardEvent;
  21.  
  22.     public class Script extends MovieClip{        // start the script
  23.    
  24.     private const gravity:int = 1;
  25.     private const max_speed:int = 8;
  26.    
  27.     private const walkspeed:int = 4;
  28.     private const jumpspeed:int = 10;
  29.    
  30.     private var forecast_x:int;
  31.     private var forecast_y:int;
  32.    
  33.     private const start_x:int = 50;
  34.     private const start_y:int = 50;
  35.    
  36.     private var left:Boolean;
  37.     private var up:Boolean;
  38.     private var right:Boolean;
  39.     private var space:Boolean;
  40.        
  41.     private var level:Array = new Array();
  42.     private var tiles:Array = new Array();
  43.    
  44.     private var Map_data:Data = new Data;            // create a version of the Data.as
  45.     private var Hero_col:collision_manager = new collision_manager;
  46.    
  47.     private var Hero:hero = new hero;
  48.    
  49.         public function Script(){                  // the init (will only be runned once)
  50.             BuildMap();
  51.             create_hero();
  52.             addEventListener(Event.ENTER_FRAME, main);
  53.             stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
  54.             stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
  55.            
  56.             Hero_col.Setup(25,level,Hero);
  57.         }
  58.        
  59.         private function main(event:Event){
  60.             update_hero();
  61.         }
  62.        
  63.         private function key_down(event:KeyboardEvent){
  64.             if(event.keyCode == 37){
  65.                 left = true;
  66.             }
  67.             if(event.keyCode == 38){
  68.                 up = true;
  69.             }
  70.             if(event.keyCode == 39){
  71.                 right = true;
  72.             }
  73.         }
  74.        
  75.         private function key_up(event:KeyboardEvent){
  76.             if(event.keyCode == 37){
  77.                 left = false;
  78.             }
  79.             if(event.keyCode == 38){
  80.                 up = false;
  81.             }
  82.             if(event.keyCode == 39){
  83.                 right = false;
  84.             }
  85.         }
  86.                                
  87.            
  88.        
  89. /*
  90. ///    ///    ///////// ///////////    ///////////
  91. ///    ///    ///         ////    ///        ///  ///
  92. ///    ///    ///         ////    ///        ///  ///
  93. //////////    ///////// //////////      ///        ///
  94. //////////    ///////// //// ///    ///      ///
  95. ///    ///    ///         ////   ///        ///  ///
  96. ///    ///    ///         ////   ///        ///  ///
  97. ///    ///    ///////// //// ///    ///////////
  98. */
  99.         private function create_hero(){
  100.             addChild(Hero);
  101.             Hero.x = start_x;
  102.             Hero.y = start_y;
  103.             Hero.x_speed = 0;
  104.             Hero.y_speed = 0;
  105.         }
  106.        
  107.         private function update_hero(){
  108.             Hero.y_speed += gravity;
  109.             if(left){
  110.                 Hero.x_speed = -walkspeed;
  111.             }
  112.             if(right){
  113.                 Hero.x_speed = walkspeed;
  114.             }
  115.             if(up && Hero_col.can_jump){
  116.                 Hero.y_speed = -jumpspeed;
  117.             }
  118.            
  119.             if(Hero.y_speed> max_speed){
  120.                 Hero.y_speed = max_speed;
  121.             }
  122.            
  123.             forecast_y = Hero.y + Hero.y_speed;
  124.             forecast_x = Hero.x + Hero.x_speed;
  125.            
  126.             Hero_col.solve_all(forecast_x, forecast_y);
  127.            
  128.            
  129.             Hero.x_speed = 0;
  130.         }
  131.    
  132.    
  133. /*
  134. |||||||||||   |||||||||||   |||||||||| ||||||||||
  135. ||||||||||||    ||||||||||||     ||||    ||||  ||||  |||
  136. ||||    ||||| |||||  ||||  ||||||||||||    ||||||||||
  137. ||||      |||||||      ||||    ||||   |||| ||||
  138. ||||        |||   |||| ||||   ||||   ||||
  139. ||||                    ||||    ||||      ||||  ||||
  140. */
  141.                
  142.         private function BuildMap(){
  143.             Map_data.Setup();                                    // setup data from extern file
  144.            
  145.             level = Map_data.level1;                                        // get data from extern file
  146.            
  147.             for(var t = 0; t <level.length; t++){
  148.                 for(var u = 0; u <level[t].length; u++){
  149.                     if(level[t][u] != 0){                           //if the data is not null
  150.                         var new_tile:platform_tile = new platform_tile;  //than build a tile
  151.                        
  152.                         addChild(new_tile);         //put it on the screen
  153.                        
  154.                         new_tile.gotoAndStop(1);
  155.                         new_tile.x = u * 25;
  156.                         new_tile.y = t * 25;
  157.                        
  158.                         tiles.push(new_tile);                        //put it in an array
  159.                     }
  160.                 }
  161.             }
  162.         }
  163.     }
  164. }
  165.  
  166. // You may not post this on any other site than: http://www.emanueleferonato.com or http://www.frozenhaddock.com. You may not claim that you wrote this code. I'm not responsible for any nuclear activity that may be caused by this script.

The level is stored in the 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, 1, 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. }
  27.  
  28. // You may not post this on any other site than: http://www.emanueleferonato.com or http://www.frozenhaddock.com. You may not claim that you wrote this code. I'm not responsible for any nuclear activity that may be caused by this script.

While the solve_all function contained in the collision_manager.as solves all collisions in the forecast position of the player

ACTIONSCRIPT:
  1. package{
  2.    
  3.     public class collision_manager{
  4.         private var Tile_size:int;      //containing the size of the tiles
  5.         private var level;          //level data container
  6.         private var forecast_x:int;   //where the player will be at the end of the frame
  7.         private var forecast_y:int;   //"                          "
  8.         public var can_jump:Boolean;        //same as in Emanuele's tutorial
  9.         private var hero;               //to store the Hero object in
  10.        
  11.         public function Setup(size,map,heroj){    //my standard setup function(init)
  12.             Tile_size = size;                  //initializing al vars
  13.             level = map;
  14.             hero = heroj;
  15.         }
  16.        
  17.         public function get_corners(point_x,point_y){            
  18.         //get the position of the four corners of the hero
  19.            
  20.             hero.downy = Math.round((point_y + 10 - Tile_size/2) / Tile_size);
  21.             hero.upy = Math.round((point_y - 10 - Tile_size/2) / Tile_size);
  22.             hero.rightx = Math.round((point_x + 5 - Tile_size/2) / Tile_size)
  23.             hero.leftx = Math.round((point_x - 5 - Tile_size/2) / Tile_size);
  24.             /*
  25.             Looks in wich tiles these four point are.
  26.             *Attention* TILES and not pixel!
  27.            
  28.             Will be used to get the position of the corners
  29.             and to get the end position of the hero.
  30.             */
  31.            
  32.             hero.downleft = level[hero.downy][hero.leftx];
  33.             hero.downright = level[hero.downy][hero.rightx];
  34.             hero.upright = level[hero.upy][hero.rightx];
  35.             hero.upleft = level[hero.upy][hero.leftx];
  36.            
  37.            
  38.             /*
  39.             Gets the sort tile the position of the corners has.
  40.            
  41.             One means there can be collision, zero is air.     
  42.             */
  43.         }
  44.        
  45.         public function check_ground(){
  46.             hero.downy = Math.round((hero.y + 11 - Tile_size/2) / Tile_size);
  47.             hero.rightx = Math.round((hero.x + 5 - Tile_size/2) / Tile_size)
  48.             hero.leftx = Math.round((hero.x - 5 - Tile_size/2) / Tile_size);
  49.             // Makes three points in tile-coordinates
  50.            
  51.             hero.downleft = level[hero.downy][hero.leftx];
  52.             hero.downright = level[hero.downy][hero.rightx];
  53.             //Checks the sort
  54.            
  55.             if(hero.downleft == 1 || hero.downright ==1){      //if there is any collision by the hero's feet
  56.                 can_jump = true;                                //than can jump
  57.             }
  58.             else{
  59.                 can_jump = false;                        //else not
  60.             }
  61.         }
  62.        
  63.         public function solve_all(forecastx, forecasty){
  64.         /*
  65.         This function looks hard... an it is...
  66.         I've been working on it for loads of hours, and this is the result.
  67.        
  68.         This is the best collision-test I've ever made, for sqaures.
  69.        
  70.         It's still readable, because I made it simple.
  71.        
  72.         It's four times almost the same. It checks for collision between the spots,
  73.         the four spots if there only were x_speeds, the four spots if there only was y_speed,
  74.         the four spots if there were both y- and x_speed.   
  75.         That are 3*4 = 12 spots.
  76.        
  77.        
  78.         //////////////////////////////////
  79.         Why's:
  80.        
  81.         question:
  82.         Why did I used forecast_x and forecast_y and not just hero.x and hero.y?
  83.        
  84.         answer:
  85.         Changing the hero over the screen will cost loads of CPU because an hero
  86.         exist out of loads of pixels. The forecast's are just simple variables
  87.        
  88.         question:
  89.         Is such a huge function not requiring a lot of CPU?
  90.        
  91.         answer:
  92.         It doesn't uses any while- or for loops, Math. functions and the most important of
  93.         all: no hitTestPoint()/hitTestObject(). So it's a lot faster.
  94.