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:

ACTIONSCRIPT:
  1. // tiles array generation
  2. tiles = [[0, 1, 0, 0], [1, 1, 2, 1], [1, 2, 1, 2], [0, 0, 1, 0]];
  3. // bombs array generation
  4. bombs = [[0, 1, 0, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 0, 1, 0]];
  5. // placing tiles
  6. for (x=0; x<tiles.length; x++) {
  7.     for (y=0; y<tiles[x].length; y++) {
  8.         if (tiles[y][x]) {
  9.             placed = _root.attachMovie("tile","tile"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:x*50, _y:y*50});
  10.             placed.gotoAndStop(tiles[y][x]);
  11.         }
  12.     }
  13. }
  14. // placing bombs
  15. for (x=0; x<bombs.length; x++) {
  16.     for (y=0; y<bombs[x].length; y++) {
  17.         if (bombs[y][x]) {
  18.             _root.attachMovie("bomb","bomb"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:x*50, _y:y*50});
  19.         }
  20.     }
  21. }

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.

ACTIONSCRIPT:
  1. // tiles array generation
  2. tiles = [[0, 1, 0, 0], [1, 1, 2, 1], [1, 2, 1, 2], [0, 0, 1, 0]];
  3. // bombs array generation
  4. bombs = [[0, 1, 0, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 0, 1, 0]];
  5. // creating the level movieclip
  6. _root.createEmptyMovieClip("level",_root.getNextHighestDepth());
  7. // placing tiles
  8. for (x=0; x<tiles.length; x++) {
  9.     for (y=0; y<tiles[x].length; y++) {
  10.         if (tiles[y][x]) {
  11.             placed = level.attachMovie("tile", "tile"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*50, _y:y*50});
  12.             placed.gotoAndStop(tiles[y][x]);
  13.         }
  14.     }
  15. }
  16. // placing bombs
  17. for (x=0; x<bombs.length; x++) {
  18.     for (y=0; y<bombs[x].length; y++) {
  19.         if (bombs[y][x]) {
  20.             level.attachMovie("bomb","bomb"+level.getNextHighestDepth(),level.getNextHighestDepth(),{_x:x*50, _y:y*50});
  21.         }
  22.     }
  23. }
  24. // centering level
  25. level._x = (Stage.width-tiles.length*50)/2;
  26. 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.

ACTIONSCRIPT:
  1. //tile size
  2. tile_size = 50;
  3. // pan variables
  4. pan_dist = 50;
  5. pan_speed = 5;
  6. // tiles array generation
  7. tiles = [[0, 1, 0, 0], [1, 1, 2, 1], [1, 2, 1, 2], [0, 0, 1, 0]];
  8. // bombs array generation
  9. bombs = [[0, 1, 0, 0], [0, 1, 0, 1], [1, 0, 1, 0], [0, 0, 1, 0]];
  10. // creating the level movieclip
  11. _root.createEmptyMovieClip("level",_root.getNextHighestDepth());
  12. // placing tiles
  13. for (x=0; x<tiles.length; x++) {
  14.     for (y=0; y<tiles[x].length; y++) {
  15.         if (tiles[y][x]) {
  16.             placed = level.attachMovie("tile", "tile"+level.getNextHighestDepth(), level.getNextHighestDepth(), {_x:x*tile_size, _y:y*tile_size});
  17.             placed.gotoAndStop(tiles[y][x]);
  18.         }
  19.     }
  20. }
  21. // placing bombs
  22. for (x=0; x<bombs.length; x++) {
  23.     for (y=0; y<bombs[x].length; y++) {
  24.         if (bombs[y][x]) {
  25.             level.attachMovie("bomb","bomb"+level.getNextHighestDepth(),level.getNextHighestDepth(),{_x:x*tile_size, _y:y*tile_size});
  26.         }
  27.     }
  28. }
  29. // centering level
  30. level._x = (Stage.width-tiles.length*tile_size)/2;
  31. level._y = (Stage.height-tiles[0].length*tile_size)/2;
  32. // panning level
  33. level.onEnterFrame = function() {
  34.     // pan right
  35.     if (_root._xmouse<pan_dist) {
  36.         level._x += pan_speed;
  37.     }
  38.     // pan left 
  39.     if (_root._xmouse>Stage.width-pan_dist) {
  40.         level._x -= pan_speed;
  41.     }
  42.     // pan down 
  43.     if (_root._ymouse<pan_dist) {
  44.         level._y += pan_speed;
  45.     }
  46.     // pan up 
  47.     if (_root._ymouse>Stage.height-pan_dist) {
  48.         level._y -= pan_speed;
  49.     }
  50. };

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!

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
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.

18 Responses to “From zero to Bombuzal - step 2”

  1. Keiran on February 23rd, 2008 4:47 am

    Thanks alot man…oh and FIRST POST

  2. Mariusz on February 23rd, 2008 9:18 am

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

  3. Emanuele Feronato on February 23rd, 2008 11:12 am

    Thank you mariusz… fixed

  4. Mariusz on February 23rd, 2008 11:31 am

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

  5. styxtwo on February 23rd, 2008 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 23rd, 2008 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 23rd, 2008 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 23rd, 2008 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 23rd, 2008 6:27 pm

    an _x bigger then *

  10. souled on February 23rd, 2008 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 24th, 2008 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 24th, 2008 2:00 am

    cool souled how did you do it ?

  13. gas on February 24th, 2008 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 24th, 2008 1:29 pm

    Logic. :)

  15. Rick on February 24th, 2008 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 24th, 2008 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. Andre Marianiello on February 26th, 2008 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
    }
    };
    }

Leave a Reply




Trackbacks

  1. 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 [...]