Creation of a platform game with Flash - step 3

To show you how hard I am going to try wiping the vaporware out of my life, I am publishing the 3rd part of the platform game tutorial.

Read parts 1 and 2 if you don't remember what I am talking about.

In this update, I introduced two new tile types: the lava tile that kills you when you walk over it (read: when you walk over it, not when you touch it), and the horizontally moving tile, that automatically (read: automatically) connects two spots.

The code needs to be cleaned, but everything seems to work

ACTIONSCRIPT:
  1. // player default speeds
  2. xspeed = 0;
  3. yspeed = 0;
  4. max_yspeed = 10;
  5. walk_speed = 4;
  6. climb_speed = 2;
  7. // am I climbing?
  8. climbing = false;
  9. // am I jumping?
  10. jumping = false;
  11. // can I jump?
  12. can_jump = true;
  13. // gravity & jump settings
  14. gravity = 1;
  15. jump_power = 10;
  16. walking_while_jumping = true;
  17. // level creation
  18. level = new Array();
  19. _root.createEmptyMovieClip("lev", _root.getNextHighestDepth());
  20. _root.createEmptyMovieClip("lad", _root.getNextHighestDepth());
  21. _root.createEmptyMovieClip("lava", _root.getNextHighestDepth());
  22. _root.createEmptyMovieClip("moving", _root.getNextHighestDepth());
  23. level[0] = new Array(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);
  24. level[1] = new Array(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);
  25. level[2] = new Array(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);
  26. level[3] = new Array(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);
  27. level[4] = new Array(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);
  28. level[5] = new Array(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);
  29. level[6] = new Array(1, 0, 0, 0, 0, 0, 1, 1, 1, 4, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  30. level[7] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  31. level[8] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  32. level[9] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  33. level[10] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  34. level[11] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  35. level[12] = new Array(1, 0, 0, 0, 0, 0, 0, 1, 3, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  36. level[13] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
  37. level[14] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1);
  38. for (y=0; y<=14; y++) {
  39.     for (x=0; x<=24; x++) {
  40.         if (level[y][x] == 1) {
  41.             place_brick = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
  42.             place_brick.gotoAndStop(level[y][x]);
  43.         }
  44.         if (level[y][x] == 2) {
  45.             ladder_brick = lad.attachMovie("block", "block_"+lad.getNextHighestDepth(), lad.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
  46.             ladder_brick.gotoAndStop(level[y][x]);
  47.         }
  48.         if (level[y][x] == 3) {
  49.             lava_brick = lava.attachMovie("block", "block_"+lava.getNextHighestDepth(), lava.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
  50.             lava_brick.gotoAndStop(level[y][x]);
  51.         }
  52.         if (level[y][x] == 4) {
  53.             moving_brick = moving.attachMovie("block", "block_"+moving.getNextHighestDepth(), moving.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
  54.             moving_brick.gotoAndStop(level[y][x]);
  55.             moving_brick.dir = 1;
  56.             moving_brick.onEnterFrame = function() {
  57.                 this._x += this.dir;
  58.                 if ((_root.lev.hitTest(this._x+10, this._y, true) and (this.dir>0)) or ((_root.lev.hitTest(this._x-10, this._y, true) and (this.dir<0)))) {
  59.                     this.dir *= -1;
  60.                 }
  61.                 if (_root.player.hitTest(this._x, this._y-11, true) or _root.player.hitTest(this._x+5, this._y-11, true) or _root.player.hitTest(this._x-5, this._y-11, true)) {
  62.                     player._x += this.dir;
  63.                 }
  64.             };
  65.         }
  66.     }
  67. }
  68. _root.attachMovie("player", "player", _root.getNextHighestDepth(), {_x:40, _y:40});
  69. // end of level creation
  70. player.onEnterFrame = function() {
  71.     if (Key.isDown(Key.LEFT)) {
  72.         if (climbing) {
  73.             xspeed = -climb_speed;
  74.         } else {
  75.             if (walking_while_jumping or can_jump) {
  76.                 xspeed = -walk_speed;
  77.             }
  78.         }
  79.     }
  80.     if (Key.isDown(Key.RIGHT)) {
  81.         if (climbing) {
  82.             xspeed = climb_speed;
  83.         } else {
  84.             if (walking_while_jumping or can_jump) {
  85.                 xspeed = walk_speed;
  86.             }
  87.         }
  88.     }
  89.     if (!feet_on_ladder()) {
  90.         climbing = false;
  91.     }
  92.     if (Key.isDown(Key.UP)) {
  93.         if (feet_on_ladder()) {
  94.             yspeed = -climb_speed;
  95.             climbing = true;
  96.             jumping = false;
  97.         }
  98.     }
  99.     if (Key.isDown(Key.DOWN)) {
  100.         if (feet_on_ladder() or ladder_under_my_feet()) {
  101.             yspeed = climb_speed;
  102.             climbing = true;
  103.             jumping = false;
  104.         }
  105.     }
  106.     if ((Key.isDown(Key.SPACE)) and can_jump and !jumping and !climbing) {
  107.         yspeed -= jump_power;
  108.         jumping = true;
  109.     }
  110.     // adjusting y speed                       
  111.     if (!climbing) {
  112.         yspeed += gravity;
  113.     }
  114.     if (yspeed>max_yspeed) {
  115.         yspeed = max_yspeed;
  116.     }
  117.     if (level_under_my_feet() and !jumping and !climbing) {
  118.         yspeed = 0;
  119.     }
  120.     if (ladder_under_my_feet() and !jumping and !climbing) {
  121.         yspeed = 0;
  122.     }
  123.     forecast_x = this._x+xspeed;
  124.     forecast_y = this._y+yspeed;
  125.     // lava control
  126.     if (_root.lava.hitTest(forecast_x, forecast_y+this._height/2-1, true)) {
  127.         forecast_x = 40;
  128.         forecast_y = 40;
  129.         jumping = false;
  130.     }
  131.     // floor control             
  132.     while ((_root.lev.hitTest(forecast_x, forecast_y+this._height/2-1, true)) or (_root.moving.hitTest(forecast_x, forecast_y+this._height/2-1, true))) {
  133.         forecast_y--;
  134.         xspeed = 0;
  135.         yspeed = 0;
  136.         jumping = false;
  137.     }
  138.     // ceiling control
  139.     while (_root.lev.hitTest(forecast_x, forecast_y-this._height/2, true) or (_root.lava.hitTest(forecast_x, forecast_y-this._height/2, true)) or (_root.moving.hitTest(forecast_x, forecast_y-this._height/2, true))) {
  140.         forecast_y++;
  141.         yspeed = 0;
  142.     }
  143.     // left wall control
  144.     while (_root.lev.hitTest(forecast_x-this._width/2+1, forecast_y, true)) {
  145.         forecast_x++;
  146.         xspeed = 0;
  147.     }
  148.     // right wall control
  149.     while (_root.lev.hitTest(forecast_x+this._width/2, forecast_y, true)) {
  150.         forecast_x--;
  151.         xspeed = 0;
  152.     }
  153.     this._x = forecast_x;
  154.     this._y = forecast_y;
  155.     // adjusting speeds for next frame
  156.     xspeed = 0;
  157.     if (climbing) {
  158.         yspeed = 0;
  159.     }
  160. };
  161. function feet_on_ladder() {
  162.     return _root.lad.hitTest(player._x, player._y+player._height/2-1, true);
  163. }
  164. function level_under_my_feet() {
  165.     return _root.lev.hitTest(player._x, player._y+player._height/2, true);
  166. }
  167. function ladder_under_my_feet() {
  168.     return _root.lad.hitTest(player._x, player._y+player._height/2, true);
  169. }

and, more important, I am learning how to fight vaporware plague.

Download the source code and give me the proof I can defeat vaporware.

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 5 out of 5)
Loading ... Loading ...

» 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.

28 Comment(s)

  1. RJ | Mar 19, 2008 | Reply

    Very useful, thanks

  2. Daniel | Mar 19, 2008 | Reply

    Nice,
    I hope you also create a side scrolling code, and clouds code, etc.
    Thanks

  3. David | Mar 19, 2008 | Reply

    Nice… Could you make the Super mario jump…. Thing? That would be awesome :).

  4. Grifo | Mar 19, 2008 | Reply

    Let us work together against Vaporware!

    Looks like I just came up with an enemy for our bombuzal. The Vaporbomber!!

  5. styxtwo | Mar 19, 2008 | Reply

    very sweet, and we all have to work agains vaporware ;)

  6. EagleVision | Mar 19, 2008 | Reply

    Thats a great Idea! :D

    Nice emanuele.

  7. Jack Hopkisn | Mar 19, 2008 | Reply

    Aweseome!
    The Vaporware has been…..Vaporized!!!!

  8. Kesh | Mar 20, 2008 | Reply

    little bug report: when u fall into the red, you are still jumpable. so while ur falling, u can jump thus defying gravity.

  9. Michael | Mar 20, 2008 | Reply

    You’re doing some good stuff here, Emanuele… keep it up :)

    I feel like I want to make another platform game now!

  10. Suely (Snakesue) | Mar 20, 2008 | Reply

    Emanuele, you hit me and everybody. Wake up people. Thanks and a lot of kisses for you.

  11. abhilash | Mar 20, 2008 | Reply

    please would u do the next tut on line rider i’am still waiting for that from last year.
    Nice tut ,ur first tutorial against vaporware :)

  12. Turboll | Mar 20, 2008 | Reply

    Many thanks for your great blog man, it is a great online game programming resource. I read most of the game design posts and you have inspired me picking up flash and actionscript.

    Your blog is as good as any game programming books, and it is an helpful guide in any programming language as it covers many basic gaming concepts. It has really helped me understanding some basic game programming functions that whas left in the dark before. It is allso helpful seeing the result of the code instantly in your web-browser.

    If I may ask: is it correct that your examples are written in actionscript 2? Can you recommend starting with As 2 or 3.0? (I’ve done some small games in C before).

    Best Regards.

  13. Eblup | Mar 21, 2008 | Reply

    Hello this tutorial can lead to side scrolling that can be done by having everything in one moiveclip and have it move to keep the guy in the screen.

  14. Amanuele | Mar 22, 2008 | Reply

    It looks like a very nice tutorial but can you help me? like can you breifly explain step by step or should I just read from parts 1-3 again?

  15. Eblup | Mar 24, 2008 | Reply

    Hello, you should put in a area on youre sight for beginers and have some tutorials for actionscript 3.0.
    P.S. i think this may help you’re website a lot.
    ;}

  16. Eblup | Mar 24, 2008 | Reply

    sorry for posting so much but if you have this
    1,1,1,1,1,1 and if you run into the moving block
    1,4,0,0,0,1 you glitch out.
    1,1,1,1,1,1

  17. Emanuele Feronato | Mar 24, 2008 | Reply

    I know, the moving platform should kill the player but at the moment this does not happen.

    The first AS3 tutorial should be out today…

  18. Josh of Pixelton | Apr 1, 2008 | Reply

    Hello Emanuele,

    I simply had to say this is my favorite blog. I’m very bad at flash but your clear tutorials and awesome samples help show me the way. I feel that I might actually be able to learn this…

    So thank you! :D

    - Josh

  19. Josh of Pixelton | Apr 2, 2008 | Reply

    Oh, and I’d love to learn how to create a level editor. Arrays are my enemy. Thanks again.

  20. Shaun | Apr 5, 2008 | Reply

    Hey I have a question. why does it get all messed up when you raise the speed of the main character. It jumps up to the floor when you hit the wall at a higher speed

  21. ricky | Apr 9, 2008 | Reply

    heyy thnx for this TUT its great
    but im wondering how to make a WIN spot?
    when u arive in the win-block it loads a swf.
    can anyone help me?

  22. Joshua Thong | Apr 14, 2008 | Reply

    great tutorial, but can you like make a tut for a non-tile platform game? I’m not really at home with xml and other stuff, only actionscript. like, can you publish a tut on how to make a platform game where the hero is a symbol and the map and other obstacles etc. is a symbol too? XD ty man

  23. Mini Chris | Apr 17, 2008 | Reply

    well so far tonypa’s is better but theres a few thing both of you dont have and thats freaking win spot to take you to next level i have tried and tried to do it but i cant.

  24. Jake | Apr 25, 2008 | Reply

    Need to add coins and an objective i dont know how Help need action script

  25. Quintus | May 16, 2008 | Reply

    Heho! :D
    i have a question.
    i am making a platform game in a soort op rpg style.

    but how do i make enemy’s, that attack and walk to you if you come close to them?

  26. Quintus | May 17, 2008 | Reply

    just put this action on your WIN place (alpha 0)

    onClipEvent(enterFrame) {
    if(_root.player.hitTest(this)) {
    //what ever you would happen if you win
    }
    }
    or do you mean somthing totally different where i dont have a clue in what for script it is?

  27. brart | Jun 10, 2008 | Reply

    For a port to the next level you have to make a 3 dimensial array where the first number is the level.
    When you finish the level, remove the movieclip lev, recreate it and than build the map with a higher number.

    I will try to make a tut for emanuele about this genre in as3. Maybe it will contain those “next level teleports”.

    Brart

  28. fraser | Jul 12, 2008 | Reply

    now all we need is a tutorial for a door then were pretty much done and thanks these tutorials are a real help for flash game making

Post a Comment