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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 16: Board games<br />

Once the player’s chosen square has been checked and found <strong>to</strong> be legal we need <strong>to</strong> update<br />

the display. The function ‘adjustBoard’, Listing 16.7, does that. The start location and current<br />

player are passed <strong>to</strong> the function and then the values s<strong>to</strong>red in the ‘flip’ array set by the ‘scanBoard’<br />

function calls are used. Again we use eight calls <strong>to</strong> another function with the direction passed <strong>to</strong><br />

each call.<br />

1 function adjustBoard(row, col, piece){<br />

2<br />

3<br />

var opponent;<br />

4 if (piece==PLAYER){<br />

5 opponent = COMPUTER;<br />

6 }else{<br />

7 opponent = PLAYER;<br />

8<br />

9<br />

}<br />

10 if (flip[0]) flipLine(row, col, piece, opponent, 1, 0);<br />

11 if (flip[1]) flipLine(row, col, piece, opponent, 0, 1);<br />

12 if (flip[2]) flipLine(row, col, piece, opponent, -1, 0);<br />

13 if (flip[3]) flipLine(row, col, piece, opponent, 0, -1);<br />

14 if (flip[4]) flipLine(row, col, piece, opponent, -1, -1);<br />

15 if (flip[5]) flipLine(row, col, piece, opponent, 1, -1);<br />

16 if (flip[6]) flipLine(row, col, piece, opponent, 1, 1);<br />

17<br />

18 }<br />

if (flip[7]) flipLine(row, col, piece, opponent, -1, 1);<br />

Listing 16.7<br />

As you can see from Listing 16.7, the function ‘flipLine’ does the actual work of manipulating the<br />

display.<br />

1 function flipLine(row, col, piece, opponent, incX, incY){<br />

2 var x, y, name;<br />

3<br />

4 x = col + incX;<br />

5 y = row + incY;<br />

6 name = "board" + y + "_" + x;<br />

7<br />

8 while(eval(name)._currentframe==opponent){<br />

9 eval(name).go<strong>to</strong>AndS<strong>to</strong>p(piece);<br />

10 x += incX;<br />

11 y += incY;<br />

12 name = "board" + y + "_" + x;<br />

13 }<br />

14 }<br />

Listing 16.8<br />

251

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

Saved successfully!

Ooh no, something went wrong!