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 8: In tip-<strong>to</strong>p condition<br />

Within the function we use these values <strong>to</strong> get the distance between the points along each axis, using<br />

x = x1 - x2;<br />

y = y1 - y2;<br />

Now if you are being extra smart, you will realize that if x2 ory2 are greater than x1 and y1<br />

respectively, both x and y could take negative values. How do we handle this case? The fact is<br />

that in the function we make use of the square of these values and a square can never be negative.<br />

If we multiply a positive or negative number by itself then we get a positive value. For example:<br />

//-2 * -2 = 4 not -4<br />

A function can return a value; <strong>to</strong> do this we use the keyword return. In this example we<br />

also use the square root function that is part of the Math object. The square root function takes a<br />

single parameter and returns the square root of this parameter. In this instance we are interested in<br />

the sum of the two squares.<br />

Now we can make use of this function <strong>to</strong> test the distance between two points:<br />

if (distanceBetween(100, 200, clip._x, clip_y)

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

Saved successfully!

Ooh no, something went wrong!