From zero to Bombuzal - step 3
In the previous step we had the problem of the level panning away if the mouse is outside the movie.
The first solution come from souled that created a button in the entire stage to manage the panning
-
//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]];
-
// 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++) {
-
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});
-
}
-
}
-
}
-
// 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;
-
};
Maybe it's not the best solution ever, because it may require you to redesign mouse pointer, but at the moment it works.
Now it's time to add the main sprite, Bombuzal itself! I'll create an object to store all its information. If you have troubles with objects, refer to Understanding Flash Objects tutorial.
At the moment the only information about Bombuzal I need are its x and y position
-
//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
-
bombuzal_obj = {xpos:0, ypos:1};
-
// 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++) {
-
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});
-
// 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;
-
};
And here it is!
I know, Bombuzal sprite sucks, but I'll fix it later. Now, I want it to move using arrow keys. If Bombuzal steps off the floor, then he dies and at the moment I want it to be redrawn at its starting position.
-
//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
-
bombuzal_obj = {xpos:0, ypos:1};
-
// 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++) {
-
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() {
-
// moving left
-
if (Key.isDown(Key.LEFT)) {
-
bombuzal_obj.xpos--;
-
}
-
// moving right
-
if (Key.isDown(Key.RIGHT)) {
-
bombuzal_obj.xpos++;
-
}
-
// moving down
-
if (Key.isDown(Key.DOWN)) {
-
bombuzal_obj.ypos++;
-
}
-
// moving up
-
if (Key.isDown(Key.UP)) {
-
bombuzal_obj.ypos--;
-
}
-
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() {
-
level.bombuzal._x = bombuzal_obj.xpos*tile_size;
-
level.bombuzal._y = bombuzal_obj.ypos*tile_size;
-
if ((tiles[bombuzal_obj.xpos][bombuzal_obj.ypos] == undefined) or (tiles[bombuzal_obj.xpos][bombuzal_obj.ypos]<1)) {
-
bombuzal_obj.xpos