Platform with ladder

July 13, 2009 12:40 PM Published by

This is a quick update to the tutorial/engine started with Creation of a platform game with Flash – step 1.

I fixed some bugs and added a ladder object (the one defined in the array with “2” and painted in light purple).

Use arrows to move and space to jump.

I won’t explain the script line by line at the moment because I want to add more features before writing a complete tutorial.

But the source code is someway commented

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. 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);
  22. 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);
  23. 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);
  24. 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);
  25. 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);
  26. 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);
  27. level[6] = new Array(1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  28. 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);
  29. 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);
  30. 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);
  31. 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);
  32. 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);
  33. level[12] = new Array(1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
  34. 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);
  35. level[14] = 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);
  36. for (y=0; y<=14; y++) {
  37. for (x=0; x<=24; x++) {
  38. if (level[y][x] == 1) {
  39. place_brick = lev.attachMovie(“block”, “block_”+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x😡*20+10, _y:y*20+10});
  40. place_brick.gotoAndStop(level[y][x]);
  41. }
  42. if (level[y][x] == 2) {
  43. place_brick = lad.attachMovie(“block”, “block_”+lad.getNextHighestDepth(), lad.getNextHighestDepth(), {_x😡*20+10, _y:y*20+10});
  44. place_brick.gotoAndStop(level[y][x]);
  45. }
  46. }
  47. }
  48. _root.attachMovie(“player”,“player”,_root.getNextHighestDepth(),{_x:40, _y:40});
  49. // end of level creation
  50. player.onEnterFrame = function() {
  51. if (Key.isDown(Key.LEFT)) {
  52. if (climbing) {
  53. xspeed = -climb_speed;
  54. }
  55. else {
  56. if (walking_while_jumping or can_jump) {
  57. xspeed = -walk_speed;
  58. }
  59. }
  60. }
  61. if (Key.isDown(Key.RIGHT)) {
  62. if (climbing) {
  63. xspeed = climb_speed;
  64. }
  65. else {
  66. if (walking_while_jumping or can_jump) {
  67. xspeed = walk_speed;
  68. }
  69. }
  70. }
  71. if (!feet_on_ladder()) {
  72. climbing = false;
  73. }
  74. if (Key.isDown(Key.UP)) {
  75. if (feet_on_ladder()) {
  76. yspeed = -climb_speed;
  77. climbing = true;
  78. jumping = false;
  79. }
  80. }
  81. if (Key.isDown(Key.DOWN)) {
  82. if (feet_on_ladder() or ladder_under_my_feet()) {
  83. yspeed = climb_speed;
  84. climbing = true;
  85. jumping = false;
  86. }
  87. }
  88. if ((Key.isDown(Key.SPACE)) and can_jump and !jumping and !climbing) {
  89. yspeed -= jump_power;
  90. jumping = true;
  91. }
  92. // adjusting y speed
  93. if (!climbing) {
  94. yspeed += gravity;
  95. }
  96. if (yspeed>max_yspeed) {
  97. yspeed = max_yspeed;
  98. }
  99. if (level_under_my_feet() and !jumping and !climbing) {
  100. yspeed = 0;
  101. }
  102. if (ladder_under_my_feet() and !jumping and !climbing) {
  103. yspeed = 0;
  104. }
  105. forecast_x = this._x+xspeed;
  106. forecast_y = this._y+yspeed;
  107. // floor control
  108. while (_root.lev.hitTest(forecast_x, forecast_y+this._height/21, true)) {
  109. forecast_y–;
  110. xspeed = 0;
  111. yspeed = 0;
  112. jumping = false;
  113. }
  114. // ceiling control
  115. while (_root.lev.hitTest(forecast_x, forecast_y-this._height/2, true)) {
  116. forecast_y++;
  117. yspeed = 0;
  118. }
  119. // left wall control
  120. while (_root.lev.hitTest(forecast_x-this._width/2+1, forecast_y, true)) {
  121. forecast_x++;
  122. xspeed = 0;
  123. }
  124. // right wall control
  125. while (_root.lev.hitTest(forecast_x+this._width/2, forecast_y, true)) {
  126. forecast_x–;
  127. xspeed = 0;
  128. }
  129. this._x = forecast_x;
  130. this._y = forecast_y;
  131. // adjusting speeds for next frame
  132. xspeed = 0;
  133. if (climbing) {
  134. yspeed = 0;
  135. }
  136. };
  137. function feet_on_ladder() {
  138. return _root.lad.hitTest(player._x, player._y+player._height/21, true);
  139. }
  140. function level_under_my_feet() {
  141. return _root.lev.hitTest(player._x, player._y+player._height/2, true);
  142. }
  143. function ladder_under_my_feet() {
  144. return _root.lad.hitTest(player._x, player._y+player._height/2, true);
  145. }

This is the result you will get:

And this is the source code to download.

Categorised in:


Tag Cloud