Tutorial details: this tutorial shows how to create the code necessary to build maze games in Flash. Using the new version of ActionScript, it can deal with any maze composed by horizontal and vertical lines. Diagonal and curve paths are not supported.
Tutorial name: A Maze Engine for Flash Games, Using ActionScript 2.0
Written by : Andrei R. Thomaz, andreithomaz@bol.com.br , www.rgbdesigndigital.com.br
Difficulty Level: advanced
Requirements: FlashMX 2004
Topics Covered: Maze games, classes, Action Script 2.0
Assumed Knowledge: some knowledge about object oriented programming and the new features introduced by Action Script 2.0
Download FLAs

This engine started with my artworks with mazes for the web. It was first implemented in SVG (Scalable Vector Graphics), using JavaScript. The engine was coded for use in the PacMan 1.0 (www.rgbdesigndigital.com.br/atravesdoespelho/pacman/), a modified version of the PacMan game (this work was selected for Rhizome Artbase and for some net art festivals). I rewrote them for ActionScript 1.0 and, now, I have completed the port for ActionScript 2.0 for use in another net art project (www.rgbdesigndigital.com.br/atravesdoespelho/pacman_minotauro/). I am very happy with the support for classes in FlashMX 2004; the new implementation is the most elegant and structured, more than the FlashMX and the SVG ones.

The engine can be used for the creation of PacMan games, and any game that has some characters moving inside a bidimensional maze. It can deal only with vertical and horizontal paths, but you can extend it for supporting diagonals, if you want.

License Information

You can use these scripts freely, but you have to give me the credits. If it is used in a commercial project, the credits has to be in some section of the game (you can put a '?' in the lower left corners, for example, and use it to show the credits). If it is not a commercial project, it can be put in a separated page.

Introduction to the Maze Engine

Because I did this to make a PacMan game, this will be our reference game along this tutorial. But these principles can be used in any maze game, I think.

First of all, we need to identify what are the elements necessary to run a maze engine. These elements are going to be implemented using the classes, the main feature introduced by ActionScript 2.0 in relation to version 1.0.

All the mazes are composed by paths that connect themselves. The characters (in my case, the PacMan and the monsters) move inside the maze, walking in these paths. Here, we can identify two structures: the groups of lines that compose the maze, and the points (PacMan, monsters, etc.) that move inside the maze.

Moving inside the maze, the point will pass by the lines of the mazes, through the crossroads found in the way. So, we need to represent this information: from one line, to what other lines is it possible to go? Here, it is the third element: the connections among the lines.

So, we have found the three main classes of this maze engine: the maze, composed by lines, the connections and the point(s) that moves inside the maze.

Sample Movie

You must use the keyboard to move the PacMan. If it doesn't move, please, click inside the maze and try again.

Line Class

The Line class is used to build the maze used by the engine. First, we need to break the maze in so many lines as nedeed.

Every one of these colored lines will be represented by a instance of Line class. This is the Line class structure:

class Line {
        var x1:Number; // the coordinates of the Line
        var y1:Number;
        var x2:Number;
        var y2:Number;
        var nType:Number = 0; // horizontal or vertical?
       
        var numConnections:Number = 0;
        var listConnections:Array;
       
        static var Vertical:Number = 1;
        static var Horizontal:Number = 2;
        static var Tolerance:Number = 10;
       
        // these will be used to define the place
        // of connections: in the beginning or in the
        // end of the line?
        static var Beginning:Boolean = true;
        static var End:Boolean = false;
       
        // ....
        // functions
}

Note: I used the static vars to emulate the #define's and enum's from C/C++. The static var was the best method that I found to set the values that I need that be the same in all the files that use them, without having to write a table of values in a paper and put it on my table, near the computer.