02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15: Mazes<br />

Notice that the frame 1 code contains a call <strong>to</strong> a function called ‘createMaze’. The purpose of this<br />

function is <strong>to</strong> dynamically create the duplicated movie clips that show a maze grid. To ensure that<br />

a previous maze grid is deleted the function calls ‘clearCells’. The number of duplicated movie<br />

clips in the vertical direction is s<strong>to</strong>red in the variable ‘cellV’ and the number in the horizontal<br />

direction in the variable ‘cellH’. These two variables are used by the ‘clearCells’ function in the<br />

two loops, for ‘row’ and ‘col’. Having duplicated a clip it is positioned based on a grid centred on<br />

the middle of the movie. To centre something you can use the code snippet<br />

left = (movieWidth - clipWidth)/2;<br />

<strong>to</strong>p = (movieHeight - clipHeight)/2;<br />

The ‘orgX’ and ‘orgY’ variables, Listing 15.2, lines 6 and 7, are set in a similar way, only rather<br />

than using the value for a clip’s width and height we take the value of the number of rows and<br />

columns multiplied by a cell width and height, giving the <strong>to</strong>tal width of the maze on screen.<br />

1 function createMaze(){<br />

2 var name, row, col, count = 1, orgX, orgY;<br />

3<br />

4 if (cellH>0 || cellV>0) clearCells();<br />

5<br />

6 orgX = (450 - cols * 16)/2;<br />

7 orgY = (400 - rows * 16)/2;<br />

8<br />

9 for (row=0; row

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!