From zero to Bombuzal - step 4

One of the biggest problems of step 3 was Bombuzal waled sooo slowly.

So I added a variable called walk_speed where I assign the walking speed in pixels per frame.

Then, in the original game you have to think fast. You can't stay on the same tile for more than about 5 secods, or the tile where you are over will turn in a spinning tile that will spin Bombuzal in a random direction everytime it walks on it.

But it's not over: if Bombuzal stays on a tile with a bomb for more than 5 seconds, the tile won't become a spinning tile but the bomb will explode, killing Bombuzal.

If Bombuzal stays on a disappearing tile for more than 5 seconds, the disappearing tile will... disappear, making Bombuzal fall down.

A lot of exceptions for an old C64 game!

At the moment, I won't make any tile change, but I will move Bombuzal in a random direction if it stays more than 5 seconds on the same tile.

I am creating a counter called does_not_move that will count all frames passed since the last time Bombuzal moved. If the counter reaches 150, then I'll randomly move Bombuzal

ACTIONSCRIPT:
  1. //create an invisible movie clip across the entire stage using API
  2. _root.createEmptyMovieClip("base", 1);
  3. with (base) {
  4.     lineStyle(2, 0x0000000, 0);
  5.     beginFill(0x00000000, 0);
  6.     lineTo(Stage.width, 0);
  7.     lineTo(Stage.width, Stage.height);
  8.     lineTo(0, Stage.height);
  9.     lineTo(0, 0);
  10. }
  11. //whether to pan or not
  12. level_pan = false;
  13. //tile size
  14. tile_size = 50;
  15. // pan variables
  16. pan_dist = 50;
  17. pan_speed = 5;
  18. // tiles array generation
  19. tiles = [[0, 1, 0, 0], [1, 1, 2, 1], [1, 2, 1, 2], [0, 0, 1, 0]];
  20. // bombs array generation
  21. bombs = [[0, 1, 0, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 0, 1, 0]];
  22. // main sprite
  23. // xpos: bombuzal x position
  24. // ypos: y position
  25. // moving: bombuzal is moving or not
  26. // facing: the side bombuzal is facing on
  27. bombuzal_obj = {xpos:0, ypos:1, moving:false, facing:"right"};
  28. // bombuzal walk speed, in pixels/frame
  29. walk_speed = 5;
  30. // time passed since bombuzal last move
  31. does_not_move = 0;
  32. // creating the level movieclip
  33. _root.createEmptyMovieClip("level", _root.getNextHighestDepth());
  34. // placing tiles
  35. for (x=0; x<tiles.length; x++) {
  36.     for (y=0; y<tiles[x].length; y++) {
  37.         // notice as it's not [x][y] but [y][x]
  38.         if (tiles[y][x]) {
  39.             placed = level.attachMovie("tile", "tile"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
  40.             placed.gotoAndStop(tiles[y][x]);
  41.         }
  42.     }
  43. }
  44. // placing bombs
  45. for (x=0; x<bombs.length; x++) {
  46.     for (y=0; y<bombs[x].length; y++) {
  47.         if (bombs[y][x]) {
  48.             level.attachMovie("bomb", "bomb"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
  49.         }
  50.     }
  51. }
  52. // placing bombuzal
  53. level.attachMovie("bombuzal", "bombuzal", level.getNextHighestDepth(), {_x:bombuzal_obj.xpos*tile_size, _y:bombuzal_obj.ypos*tile_size});
  54. level.bombuzal.onEnterFrame = function() {
  55.     // if bombuzal is not alread moving...
  56.     if (!bombuzal_obj.moving) {
  57.         // incrementing counter
  58.         does_not_move++;
  59.         // if 150 frames has passed and bombuzal did not move..
  60.         if (does_not_move == 150) {
  61.             bombuzal_obj.moving = true;
  62.             // choosing a random direction
  63.             moving_direction = Math.floor(Math.random()*4)+1;
  64.             switch (moving_direction) {
  65.             case 1 :
  66.                 bombuzal_obj.facing = "left";
  67.                 break;
  68.             case 2 :
  69.                 bombuzal_obj.facing = "right";
  70.                 break;
  71.             case 3 :
  72.                 bombuzal_obj.facing = "up";
  73.                 break;
  74.             case 4 :
  75.                 bombuzal_obj.facing = "down";
  76.                 break;
  77.             }
  78.         }
  79.         // moving left     
  80.         if (Key.isDown(Key.LEFT)) {
  81.             bombuzal_obj.moving = true;
  82.             bombuzal_obj.facing = "left";
  83.         }
  84.         // moving right                   
  85.         if (Key.isDown(Key.RIGHT)) {
  86.             bombuzal_obj.moving = true;
  87.             bombuzal_obj.facing = "right";
  88.         }
  89.         // moving down                   
  90.         if (Key.isDown(Key.DOWN)) {
  91.             bombuzal_obj.moving = true;
  92.             bombuzal_obj.facing = "down";
  93.         }
  94.         // moving up                   
  95.         if (Key.isDown(Key.UP)) {
  96.             bombuzal_obj.moving = true;
  97.             bombuzal_obj.facing = "up";
  98.         }
  99.         // else, if it's moving...       
  100.     } else {
  101.         does_not_move = 0;
  102.         switch (bombuzal_obj.facing) {
  103.             // moving left
  104.         case "left" :
  105.             dx = -1;
  106.             dy = 0;
  107.             this._x -= walk_speed;
  108.             break;
  109.             // moving right
  110.         case "right" :
  111.             dx = 1;
  112.             dy = 0;
  113.             this._x += walk_speed;
  114.             break;
  115.             // moving up
  116.         case "up" :
  117.             dx = 0;
  118.             dy = -1;
  119.             this._y -= walk_speed;
  120.             break;
  121.             // moving down
  122.         case "down" :
  123.             dx = 0;
  124.             dy = 1;
  125.             this._y += walk_speed;
  126.             break;
  127.         }
  128.         // if bombuzal walked for an entire tile...
  129.         if ((this._x%tile_size == 0) and (this._y%tile_size == 0)) {
  130.             // stop moving bombuzal
  131.             bombuzal_obj.moving = false;
  132.             // increase bombuzal x and y position according to its direction
  133.             bombuzal_obj.xpos += dx;
  134.             bombuzal_obj.ypos += dy;
  135.             update_bombuzal_position();
  136.         }
  137.     }
  138. };
  139. // centering level
  140. level._x = (Stage.width-tiles.length*tile_size)/2;
  141. level._y = (Stage.height-tiles[0].length*tile_size)/2;
  142. level.onEnterFrame = function() {
  143.     //if mouse is on stage
  144.     if (level_pan) {
  145.         //stopping level leaving screen
  146.         //too high
  147.         if (level._y<=0) {
  148.             level._y = 0;
  149.         }
  150.         //too low                                   
  151.         if (level._y>=Stage.height-level._height) {
  152.             level._y = Stage.height-level._height;
  153.         }
  154.         //too far to the left                                   
  155.         if (level._x<=0) {
  156.             level._x = 0;
  157.         }
  158.         //too far to the right                                   
  159.         if (level._x>=Stage.width-level._width) {
  160.             level._x = Stage.width-level._width;
  161.         }
  162.         // panning level                                   
  163.         // pan right
  164.         if (_root._xmouse<pan_dist) {
  165.             level._x += pan_speed;
  166.         }
  167.         // pan left                                   
  168.         if (_root._xmouse>Stage.width-pan_dist) {
  169.             level._x -= pan_speed;
  170.         }
  171.         // pan down                                   
  172.         if (_root._ymouse<pan_dist) {
  173.             level._y += pan_speed;
  174.         }
  175.         // pan up                                   
  176.         if (_root._ymouse>Stage.height-pan_dist) {
  177.             level._y -= pan_speed;
  178.         }
  179.     }
  180. };
  181. //when mouse is on stage pan
  182. base.onRollOver = function() {
  183.     level_pan = true;
  184. };
  185. //when mouse leaves stage stop panning
  186. base.onRollOut = function() {
  187.     level_pan = false;
  188. };
  189. // update bombuzal position
  190. function update_bombuzal_position() {
  191.     // if bombuzal falls off the stage...
  192.     if ((tiles[bombuzal_obj.ypos][bombuzal_obj.xpos] == undefined) or (tiles[bombuzal_obj.ypos][bombuzal_obj.xpos]<1)) {
  193.         // reset bombuzal to its starting position
  194.         bombuzal_obj.xpos = 0;
  195.         bombuzal_obj.ypos = 1;
  196.         level.bombuzal._x = bombuzal_obj.xpos*tile_size;
  197.         level.bombuzal._y = bombuzal_obj.ypos*tile_size;
  198.         update_bombuzal_position();
  199.     }
  200. }

Now I want "broken" tiles to disappear after Bombuzal passed over them. To do it, when Bombuzal stops after walking for an entire tile, I check if the tile it just left is a broken tile.

In this case, I remove the tile movieclip and update the tiles array...

ACTIONSCRIPT:
  1. //create an invisible movie clip across the entire stage using API
  2. _root.createEmptyMovieClip("base", 1);
  3. with (base) {
  4.     lineStyle(2, 0x0000000, 0);
  5.     beginFill(0x00000000, 0);
  6.     lineTo(Stage.width, 0);
  7.     lineTo(Stage.width, Stage.height);
  8.     lineTo(0, Stage.height);
  9.     lineTo(0, 0);
  10. }
  11. //whether to pan or not
  12. level_pan = false;
  13. //tile size
  14. tile_size = 50;
  15. // pan variables
  16. pan_dist = 50;
  17. pan_speed = 5;
  18. // tiles array generation
  19. tiles = [[0, 1, 0, 0], [1, 1, 2, 1], [1, 2, 1, 2], [0, 0, 1, 0]];
  20. // bombs array generation
  21. bombs = [[0, 1, 0, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 0, 1, 0]];
  22. // main sprite
  23. // xpos: bombuzal x position
  24. // ypos: y position
  25. // moving: bombuzal is moving or not
  26. // facing: the side bombuzal is facing on
  27. bombuzal_obj = {xpos:0, ypos:1, moving:false, facing:"right"};
  28. // bombuzal walk speed, in pixels/frame
  29. walk_speed = 5;
  30. // time passed since bombuzal last move
  31. does_not_move = 0;
  32. // creating the level movieclip
  33. _root.createEmptyMovieClip("level", _root.getNextHighestDepth());
  34. // placing tiles
  35. for (x=0; x<tiles.length; x++) {
  36.     for (y=0; y<tiles[x].length; y++) {
  37.         // notice as it's not [x][y] but [y][x]
  38.         if (tiles[y][x]) {
  39.             placed = level.attachMovie("tile", "tile_"+(x*tiles.length+y), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
  40.             placed.gotoAndStop(tiles[y][x]);
  41.         }
  42.     }
  43. }
  44. // placing bombs
  45. for (x=0; x<bombs.length; x++) {
  46.     for (y=0; y<bombs[x].length; y++) {
  47.         if (bombs[y][x]) {
  48.             level.attachMovie("bomb", "bomb"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
  49.         }
  50.     }
  51. }
  52. // placing bombuzal
  53. level.attachMovie("bombuzal", "bombuzal", level.getNextHighestDepth(), {_x:bombuzal_obj.xpos*tile_size, _y:bombuzal_obj.ypos*tile_size});
  54. level.bombuzal.onEnterFrame = function() {
  55.     // if bombuzal is not alread moving...
  56.     if (!bombuzal_obj.moving) {
  57.         // incrementing counter
  58.         does_not_move++;
  59.         // if 150 frames has passed and bombuzal did not move..
  60.         if (does_not_move == 150) {
  61.             bombuzal_obj.moving = true;
  62.             // choosing a random direction
  63.             moving_direction = Math.floor(Math.random()*4)+1;
  64.             switch (moving_direction) {
  65.             case 1 :
  66.                 bombuzal_obj.facing = "left";
  67.                 break;
  68.             case 2 :
  69.                 bombuzal_obj.facing = "right";
  70.                 break;
  71.             case 3 :
  72.                 bombuzal_obj.facing = "up";
  73.                 break;
  74.             case 4 :
  75.                 bombuzal_obj.facing = "down";
  76.                 break;
  77.             }
  78.         }
  79.         // moving left     
  80.         if (Key.isDown(Key.LEFT)) {
  81.             bombuzal_obj.moving = true;
  82.             bombuzal_obj.facing = "left";
  83.         }
  84.         // moving right                   
  85.         if (Key.isDown(Key.RIGHT)) {
  86.             bombuzal_obj.moving = true;
  87.             bombuzal_obj.facing = "right";
  88.         }
  89.         // moving down                   
  90.         if (Key.isDown(Key.DOWN)) {
  91.             bombuzal_obj.moving = true;
  92.             bombuzal_obj.facing = "down";
  93.         }
  94.         // moving up                   
  95.         if (Key.isDown(Key.UP)) {
  96.             bombuzal_obj.moving = true;
  97.             bombuzal_obj.facing = "up";
  98.         }
  99.         // else, if it's moving...       
  100.     } else {
  101.         does_not_move = 0;
  102.         switch (bombuzal_obj.facing) {
  103.             // moving left
  104.         case "left" :
  105.             dx = -1;
  106.             dy = 0;
  107.             this._x -= walk_speed;
  108.             break;
  109.             // moving right
  110.         case "right" :
  111.             dx = 1;
  112.             dy = 0;
  113.             this._x += walk_speed;
  114.             break;
  115.             // moving up
  116.         case "up" :
  117.             dx = 0;
  118.             dy = -1;
  119.             this._y -= walk_speed;
  120.             break;
  121.             // moving down
  122.         case "down" :
  123.             dx = 0;