New tile based platform engine – part 6 – ladders

It's time to introduce ladders.

Ladders are quite hard to do for one reason: there is a lot of ways of intending ladders.

Can the player jump when on a ladder?

Should a ladder act as a hole if the player walks on it?

These are only two of the many questions you may ask about ladders.

So I have to made some rules:

1) Player climbs the ladder up and down using UP and DOWN arrows

2) Player can jump when on a ladder

3) If the player is falling and encounters a ladder, he will keep falling until he presses UP or DOWN

4) Player can climb a ladder if at least one of its corners is in the ladder

This is the actionscript

ACTIONSCRIPT:
  1. tile_size = 20;
  2. ground_acceleration = 1;
  3. ground_friction = 0.8;
  4. air_acceleration = 0.5;
  5. air_friction = 0.7;
  6. ice_acceleration = 0.15;
  7. ice_friction = 0.95;
  8. treadmill_speed = 2;
  9. max_speed = 3;
  10. xspeed = 0;
  11. yspeed = 0;
  12. falling = false;
  13. gravity = 0.5;
  14. jump_speed = 6;
  15. climbing = false;
  16. climb_speed = 0.8;
  17. level = new Array();
  18. 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];
  19. 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];
  20. 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];
  21. level[3] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
  22. level[4] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
  23. level[5] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 1];
  24. level[6] = [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 6, 1, 0, 0, 0, 0, 0, 1];
  25. level[7] = [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1];
  26. level[8] = [1, 1, 1, 1, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 1];
  27. level[9] = [1, 1, 1, 1, 3, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 4, 4, 4, 4, 1, 1];
  28. player = [5, 1];
  29. function create_level(l) {
  30.     _root.createEmptyMovieClip("level_container", 1);
  31.     level_height = l.length;
  32.     level_width = l[0].length;
  33.     for (y=0; y<level_height; y++) {
  34.         for (x=0; x<level_width; x++) {
  35.             if (l[y][x] != 0) {
  36.                 t = level_container.attachMovie("tile", "t"+y+"_"+x, _root.level_container.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
  37.                 t.gotoAndStop(l[y][x]);
  38.             }
  39.         }
  40.     }
  41.     x_pos = player[0]*tile_size+tile_size/2;
  42.     y_pos = player[1]*tile_size+tile_size/2+1;
  43.     level_container.attachMovie("hero", "hero", _root.level_container.getNextHighestDepth(), {_x:x_pos, _y:y_pos});
  44. }
  45. create_level(level);
  46. _root.onEnterFrame = function() {
  47.     ground_under_feet();
  48.     walking = false;
  49.     climbing = false;
  50.     if (Key.isDown(Key.LEFT)) {
  51.         xspeed -= speed;
  52.         walking = true;
  53.     }
  54.     if (Key.isDown(Key.RIGHT)) {
  55.         xspeed += speed;
  56.         walking = true;
  57.     }
  58.     if (Key.isDown(Key.UP)) {
  59.         get_edges();
  60.         if (top_right == 6 or bottom_right == 6 or top_left == 6 or bottom_left == 6) {
  61.             jumping = false;
  62.             falling = false;
  63.             climbing = true;
  64.             climbdir = -1;
  65.         }
  66.     }
  67.     if (Key.isDown(Key.DOWN)) {
  68.         get_edges();
  69.         if (over == "ladder") {
  70.             jumping = false;
  71.             falling = false;
  72.             climbing = true;
  73.             climbdir = 1;
  74.         }
  75.     }
  76.     if (Key.isDown(Key.SPACE)) {
  77.         get_edges();
  78.         if (!falling and !jumping) {
  79.             jumping = true;
  80.             yspeed = -jump_speed;
  81.         }
  82.     }
  83.     if (!walking) {
  84.         xspeed *= friction;
  85.         if (Math.abs(xspeed)<0.5) {
  86.             xspeed = 0;
  87.         }
  88.     }
  89.     if (xspeed>max_speed) {
  90.         xspeed = max_speed;
  91.     }
  92.     if (xspeed<max_speed*-1) {
  93.         xspeed = max_speed*-1;
  94.     }
  95.     if (falling or jumping) {
  96.         yspeed += gravity;
  97.     }
  98.     if (climbing) {
  99.         yspeed = climb_speed*climbdir;
  100.     }
  101.     if (!falling and !jumping and !climbing) {
  102.         yspeed = 0;
  103.     }
  104.     xspeed += bonus_speed;
  105.     check_collisions();
  106.     level_container.hero._x = x_pos;
  107.     level_container.hero._y = y_pos;
  108.     xspeed -= bonus_speed;
  109. };
  110. function ground_under_feet() {
  111.     bonus_speed = 0;
  112.     left_foot_x = Math.floor((x_pos-6)/tile_size);
  113.     right_foot_x = Math.floor((x_pos+5)/tile_size);
  114.     foot_y = Math.floor((y_pos+9)/tile_size);
  115.     left_foot = level[foot_y][left_foot_x];
  116.     right_foot = level[foot_y][right_foot_x];
  117.     if (left_foot != 0) {
  118.         current_tile = left_foot;
  119.     } else {
  120.         current_tile = right_foot;
  121.     }
  122.     switch (current_tile) {
  123.     case 0 :
  124.         speed = air_acceleration;
  125.         friction = air_friction;
  126.         falling = true;
  127.         break;
  128.     case 1 :
  129.         over = "ground";
  130.         speed = ground_acceleration;
  131.         friction = ground_friction;
  132.         break;
  133.     case 2 :
  134.         over = "ice";
  135.         speed = ice_acceleration;
  136.         friction = ice_friction;
  137.         break;
  138.     case 3 :
  139.         over = "treadmill";
  140.         speed = ground_acceleration;
  141.         friction = ground_friction;
  142.         bonus_speed = -treadmill_speed;
  143.         break;
  144.     case 4 :
  145.         over = "treadmill";
  146.         speed = ground_acceleration;
  147.         friction = ground_friction;
  148.         bonus_speed = treadmill_speed;
  149.         break;
  150.     case 5 :
  151.         over = "cloud";
  152.         speed = ground_acceleration;
  153.         friction = ground_friction;
  154.         break;
  155.     case 6 :
  156.         over = "ladder";
  157.         speed = ground_acceleration;
  158.         friction = ground_friction;
  159.         break;
  160.     }
  161. }
  162. function check_collisions() {
  163.     y_pos += yspeed;
  164.     get_edges();
  165.     // collision to the bottom
  166.     if (yspeed>0) {
  167.         if ((bottom_right != 0 and bottom_right != 6) or (bottom_left != 0 and bottom_left != 6)) {
  168.             if (bottom_right != 5 and bottom_left != 5) {
  169.                 y_pos = bottom*tile_size-9;
  170.                 yspeed = 0;
  171.                 falling = false;
  172.                 jumping = false;
  173.             } else {
  174.                 if (prev_bottom<bottom) {
  175.                     y_pos = bottom*tile_size-9;
  176.                     yspeed = 0;
  177.                     falling = false;
  178.                     jumping = false;
  179.                 }
  180.             }
  181.         }
  182.     }
  183.     // collision to the top                          
  184.     if (yspeed<0) {
  185.         if ((top_right != 0 and top_right != 5 and top_right != 6) or (top_left != 0 and top_left != 5 and top_left != 6)) {
  186.             y_pos = bottom*tile_size+1+8;
  187.             yspeed = 0;
  188.             falling = false;
  189.             jumping = false;
  190.         }
  191.     }
  192.     x_pos += xspeed;
  193.     get_edges();
  194.     // collision to the left          
  195.     if (xspeed<0) {
  196.         if ((top_left != 0 and top_left != 5 and top_left != 6) or (bottom_left != 0 and bottom_left != 5 and bottom_left != 6)) {
  197.             x_pos = (left+1)*tile_size+6;
  198.             xspeed = 0;
  199.         }
  200.     }
  201.     // collision to the right                                                  
  202.     if (xspeed>0) {
  203.         if ((top_right != 0 and top_right != 5 and top_right != 6) or (bottom_right != 0 and bottom_right != 5 and bottom_right != 6)) {
  204.             x_pos = right*tile_size-6;
  205.             xspeed = 0;
  206.         }
  207.     }
  208.     x_pos = Math.round(x_pos);
  209.     y_pos = Math.round(y_pos);
  210.     prev_bottom = bottom;
  211. }
  212. function get_edges() {
  213.     // right edge
  214.     right = Math.floor((x_pos+5)/tile_size);
  215.     // left edge  
  216.     left = Math.floor((x_pos-6)/tile_size);
  217.     // bottom edge
  218.     bottom = Math.floor((y_pos+8)/tile_size);
  219.     // top edge
  220.     top = Math.floor((y_pos-9)/tile_size);
  221.     // adjacent tiles
  222.     top_right = level[top][right];
  223.     top_left = level[top][left];
  224.     bottom_left = level[bottom][left];
  225.     bottom_right = level[bottom][right];
  226. }

and this is the result

Any game on the horizon? I am making one...

Download the source code and enjoy.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (23 votes, average: 4.74 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

23 Responses to “New tile based platform engine – part 6 – ladders”

  1. David D on September 23rd, 2008 11:02 am

    This is awesome! But is this the last part? You should add some spikes or something that would kill the player. And maybe comment on the code?

  2. dix huit on September 23rd, 2008 11:36 am

    Top banana! Any news on the WordPress plugin Emanuele?

  3. Dean Watts on September 23rd, 2008 1:06 pm

    Is this the last one?
    Its awesome but we need enemies!

  4. Prankard on September 23rd, 2008 1:50 pm

    Looks really impressive. A good engine so far in only 6 steps with lots of possibility.

    Found a small glitch, when you run off the edge (where the ladders are) you can easily get back up on the ledge even when your falling off by running back towards it in mid-air.
    I guess this could be fixed by having another side hitTest nearer his feet.

  5. Everton Denis on September 23rd, 2008 4:29 pm

    Good job!!!!!
    But…I’m waiting in actionscript 3.0!!!!

  6. sam on September 23rd, 2008 7:24 pm

    i think this is a great engine and just what i need for my next game. this will be my first attempt at a tile based game but this tutorial will help me greatly.

    things that could possibly be added…
    spikes,enemys(plus kill, maybe by jumping on them.),left/right/jump animations,scrolling background.

    with those added it will be an absoloutly brillantant game.

    please reply if your do plan to add any of these features.

  7. Kevin on September 23rd, 2008 9:34 pm

    Found another glitch. While climbing down the ladder it’s possible to move left or right and still be climbing downwards as long as you continue to hold the down button.

    Still, amazing work, it’s looking great so far. :)

  8. Enrique David on September 23rd, 2008 10:09 pm

    Hi, i sugest you… that check collision in ladder when the player jumo and going down… isn’t ?

  9. Justin on September 24th, 2008 4:26 am

    Yes, please do another step where the player dies…

    IE: Spikes

  10. JL on September 24th, 2008 6:56 am

    This series of tutorials is fantastic.
    I see so many possibilities.
    Thank you!!!

  11. bigbill on September 24th, 2008 9:45 am

    Great stuff but I’m having issues, I you move to the right hand side of the screen and jump, you seem to get stuck on the wall. Any idea to fix this? Thnaks

  12. Bob on September 24th, 2008 9:45 am

    From one game developer to another I’ll thank you again for doing this. You are an incredible coder and I’ve been following your work for a long time. Thanks a ton and get some sleep!

  13. faaa on September 24th, 2008 10:06 am

    i don’t know if its a bug but you can walk on top of the ladder without falling,but if you jump the ladder become a “hole” and you’ll fall

  14. matt on September 24th, 2008 11:16 pm

    One glitch is that you can float slowly downwards if you hold down and the last tile you touched is a ladder tile.

  15. Questo on September 25th, 2008 12:09 am

    Another glitch If you jump and the top of your character hits a grey tile while still touching a ladder you float in midair until you aren’t touching ladder anymore. By the way this engine is awsome but for some reason if I make a square who is center aligned the hittests are way off. Do I need to make it to the exact dimensions of the one you have?

  16. martin on September 27th, 2008 6:32 am

    many thanks, it’s very useful

  17. Jai on September 28th, 2008 10:14 am

    maybe, when going up ladders, it would be a good idea that you could only stay suspended on a ladder for a certain amount of time, or you fall down…?
    Good tutorial :)

  18. Andrius on September 29th, 2008 6:46 am

    One of the best tutorials here! I have looked ~75% of your flash tutorials and I like the most of ‘em’. I’m making a little game for this. I made now exit and falling function (when you fall out from level). And I know i can make enemies and something like pipes so you only hit them from left and right so you can go downwards trough them. But yea, ladders have some bugs.

  19. CoryMathews on September 29th, 2008 3:58 pm

    Looking great man. This is one of your best sets of tutorials. Really enjoying reading these keep em up.

  20. Andrius on September 30th, 2008 6:27 pm

    My spike code:

    where you add tile add this:

    case 1 :
    over = “ground”;
    x_pos = 40
    y_pos = 40
    break;

    It’s exaple, change x and y of player by detecting em using variables. That’s how I also do falling:

    if(y_pos >140 ){
    y_pos = 40
    x_pos = 40
    }

    It’s easy.

Leave a Reply




Trackbacks

  1. New tile based platform engine - AS3 version : Emanuele Feronato on September 25th, 2008 11:41 am

    [...] This is the AS3 version of part 6. [...]

  2. New tile based platform engine - part 7 - trampolines : Emanuele Feronato on September 30th, 2008 1:00 pm

    [...] the AS3 translation of AS2 part 6, it’s time to introduce [...]

  3. New tile based platform engine - more theory : Emanuele Feronato on October 6th, 2008 6:30 pm

    [...] bug appeared in part 6 and was fixed in part [...]

Posts