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 18: Sports simulations<br />

Having calculated the nearest player <strong>to</strong> the ball for both teams, these are s<strong>to</strong>red in the variables<br />

‘a’ and ‘b’. The code is designed so that a player can choose which team <strong>to</strong> use, then the choice<br />

is s<strong>to</strong>red in the variable ‘playersTeam’. If the opponent closest <strong>to</strong> the ball has changed after calling<br />

the ‘nearestPlayer’ function then root level variables ‘opponentNo’ and ‘movecount’ are updated,<br />

the function ‘opponentMove’ is executed on the opponent nearest <strong>to</strong> the ball and the current team<br />

member nearest <strong>to</strong> the ball is s<strong>to</strong>red in the root level variable ‘closestplayer’.<br />

The opponentMove function<br />

This function is quite complex and needs some careful study. The basic idea is that the player wants<br />

<strong>to</strong> move <strong>to</strong>wards the ball in such a direction that they can have a direct kick <strong>to</strong>wards the correct<br />

goal. The function starts by creating a vec<strong>to</strong>r from the goal <strong>to</strong> the ball. This vec<strong>to</strong>r needs <strong>to</strong> be<br />

of length 1. To convert an arbitrary vec<strong>to</strong>r <strong>to</strong> a vec<strong>to</strong>r of length 1 in the same direction we must<br />

normalize the vec<strong>to</strong>r; that is we must divide both the values by the same number, so it is then still<br />

in the same direction, in such a way that if we draw the diagonal vec<strong>to</strong>r connecting the start of<br />

the vec<strong>to</strong>r <strong>to</strong> the end the length will be 1. First we find out what the current length is using the<br />

famous Pythagoras theorem. This value is s<strong>to</strong>red in the variable ‘mag’. Now we divide both the<br />

elements in the vec<strong>to</strong>r by this value, and hey pres<strong>to</strong> we have a unit length vec<strong>to</strong>r. Next we move<br />

along this vec<strong>to</strong>r three times the ball’s diameter and create a new target s<strong>to</strong>red in the variables ‘gx’<br />

and ‘gy’ which will be the perfect spot for our player <strong>to</strong> run and hit the ball <strong>to</strong> get a terrific goal<br />

kick. Now we create two ramp values ‘a’ and ‘b’. ‘a’ is how much we want our player <strong>to</strong> run<br />

<strong>to</strong>wards our perfect spot <strong>to</strong> kick the ball, ‘b’ is how much we want our player <strong>to</strong> just run at the<br />

ball. If we are within three times the ball’s diameter then we want the player <strong>to</strong> be more targeted<br />

at hitting the ball the nearer they are. To make sure we show the appropriate images of the player<br />

we must calculate the angle that the player will be running now that we have the information<br />

needed. The player graphics contains 32 separate two-frame run cycles, rotating around a full 360<br />

degrees. We can decide which <strong>to</strong> show on the basis of this angle. The angle is calculated using<br />

the ‘atan’ function, which takes two sides of a triangle and returns an angle. This angle can be<br />

converted in<strong>to</strong> which frame <strong>to</strong> show using a division by the constant FRAMETORADIANS.<br />

The player must avoid kicking the ball <strong>to</strong>wards their own goal so if they are positioned in a line<br />

comprising player, ball, own goal then they must run around the outside of the ball. This is done<br />

by adjusting their angle; if this is the case, aiming <strong>to</strong>wards the outside of a circle that is three times<br />

the player’s diameter.<br />

1 function opponentmove(){<br />

2 //Create a vec<strong>to</strong>r from the goal <strong>to</strong> the ball<br />

3 x = _root.Pitch.Ball._x - 260;<br />

4 y = _root.Pitch.Ball._y - 0;<br />

5 //Normalise this vec<strong>to</strong>r<br />

6 //mag is the distance from the ball <strong>to</strong> the goal<br />

7 mag = Math.sqrt(x*x + y*y);<br />

8 x /= mag;<br />

9 y /= mag;<br />

289

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

Saved successfully!

Ooh no, something went wrong!