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.

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

The next step in the main game loop is <strong>to</strong> determine the player from each team that is nearest <strong>to</strong><br />

the ball. This is done using the function ‘nearestPlayer’. The function sets up the team name using<br />

the passed in team number. Then for each of the five players in the team the x and y distance<br />

<strong>to</strong> the ball is calculated. The actual distance <strong>to</strong> the ball is then the square root of the x distance<br />

square plus the y distance square. This is just Pythagoras’s theorem about the sides of a triangle.<br />

The square root function is actually quite a slow function <strong>to</strong> calculate and you could improve<br />

this code by just calculating the squared distance, because we are only interested in the nearest<br />

not the actual distance. If the loop is being run for the first time, namely i equals zero, then the<br />

variables ‘player’ and ‘dist’ are set <strong>to</strong> the current values of ‘i’ and ‘balldist’ respectively. If the loop<br />

is in the second or higher passes then the values for ‘player’ and ‘dist’ are only set if the ‘dist’<br />

value is greater than the value for ‘balldist’ in the current pass. Finally the ‘Direction’ clip inside<br />

the player’s clip is set if the current team is not the player’s team. The direction clip is an arrow<br />

indica<strong>to</strong>r showing the player’s direction and indicating <strong>to</strong> the player that this player has the ball<br />

focus.<br />

1 function nearestplayer(teamno){<br />

2<br />

3<br />

var i, dist, dx, dy, name, player;<br />

4 if (teamno==BLUE){<br />

5 teamname = "Pitch.Blue";<br />

6 }else{<br />

7 teamname = "Pitch.Red";<br />

8<br />

9<br />

}<br />

10 for (i=0; i

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

Saved successfully!

Ooh no, something went wrong!