From zero to Bombuzal - step 2
It's time to create the field of the first level.
Creating the level
Merging the ideas of Andre Marianiello and Chris, I was able to create the 2d level in this way:
-
// 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]];
-
// placing tiles
-
for (x=0; x<tiles.length; x++) {
-
for (y=0; y<tiles[x].length; y++) {
-
if (tiles[y][x]) {
-
placed = _root.attachMovie("tile","tile"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:x*50, _y:y*50});
-
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]) {
-
_root.attachMovie("bomb","bomb"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:x*50, _y:y*50});
-
}
-
}
-
}
and this is the result:
Aligning the level to the stage
"Everything" is working, but I don't want the level to start at the upper left corner... I want the level to be centered in the stage.
So I have to calculate level's height and width and move the movieclip in the center of the stage.
-
// 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*50, _y:y*50});
-
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*50, _y:y*50});
-
}
-
}
-
}
-
// centering level
-
level._x = (Stage.width-tiles.length*50)/2;
-
level._y = (Stage.height-tiles[0].length*50)/2;
Now the level is aligned to the stage
Panning the level
I think advanced levels will be greater than this 4x4 array, so I have to allow the player to pan the level with mouse. I want the level to pan when the mouse gets closer to stage edges.
-
//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;
-
// panning level
-
level.onEnterFrame = function() {
-
// 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;
-
}
-
};
And this is the first error... panning in this way means your level will pan even when your mouse is outside the stage. Probably you will need to reload the page to see the level in this movieclip
Also, I don't want the movieclip to leave the stage if it entirely fits in it.
That's where you can help me and have your name in the credits: make the level pan only when the mouse is inside the stage and don't let the level leave the stage if it entirely fits in it.
I would like you all to read AS3 Bombuzal tile test post in the forum by LivesInABox where you can find a very good AS3 script to generate both 2D and isometric levels.
It would be interesting to create Bombuzal both in AS2 and in AS3.
The winner of this post is LivesInABox for his wonderful AS3 work.
I remember that winners will have their names in the credits of this remake, and should the game be sponsored (but I think it will be quite hard because it's an open source game) I will share the income with winners.
Download the source code and fix that scrolling!
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
18 Responses to “From zero to Bombuzal - step 2”
Leave a Reply
Trackbacks
-
From zero to Bombuzal - step 3 : Emanuele Feronato - italian geek and PROgrammer on
February 26th, 2008 5:40 pm
[...] the previous step we had the problem of the level panning away if the mouse is outside the [...]

Thanks alot man…oh and FIRST POST
Hi,
i can’t see any content in your movieclips :-/
Thank you mariusz… fixed
Looks great so far - thx for sharing your knowledge ;-)
use something like :
if the mouse_x is between 0 and 550 and the mouse_y is between 0 and 400 only then pan the screen.
this won’t work… when the mouse leaves the stage, if its last position was (499,123), _xmouse and _ymouse will keep their values and the screen will continue panning
i beleave there was a way to check wether the game had “keyboard control”. so you could only pan it when the game has that control…
or else, keep the panning, but only pan it as long as one of the tiles is inside the screen. so if all the tiles have an _x then 550 then it stops panning.
an _x bigger then *
I don’t think I need to send you a new .fla, just replace the code with what I’ve done. I put it up here:
http://souled.newgrounds.com/news/post/85482
I’ve fixed the panning and the other problem, however it does now have a button across the entire stage, so it would look better with a custom mouse. :/
Oh, and here’s an example:
http://www.ngup.net/view?file=668
Hope that’s what you need! :)
cool souled how did you do it ?
Using “tollerance”?
[code]
// panning level
level.onEnterFrame = function() {
tolerance = 10;
dontmove = false;
if ((_root._xmouse>Stage.width-tolerance) or (_root._ymouse>Stage.height-tolerance) or (_root._xmouse<tolerance) or (_root._ymouse<tolerance)) {
dontmove = true;
}
if (!dontmove) {
// pan right
if (_root._xmouseStage.width-pan_dist) {
level._x -= pan_speed;
}
// pan down
if (_root._ymouseStage.height-pan_dist) {
level._y -= pan_speed;
}
}
};
[/code]
I think it’s better than having an overall button that can cause problem with other buttons… ;) :)
Logic. :)
would it be easier to name the tile and bomb movies with x and y in the movie’s name ex: “tile_”+y+”_”+x, and “bomb_”+y+”_”+x, instead of using “tile”+level.getNextHighestDepth(), ?
(someone else made this suggestion to me and I found it usefull)
well, why do you have to make the mouse pan all the time? why don’t you only make the mouse pan when the space button is pressed? so if you want to move your mouse off the stage, you wouldn’t hold down space, and no one would pan unless they needed to, so it solves the 2nd problem too.
buffer = 25;//max distance the edge of the level can get away from edge of the stage
if(level._width>Stage.width and level._height>Stage.height){
level.onEnterFrame = function() {
// pan right
if (_root._xmouseStage.width-pan_dist) {
level._x -= pan_speed;
}
// pan down
if (_root._ymouseStage.height-pan_dist) {
level._y -= pan_speed;
}
if(level._x>Stage.width-buffer-level._width/2){
level._x=Stage.width-buffer-level._width/2
}
if(level._xStage.height-buffer-level._height/2){
level._y=Stage.height-buffer-level._height/2
}
if(level._y<Stage.height+buffer+level._height/2){
level._y=Stage.height+buffer+level._height/2
}
};
}