To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 10-22-2004, 07:50 PM   #1
John M O'Shea
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.
John M O'Shea is offline   Reply With Quote
Old 10-22-2004, 08:00 PM   #2
jbum
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 08:05 PM.
jbum is offline   Reply With Quote
Old 10-22-2004, 08:16 PM   #3
John M O'Shea
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
John M O'Shea is offline   Reply With Quote
Old 10-22-2004, 08:39 PM   #4
jbum
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
__________________
jbum is offline   Reply With Quote
Old 10-22-2004, 08:50 PM   #5
jbum
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
File Type: fla maze_example.fla (290.0 KB, 2172 views)
__________________

Last edited by jbum; 10-22-2004 at 08:52 PM.
jbum is offline   Reply With Quote
Old 10-22-2004, 09:27 PM   #6
John M O'Shea
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 01:01 AM.
John M O'Shea is offline   Reply With Quote
Old 10-23-2004, 03:22 AM   #7
jbum
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...
__________________
jbum is offline   Reply With Quote
Old 01-05-2007, 07:30 PM   #8
robonflashkit
Junior Member
 
Join Date: Apr 2006
Posts: 7
How would you do the same but using arrowkeys?
robonflashkit is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:42 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.