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
-
//create an invisible movie clip across the entire stage using API
-
_root.createEmptyMovieClip("base", 1);
-
with (base) {
-
lineStyle(2, 0x0000000, 0);
-
beginFill(0x00000000, 0);
-
lineTo(Stage.width, 0);
-
lineTo(Stage.width, Stage.height);
-
lineTo(0, Stage.height);
-
lineTo(0, 0);
-
}
-
//whether to pan or not
-
level_pan = false;
-
//tile size
-
tile_size = 50;
-
// pan variables
-
pan_dist = 50;
-
pan_speed = 5;
-
// tiles array generation
-
tiles = [[0, 1, 0, 0], [1, 1, 2, 1], [1, 2, 1, 2], [0, 0, 1, 0]];
-
// bombs array generation
-
bombs = [[0, 1, 0, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 0, 1, 0]];
-
// main sprite
-
// xpos: bombuzal x position
-
// ypos: y position
-
// moving: bombuzal is moving or not
-
// facing: the side bombuzal is facing on
-
bombuzal_obj = {xpos:0, ypos:1, moving:false, facing:"right"};
-
// bombuzal walk speed, in pixels/frame
-
walk_speed = 5;
-
// time passed since bombuzal last move
-
does_not_move = 0;
-
// creating the level movieclip
-
_root.createEmptyMovieClip("level", _root.getNextHighestDepth());
-
// placing tiles
-
for (x=0; x<tiles.length; x++) {
-
for (y=0; y<tiles[x].length; y++) {
-
// notice as it's not [x][y] but [y][x]
-
if (tiles[y][x]) {
-
placed = level.attachMovie("tile", "tile"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
-
placed.gotoAndStop(tiles[y][x]);
-
}
-
}
-
}
-
// placing bombs
-
for (x=0; x<bombs.length; x++) {
-
for (y=0; y<bombs[x].length; y++) {
-
if (bombs[y][x]) {
-
level.attachMovie("bomb", "bomb"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
-
}
-
}
-
}
-
// placing bombuzal
-
level.attachMovie("bombuzal", "bombuzal", level.getNextHighestDepth(), {_x:bombuzal_obj.xpos*tile_size, _y:bombuzal_obj.ypos*tile_size});
-
level.bombuzal.onEnterFrame = function() {
-
// if bombuzal is not alread moving...
-
if (!bombuzal_obj.moving) {
-
// incrementing counter
-
does_not_move++;
-
// if 150 frames has passed and bombuzal did not move..
-
if (does_not_move == 150) {
-
bombuzal_obj.moving = true;
-
// choosing a random direction
-
moving_direction = Math.floor(Math.random()*4)+1;
-
switch (moving_direction) {
-
case 1 :
-
bombuzal_obj.facing = "left";
-
break;
-
case 2 :
-
bombuzal_obj.facing = "right";
-
break;
-
case 3 :
-
bombuzal_obj.facing = "up";
-
break;
-
case 4 :
-
bombuzal_obj.facing = "down";
-
break;
-
}
-
}
-
// moving left
-
if (Key.isDown(Key.LEFT)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "left";
-
}
-
// moving right
-
if (Key.isDown(Key.RIGHT)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "right";
-
}
-
// moving down
-
if (Key.isDown(Key.DOWN)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "down";
-
}
-
// moving up
-
if (Key.isDown(Key.UP)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "up";
-
}
-
// else, if it's moving...
-
} else {
-
does_not_move = 0;
-
switch (bombuzal_obj.facing) {
-
// moving left
-
case "left" :
-
dx = -1;
-
dy = 0;
-
this._x -= walk_speed;
-
break;
-
// moving right
-
case "right" :
-
dx = 1;
-
dy = 0;
-
this._x += walk_speed;
-
break;
-
// moving up
-
case "up" :
-
dx = 0;
-
dy = -1;
-
this._y -= walk_speed;
-
break;
-
// moving down
-
case "down" :
-
dx = 0;
-
dy = 1;
-
this._y += walk_speed;
-
break;
-
}
-
// if bombuzal walked for an entire tile...
-
if ((this._x%tile_size == 0) and (this._y%tile_size == 0)) {
-
// stop moving bombuzal
-
bombuzal_obj.moving = false;
-
// increase bombuzal x and y position according to its direction
-
bombuzal_obj.xpos += dx;
-
bombuzal_obj.ypos += dy;
-
update_bombuzal_position();
-
}
-
}
-
};
-
// centering level
-
level._x = (Stage.width-tiles.length*tile_size)/2;
-
level._y = (Stage.height-tiles[0].length*tile_size)/2;
-
level.onEnterFrame = function() {
-
//if mouse is on stage
-
if (level_pan) {
-
//stopping level leaving screen
-
//too high
-
if (level._y<=0) {
-
level._y = 0;
-
}
-
//too low
-
if (level._y>=Stage.height-level._height) {
-
level._y = Stage.height-level._height;
-
}
-
//too far to the left
-
if (level._x<=0) {
-
level._x = 0;
-
}
-
//too far to the right
-
if (level._x>=Stage.width-level._width) {
-
level._x = Stage.width-level._width;
-
}
-
// panning level
-
// pan right
-
if (_root._xmouse<pan_dist) {
-
level._x += pan_speed;
-
}
-
// pan left
-
if (_root._xmouse>Stage.width-pan_dist) {
-
level._x -= pan_speed;
-
}
-
// pan down
-
if (_root._ymouse<pan_dist) {
-
level._y += pan_speed;
-
}
-
// pan up
-
if (_root._ymouse>Stage.height-pan_dist) {
-
level._y -= pan_speed;
-
}
-
}
-
};
-
//when mouse is on stage pan
-
base.onRollOver = function() {
-
level_pan = true;
-
};
-
//when mouse leaves stage stop panning
-
base.onRollOut = function() {
-
level_pan = false;
-
};
-
// update bombuzal position
-
function update_bombuzal_position() {
-
// if bombuzal falls off the stage...
-
if ((tiles[bombuzal_obj.ypos][bombuzal_obj.xpos] == undefined) or (tiles[bombuzal_obj.ypos][bombuzal_obj.xpos]<1)) {
-
// reset bombuzal to its starting position
-
bombuzal_obj.xpos = 0;
-
bombuzal_obj.ypos = 1;
-
level.bombuzal._x = bombuzal_obj.xpos*tile_size;
-
level.bombuzal._y = bombuzal_obj.ypos*tile_size;
-
update_bombuzal_position();
-
}
-
}
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...
-
//create an invisible movie clip across the entire stage using API
-
_root.createEmptyMovieClip("base", 1);
-
with (base) {
-
lineStyle(2, 0x0000000, 0);
-
beginFill(0x00000000, 0);
-
lineTo(Stage.width, 0);
-
lineTo(Stage.width, Stage.height);
-
lineTo(0, Stage.height);
-
lineTo(0, 0);
-
}
-
//whether to pan or not
-
level_pan = false;
-
//tile size
-
tile_size = 50;
-
// pan variables
-
pan_dist = 50;
-
pan_speed = 5;
-
// tiles array generation
-
tiles = [[0, 1, 0, 0], [1, 1, 2, 1], [1, 2, 1, 2], [0, 0, 1, 0]];
-
// bombs array generation
-
bombs = [[0, 1, 0, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 0, 1, 0]];
-
// main sprite
-
// xpos: bombuzal x position
-
// ypos: y position
-
// moving: bombuzal is moving or not
-
// facing: the side bombuzal is facing on
-
bombuzal_obj = {xpos:0, ypos:1, moving:false, facing:"right"};
-
// bombuzal walk speed, in pixels/frame
-
walk_speed = 5;
-
// time passed since bombuzal last move
-
does_not_move = 0;
-
// creating the level movieclip
-
_root.createEmptyMovieClip("level", _root.getNextHighestDepth());
-
// placing tiles
-
for (x=0; x<tiles.length; x++) {
-
for (y=0; y<tiles[x].length; y++) {
-
// notice as it's not [x][y] but [y][x]
-
if (tiles[y][x]) {
-
placed = level.attachMovie("tile", "tile_"+(x*tiles.length+y), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
-
placed.gotoAndStop(tiles[y][x]);
-
}
-
}
-
}
-
// placing bombs
-
for (x=0; x<bombs.length; x++) {
-
for (y=0; y<bombs[x].length; y++) {
-
if (bombs[y][x]) {
-
level.attachMovie("bomb", "bomb"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
-
}
-
}
-
}
-
// placing bombuzal
-
level.attachMovie("bombuzal", "bombuzal", level.getNextHighestDepth(), {_x:bombuzal_obj.xpos*tile_size, _y:bombuzal_obj.ypos*tile_size});
-
level.bombuzal.onEnterFrame = function() {
-
// if bombuzal is not alread moving...
-
if (!bombuzal_obj.moving) {
-
// incrementing counter
-
does_not_move++;
-
// if 150 frames has passed and bombuzal did not move..
-
if (does_not_move == 150) {
-
bombuzal_obj.moving = true;
-
// choosing a random direction
-
moving_direction = Math.floor(Math.random()*4)+1;
-
switch (moving_direction) {
-
case 1 :
-
bombuzal_obj.facing = "left";
-
break;
-
case 2 :
-
bombuzal_obj.facing = "right";
-
break;
-
case 3 :
-
bombuzal_obj.facing = "up";
-
break;
-
case 4 :
-
bombuzal_obj.facing = "down";
-
break;
-
}
-
}
-
// moving left
-
if (Key.isDown(Key.LEFT)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "left";
-
}
-
// moving right
-
if (Key.isDown(Key.RIGHT)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "right";
-
}
-
// moving down
-
if (Key.isDown(Key.DOWN)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "down";
-
}
-
// moving up
-
if (Key.isDown(Key.UP)) {
-
bombuzal_obj.moving = true;
-
bombuzal_obj.facing = "up";
-
}
-
// else, if it's moving...
-
} else {
-
does_not_move = 0;
-
switch (bombuzal_obj.facing) {
-
// moving left
-
case "left" :
-
dx = -1;
-
dy = 0;
-
this._x -= walk_speed;
-
break;
-
// moving right
-
case "right" :
-
dx = 1;
-
dy = 0;
-
this._x += walk_speed;
-
break;
-
// moving up
-
case "up" :
-
dx = 0;
-
dy = -1;
-
this._y -= walk_speed;
-
break;
-
// moving down
-
case "down" :
-
dx = 0;