A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: I need some actionscript maze help

  1. #1
    Member
    Join Date
    Oct 2004
    Posts
    75

    I need some actionscript maze help

    I've been all over the internet looking for a way to get my mazes that I can make into a flash game where you run a ball through it with either the mouse or the arrow keys. I've made simple square mazes that work, nothing to hard, but I'm more interested in trying to create a maze that looks a little like this:

    this maze

    I've been trying myself but I'm no actionscript expert at all. Anyone think they might be able to help me?

    P.S. I can redraw this in flash to make it work much better I just drew this on some paper and scanned it.

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Well, there's two techniques you can use here.

    The technique I use can be exhibited here:

    http://krazydad.com/fungames.php

    and here:

    http://krazydad.com/bestiary/

    I use a recursive algorithm (called "hunt and kill") which builds the maze using random numbers. Although the different mazes on my site look different, they are all built with the same algorithm (I use the same #include file for each of my mazes).

    Using this system, each time the script is run, you get a different maze. Although the mazes on display on my site tend to be fairly geometrical in look, it is possible to use curvy lines and more irregular shapes (my 'adjustable maze' uses some randomness in the line drawing) - the only requirement is that the space that the maze is in consist of a network of nodes, in which each node is connected to the surrounding nodes by a single path, such as a wall (as in a piece of graph paper or a honeycomb). The maze is made by removing the walls that connect nodes.

    When an object moves thru the maze, the datastructure already knows where the paths are, so you don't have to do any collision tests - you just only allow movement along legal paths.

    However, this method requires a lot of programming. A good website that describes this type of maze-creation algorithm is here:

    http://www.astrolog.org/labyrnth/algrithm.htm


    The second method is to use a scanned hand-drawn maze (or draw it in flash) and then use hitTest() to do collision checks when an object travels thru the maze. The good thing about this method is that it is easier to get a more organic looking maze, however there are drawbacks. The maze is always the same, and it is difficult to get the hitTest() based code to work properly - if an object moves too quickly it may jump over a wall.
    Last edited by jbum; 10-22-2004 at 07:05 PM.

  3. #3
    Member
    Join Date
    Oct 2004
    Posts
    75
    how would I go about doin the last option? Would I just have to scan the maze and import it into flash as a symbol? How would the hittest() code work for a symbol in that case?

    edit: Also I'm not really looking for a randomly created maze or anything like that, just the ones I make

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Import the graphics onto the stage, and use trace-bitmap to convert it into flash graphics. Select the white area and delete it, leaving only the black lines. Select the whole thing and convert it to a symbol and call it maze_mc.

    maze_mc.hitTest(x,y,true)

    will return true if the x,y coordinates are touching one of your walls.

    If I have time, I'll post an example using your original image.

    - Jim

  5. #5
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Attached is an example maze using your illustration.

    There is a little dot movieclip that moves using the hitTest system, and this is the script:

    code:

    speed = 2;

    dot_mc.onEnterFrame = function()
    {
    // determine angle in direction of mouse
    var a = Math.atan2(_root._ymouse - this._y, _root._xmouse - this._x);

    // determine new position, using that angle
    var dx = this._x + Math.cos(a)*speed;
    var dy = this._y + Math.sin(a)*speed;

    // test if that position collides with a wall, if not, move there

    if (! maze_mc.hitTest(dx,dy,true) && // checks center of dot
    ! maze_mc.hitTest(dx-this._width/2,dy-this._height/2,true) && // checks top/left edge of dot
    ! maze_mc.hitTest(dx+this._width/2,dy+this._height/2,true) ) // checks lower/right edge of dot
    {
    // no collision - move there
    this._x = dx;
    this._y = dy;
    }
    }



    If you try this example out, you'll discover one of the potential drawbacks of using a hitTest() based movement system. You'll notice that there are some bottlenecks that the dot can't move thru. That problem can be reduced by only checking against the center of the dot (I'm currently checking the center and the upper/left edge and lower/right edge of the dot) however, then you'll have a new problem, which is that the dot will easily jump skinny lines, especially if you increase it's speed, which you'll probably want to do - it moves too slowly right now.

    So you have this tradeoff between speed and accuracy...

    If you tweak it enough, and redo your drawing a bit to use good solid lines with wide corridors, you should be able to make it useable, though.
    Attached Files Attached Files
    Last edited by jbum; 10-22-2004 at 07:52 PM.

  6. #6
    Member
    Join Date
    Oct 2004
    Posts
    75
    holy moly mother of god thank you so much! This is by far the most help I've ever gotten from any forum for anything! Works great i'll try tweaking the code a little bit and making a game from this.

    I'm gonna put in a timer to see how long it takes to get through the maze and the time you did it in and minus the times and thats the points you got. This maze took me approxometly 30 seconds to make so I'll be more careful and make'm a little better and make a whole bunch. If I get done soon I'll put on the final product so you guys can see thanks!!!!!

    edit:

    this actionscript:

    speed = 4;
    dot_mc.onEnterFrame = function() {
    var a = Math.atan2(_root._ymouse-this._y, _root._xmouse-this._x);
    var dx = this._x+Math.cos(a)*speed;
    var dy = this._y+Math.sin(a)*speed;
    if (!maze_mc.hitTest(dx, dy, true) && !maze_mc.hitTest(dx-this._width/2, dy-this._height/2, true) && !maze_mc.hitTest(dx+this._width/2, dy+this._height/2, true)) {
    this._x = dx;
    this._y = dy;
    }
    };

    dot.mc is slow when below 5 for a speed and above that its really laggy when it is higher. Can this be fixed or tweaked in anyway?
    Last edited by John M O'Shea; 10-23-2004 at 12:01 AM.

  7. #7
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    I assume by laggy, you are referring to the large jumps it makes.

    I would suggest increasing the framerate of your movie to 30 fps or so... This will allow you to get more speed out of smaller increments.

    I was able to avoid this problem in my own mazes by using a different kind of movement system, but it requires that the script have more knowledge of the maze topology...

  8. #8
    Junior Member
    Join Date
    Apr 2006
    Posts
    7
    How would you do the same but using arrowkeys?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center