## As: Maze

• 4,672 Views
• 11 Replies
Begoner
Begoner
• Member since: Oct. 10, 2004
• Offline.
Forum Stats
Member
Level 10
Blank Slate
As: Maze 2005-07-05 17:41:40

AS: Main

This is a tutorial on how to make a simple maze game that demonstrates the usefulness of arrays.

The Basic Structure of the Maze

The maze will consist of many individual "cells." Each cell is like a block in the maze, and each cell has four walls (north, east, south, and west). In the making of the maze, some walls will be knocked down to form a path, so each of the walls must be its own seperate movie clip, contained within the cell movie clip. Also, each wall must consist of two frames, for the two possible states -- standing up and knocked down.

Now, to actually make a cell. Hit Ctrl + F8, give the movie clip the name "cell," and check the box that says "export for ActionScript." Next, draw a 25 * 25 square, and give it an x-coordinate of 0 and a y-coordinate of 0. Then, make its sides into movie clips (top side should be called "north", right side should be called "east", bottom side should be called "south", and left side should be called "west"). Make sure you not only give them names, but instance names. Each of those movie clips should have two frames. On the first frame, write:

stop();

Then, insert a blank keyframe for the second frame, and write the same thing for it. Do this for each of the four walls. Once you're done with that, go to the main timeline. Now we'll need a function that makes a grid of these cells.

The Basic Structure of the First Function

Basically, we want the function to cycle through each of the rows in the maze, and then each of the spaces with those rows. For each of those spaces, we want a cell to be added. To differentiate between the cells, we'll call them "cell0", "cell1", "cell2", etc. Also, we'll make an array. Each item in the array will represent a seperate row in the grid. In addition, we'll have a variable called "carved," which will be false only if all four walls of a cell are standing.

Pseudo-Code for the First Function

Create Array Representing Grid
Set Variable that Contains Current Cell Number
For each of the rows:

Create Array Representing Row

For each of the Cells Within the Row:

Attach Cell to Grid
Set Carved for Current Cell to False
Append Cell to Array
Increment Current Cell Number (+1)

End for

Append Array Representing Row to Array Representing Grid

End for

Now for the real code:

Actual Code

function createGrid()
{
currentCell = 0;
arrayOfCells = [];
for (i = 0; i < 16; i++)
{
thisRow = [];
for (j = 0; j < 22; j++)
{
newCell = attachMovie("cell", "cell" + currentCell, this.getNextHighestDepth());
newCell._x = j * 25;
newCell._y = i * 25;
newCell.carved = false;
currentCell++;
thisRow.push(newCell);
}
arrayOfCells.push(thisRow);
}
}

Place this code in the actions for the main timeline.

The Basic Structure of the Second Function

Now for building the actual maze. The algorithm is quite simple. At each cell, you can go to any neighboring cell that is uncarved (all its walls are standing up). You can pick a random one. If you have nowhere to go, you go back to the last place where you had two or more choices of where to go (you go down one path, you run into a dead end, then you go down the other). When you don't have any more places where you had two choices and you're stuck, the maze is done.

Pseudo-Code for the Second Function

Current Row = Last Row
Current Cell = Last Cell in Row
Create Array for Places with 2+ Choices
Until the Maze is Done, Do:

Set Current Cell to "carved"
Set Array of Choices to []
Add All Neighboring Cells that are not Carved to the Array
If you have no Choices:

If the Array of Places with 2+ Choices is Empty:

the Maze is Done

Otherwise:

Go to the Last Item in the Array of Places With 2+ Choices
Delete that Last Item of that Array

Pick Random Item from Array
Current Row = new Current Row
Current Cell = new Current Cell

Actual Code

function carveMaze()
{
row = 15;
item = 21;
stack = [];
while (true)
{
arrayOfCells[row][item].carved = true;
choices = [];
if (arrayOfCells[row][item + 1].carved == false)
{
choices.push("EAST");
}
if (arrayOfCells[row][item - 1].carved == false)
{
choices.push("WEST");
}
if (arrayOfCells[row + 1][item].carved == false)
{
choices.push("SOUTH");
}
if (arrayOfCells[row - 1][item].carved == false)
{
choices.push("NORTH");
}
if (choices.length == 0)
{
if (stack.length == 0)
{
break;
}
row = stack[stack.length - 1][0];
item = stack[stack.length - 1][1];
stack.pop();
}
else
{
if (choices.length >= 2)
{
stack.push([row, item]);
}
choice = choices[random(choices.length)];
if (choice == "NORTH")
{
arrayOfCells[row][item].north.gotoAndStop(
2);
arrayOfCells[row - 1][item].south.gotoAndStop(2);
arrayOfCells[row - 1][item].carved = true;
row--;
}
if (choice == "EAST")
{
arrayOfCells[row][item].east.gotoAndStop(2
);
arrayOfCells[row][item + 1].west.gotoAndStop(2);
arrayOfCells[row][item + 1].carved = true;
item++;
}
if (choice == "SOUTH")
{
arrayOfCells[row][item].south.gotoAndStop(
2);
arrayOfCells[row + 1][item].north.gotoAndStop(2);
arrayOfCells[row + 1][item].carved = true;
row++;
}
if (choice == "WEST")
{
arrayOfCells[row][item].west.gotoAndStop(2
);
arrayOfCells[row][item - 1].east.gotoAndStop(2);
arrayOfCells[row][item - 1].carved = true;
item--;
}
}
}
}

Stack is what contains all the places where you had two or more choices -- push() adds an item to an array, and pop() deletes the last item.Place that code on the actions for the main timeline. Then, to run both functions:\

createGrid();
carveMaze();
stop();

It should look something like this:

http://img236.images..hp?image=maze2cs.swf

• Member since: Jun. 30, 2005
• Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to As: Maze 2005-07-05 18:21:04

Nice, but I made a maze game with a lot simpler code than this. I just used walls! XD

Rustygames
Rustygames
• Member since: May. 7, 2005
• Offline.
Forum Stats
Supporter
Level 19
Programmer
Response to As: Maze 2005-07-23 15:05:13

At 7/5/05 06:21 PM, Galactic_Shit-Head wrote: Nice, but I made a maze game with a lot simpler code than this. I just used walls! XD

No I dont think you get it. His walls are dyanamic and thus this PWNs your thing

Shomer
Shomer
• Member since: Jun. 23, 2005
• Offline.
Forum Stats
Member
Level 13
Blank Slate
Response to As: Maze 2005-07-23 16:02:49

heres one thats alot simpler.
make a ball. give it the script:

moveSpeed = 5;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
if (_root.Walls.hitTest(getBounds(_root).xMax
,_y,true)) {
} else {
this._x += moveSpeed;

}

} else if (Key.isDown(Key.UP)) {
if (_root.Walls.hitTest(_x,getBounds(_root).y
Min,true)) {
} else {
this._y -= moveSpeed;

}

} else if (Key.isDown(Key.DOWN)) {
if (_root.Walls.hitTest(_x,getBounds(_root).y
Max,true)) {
} else {
this._y += moveSpeed;

}

} else if (Key.isDown(Key.LEFT)) {
if (_root.Walls.hitTest(getBounds(_root).xMin
,_y,true)) {
} else {
this._x -= moveSpeed;

}

}
}

now draw your maze make it a movieclip.
make its instance name "Walls" CAPS LOCK 'W".
no script needed for the walls!

Rustygames
Rustygames
• Member since: May. 7, 2005
• Offline.
Forum Stats
Supporter
Level 19
Programmer
Response to As: Maze 2005-07-23 16:08:07

At 7/23/05 04:02 PM, Shomer wrote: SOMETHING COMPLETELY DIFFERENT TO WHAT THIS THREAD ACHEIVES

Look did you actualy read the above stuff?
The code he explains how to make is all dyanamic and thus creats a new maze everytime so shhhhhhhhhhh n00bs

jkhkhj
jkhkhj
• Member since: Aug. 22, 2003
• Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to As: Maze 2005-07-23 16:53:10

At 7/23/05 04:08 PM, Ninja-Chicken wrote:
At 7/23/05 04:02 PM, Shomer wrote: SOMETHING COMPLETELY DIFFERENT TO WHAT THIS THREAD ACHEIVES
Look did you actualy read the above stuff?
The code he explains how to make is all dyanamic and thus creats a new maze everytime so shhhhhhhhhhh n00bs

Lol stfu. You don't deserve to call people n00bs yet, you're just not at that level.

Creepy
Creepy
• Member since: Nov. 28, 2004
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Maze 2005-09-06 16:24:31

At 7/23/05 04:53 PM, -fwe4life- wrote:
At 7/23/05 04:08 PM, Ninja-Chicken wrote:
At 7/23/05 04:02 PM, Shomer wrote: SOMETHING COMPLETELY DIFFERENT TO WHAT THIS THREAD ACHEIVES
Look did you actualy read the above stuff?
The code he explains how to make is all dyanamic and thus creats a new maze everytime so shhhhhhhhhhh n00bs
Lol stfu. You don't deserve to call people n00bs yet, you're just not at that level.

level, he didnt called peoples n00bs because of their levels, but because they sux in Actionscript! And yes Ninja-chichen is much better than this guys in AS!

mooseisloose
mooseisloose
• Member since: Dec. 6, 2004
• Offline.
Forum Stats
Member
Level 35
Game Developer
Response to As: Maze 2005-09-07 06:03:52

At 7/23/05 04:53 PM, -fwe4life- wrote:
At 7/23/05 04:08 PM, Ninja-Chicken wrote:
At 7/23/05 04:02 PM, Shomer wrote: SOMETHING COMPLETELY DIFFERENT TO WHAT THIS THREAD ACHEIVES
Look did you actualy read the above stuff?
The code he explains how to make is all dyanamic and thus creats a new maze everytime so shhhhhhhhhhh n00bs
Lol stfu. You don't deserve to call people n00bs yet, you're just not at that level.

it doesn't matter about level, you're talking to NINJA-CHICKEN!!! who has good enough AS skills to call many people n00bs. Be careful who you are talking to from now on.

Rustygames
Rustygames
• Member since: May. 7, 2005
• Offline.
Forum Stats
Supporter
Level 19
Programmer
Response to As: Maze 2005-09-07 11:18:13

Heh you know the above stuff I said was quite a while ago and I dont know how this thread got revived but thanks to the people who stuck up for me : )

Creepy
Creepy
• Member since: Nov. 28, 2004
• Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to As: Maze 2005-09-07 13:27:59

is he a:

is he??
i thought he said: n000bSSSSSS!
the "s" means more persons

flash noob?

no

forum noob?

no

maze noob?

no

computer noob?

no

internett noob?

no

the n00bs ninja chicken was talkin about was Actionscript n00bs!

the only thing in the world that is a "noob" is a baby becouse its new on everything ;P

rather call him:

a stuped motherfucker, whom diserves nothing but pain and suffer, that is so ugly that he is forbidden by law to have windows...

why, these n00bs dont deserve a so nice title!

I hope this was helpfull :D

nope

im norwegian!

nice for you! im too!
what is this topic about! huh?
Dont use ur time to write these "i know everything" posts!

go norwegian! xD

At 9/7/05 11:18 AM, Ninja-Chicken wrote: Heh you know the above stuff I said was quite a while ago and I dont know how this thread got revived but thanks to the people who stuck up for me : )

no problem!
Ninja-CHicken 4eva! xD

Starogre
Starogre
• Member since: May. 8, 2004
• Offline.
Forum Stats
Member
Level 18
Artist
Response to As: Maze 2005-09-07 13:35:40

Don't worry, I like your tutorial...

This maze is the best you can do without painting them in a movie clip...AHEM...

Good job, I'll use it some time...Unless I can do it myself :P

am i the only one that mentioned something about your tut? >.>

the events are merely fictional, written, directed, and acted out by all who create them

Starogre
Starogre
• Member since: May. 8, 2004
• Offline.
Forum Stats
Member
Level 18
Artist
Response to As: Maze 2005-09-07 13:38:32

At 9/7/05 06:03 AM, Moose_is_loose wrote:
At 7/23/05 04:53 PM, -fwe4life- wrote:
At 7/23/05 04:08 PM, Ninja-Chicken wrote:
At 7/23/05 04:02 PM, Shomer wrote: SOMETHING COMPLETELY DIFFERENT TO WHAT THIS THREAD ACHEIVES
Look did you actualy read the above stuff?
The code he explains how to make is all dyanamic and thus creats a new maze everytime so shhhhhhhhhhh n00bs
Lol stfu. You don't deserve to call people n00bs yet, you're just not at that level.
it doesn't matter about level, you're talking to NINJA-CHICKEN!!! who has good enough AS skills to call many people n00bs. Be careful who you are talking to from now on.

LOL...Sorry to double post but this was too funny to respond to...

HAVE YOU SEEN WHAT FWE HAS DONE!? hahaha...

Man oh man, you obviously don't know who YOU'RE talking to...

PS: HOLLOW POINT RULES!

the events are merely fictional, written, directed, and acted out by all who create them