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

Updating the players<br />

By the time the ‘update’ function is called for the players, all user input has been gathered and<br />

the nearest player <strong>to</strong> the ball has been s<strong>to</strong>red. There are two different update methods; either the<br />

player is under keyboard control in which case the movement has taken place, or the player is<br />

the computer-controlled player nearest the ball, in which case the function ‘opponentMove’ has<br />

already been called <strong>to</strong> update their position. If neither of these possibilities applies <strong>to</strong> the current<br />

player then the function ‘moveToTarget’ is used. Each player has a target position at which they<br />

are aiming, which is a simple mechanism <strong>to</strong> get the players <strong>to</strong> work as a team, rather than every<br />

player heading <strong>to</strong>wards the ball. The function works by first creating a vec<strong>to</strong>r from the current<br />

position of the player <strong>to</strong> the target position. If the target has been achieved then the square of<br />

this vec<strong>to</strong>r will be less than the constant ‘TARGETMIN’. If this is the case then a new target<br />

is calculated. Some players are set <strong>to</strong> be attacking players and the others are defenders. The new<br />

target is calculated taking in<strong>to</strong> consideration an arbitrary location on the pitch and the current<br />

ball position; attacking players use more of the ball position in deciding on a new target, whereas<br />

defenders tend <strong>to</strong> stay inside their own half. The function closes with a call <strong>to</strong> ‘setDirection’.<br />

1 function moveToTarget(){<br />

2 x = _x - targetx;<br />

3 y = _y - targety;<br />

4 if ((x*x + y*y)

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

Saved successfully!

Ooh no, something went wrong!