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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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 4×4 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//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!

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 18 comments

  1. Keiran

    on February 23, 2008 at 4:47 am

    Thanks alot man…oh and FIRST POST

  2. Mariusz

    on February 23, 2008 at 9:18 am

    Hi,
    i can’t see any content in your movieclips :-/

  3. Emanuele Feronato

    on February 23, 2008 at 11:12 am

    Thank you mariusz… fixed

  4. Mariusz

    on February 23, 2008 at 11:31 am

    Looks great so far – thx for sharing your knowledge ;-)

  5. styxtwo

    on February 23, 2008 at 2:19 pm

    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.

  6. Emanuele Feronato

    on February 23, 2008 at 2:42 pm

    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

  7. styxtwo

    on February 23, 2008 at 5:30 pm

    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…

  8. styxtwo

    on February 23, 2008 at 5:33 pm

    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.

  9. styxtwo

    on February 23, 2008 at 6:27 pm

    an _x bigger then *

  10. souled

    on February 23, 2008 at 11:57 pm

    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. :/

  11. souled

    on February 24, 2008 at 12:01 am

    Oh, and here’s an example:

    http://www.ngup.net/view?file=668

    Hope that’s what you need! :)

  12. styxtwo

    on February 24, 2008 at 2:00 am

    cool souled how did you do it ?

  13. gas

    on February 24, 2008 at 5:29 am

    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… ;) :)

  14. souled

    on February 24, 2008 at 1:29 pm

    Logic. :)

  15. Rick

    on February 24, 2008 at 2:25 pm

    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)

  16. Xodus

    on February 24, 2008 at 3:23 pm

    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.

  17. From zero to Bombuzal - step 3 : Emanuele Feronato - italian geek and PROgrammer

    on February 26, 2008 at 5:40 pm

    [...] the previous step we had the problem of the level panning away if the mouse is outside the [...]

  18. Andre Marianiello

    on February 26, 2008 at 10:35 pm

    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
    }
    };
    }