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 />

Instead of using lots of if conditions we can use a simple physics calculation. The Y value for a projectile<br />

motion is defined as:<br />

y = ut + gt 2<br />

where u is the launch value, t is the time in seconds and g is the value of gravity. If time, t, is less than<br />

a second the value for t 2 will be less than t, but above one second, t 2 will overtake. If g operates in<br />

the opposite direction <strong>to</strong> u then the increase in the values for y will decrease and eventually reverse.<br />

But this may seem all very hit and miss. Actually it is easy <strong>to</strong> calculate. Suppose that you want a jump<br />

<strong>to</strong> occur over one second and the starting and ending values for y are zero, then you want:<br />

ut =−gt 2<br />

when t = 0 and t = 1. To do this you need <strong>to</strong> solve a quadratic equation. Whoops, that sounds like<br />

maths! But believe me, it is not that hard. You want:<br />

ut + gt 2 = 0<br />

But you also want <strong>to</strong> control the maximum height of the jump. This occurs when the derivative<br />

of the curve has a value of zero. The derivative of the curve is:<br />

u + 2gt<br />

OK, so let’s fiddle with the figures. If we want a maximum height for the curve of 150 and if the<br />

<strong>to</strong>tal jump takes one second then the maximum height is going <strong>to</strong> be at 0.5 seconds; in other words,<br />

half way through the jump. At t = 0.5 we want:<br />

u + 2gt = 0 and ut + gt 2 = 150<br />

Replacing t with 0.5 gives:<br />

u + g = 0 and 0.5u + 0.25g = 150<br />

Two equations with two unknowns can be solved: u =−g from the first equation therefore we can<br />

substitute u for −g in the second equation giving −0.5g + 0.25g = 150 or 0.25g = 150; which means<br />

that g =−600 and u = 600.<br />

If you are working in metres rather than pixels then the usual value for g is −9.81 ms 2 but for<br />

convenience we will call this −10 ms 2 . Imagine that the cat is about one metre tall and can jump just<br />

270

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

Saved successfully!

Ooh no, something went wrong!