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.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

7 eval(name).go<strong>to</strong>AndS<strong>to</strong>p(1);<br />

8 }<br />

9 }<br />

10 curGo = 0;<br />

11 playersMove();<br />

12 }<br />

Listing 16.3<br />

The initialization function makes use of one of two functions that swap the current player and<br />

update the move counter ‘curGo’. Two movie clips use the variable ‘whoseGo’ <strong>to</strong> check for the<br />

next player; they are visually the lozenges behind the word ‘Computer’ and ‘Player’.<br />

1 function playersMove(){<br />

2 computerHL.go<strong>to</strong>AndS<strong>to</strong>p(1);<br />

3 playerHL.go<strong>to</strong>AndS<strong>to</strong>p(2);<br />

4 whoseGo = PLAYER;<br />

5 curGo++;<br />

6 }<br />

7<br />

8 function computerMove(){<br />

9 computerHL.go<strong>to</strong>AndS<strong>to</strong>p(2);<br />

10 playerHL.go<strong>to</strong>AndS<strong>to</strong>p(1);<br />

11 whoseGo = COMPUTER;<br />

12 curGo++;<br />

13 }<br />

Listing 16.4<br />

Tracking the player’s move<br />

A player’s move is legal if the square they clicked is empty, if in one of eight directions stemming<br />

from this square there is an opponent’s piece and if continuing in the same direction there is a<br />

player’s piece. This checking is all done using the ‘checkBoard’ function. Either the player or the<br />

computer can call this function; the caller is contained in the variable ‘piece’. The function calls a<br />

further function ‘scanBoard’ a <strong>to</strong>tal of eight times. For each call the direction that is being scanned<br />

is contained in the fifth and sixth parameters. Parameters one and two contain the starting square<br />

and parameters three and four the current player and opponent. For each direction the array ‘flip’<br />

is set <strong>to</strong> either true or false, and a variable ‘legal’ dictates whether this is an acceptable square.<br />

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

2 var legal = false, opponent;<br />

3<br />

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

5 opponent = COMPUTER;<br />

248

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

Saved successfully!

Ooh no, something went wrong!