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

Options for easing in and out<br />

When you move a clip from one location <strong>to</strong> another over time you have several options over the<br />

look of the motion. Each time interval could be used <strong>to</strong> move the clip exactly the same distance.<br />

This is called linear interpolation and it is the default for <strong>Flash</strong> motion tweening. The result is<br />

fine for an object that is already moving but looks stiff and artificial if the object either starts from<br />

stationary or s<strong>to</strong>ps while in view. An alternative is <strong>to</strong> ease in <strong>to</strong> the motion; the first time interval<br />

would move the clip only a small amount and this would be increased for each successive time<br />

interval, either until a certain speed is achieved or the ramping could continue throughout the<br />

motion. Similarly at the end of a motion the amount of motion for each successive time interval<br />

could be reduced <strong>to</strong> bring the clip <strong>to</strong> a smooth s<strong>to</strong>p. When you start <strong>to</strong> play about with the speed<br />

of a clip in this way you will find that accelerating and then abruptly s<strong>to</strong>pping has a dynamic that<br />

may be suitable for a particular application. Motion is as important <strong>to</strong> the success of a game as the<br />

look and feel. Controlling the speed of a clip can be done using simple mathematics, the simplest<br />

option being linear.<br />

Linear interpolation<br />

If a clip moves from location A <strong>to</strong> location B in time T, then linear interpolation is simply<br />

var dt = time/duration;<br />

_x = offset.x * dt + start.x;<br />

_y = offset.y * dt + start.y;<br />

Here we use a point object <strong>to</strong> s<strong>to</strong>re the start position and the <strong>to</strong>tal movement that is required.<br />

A motion is usually relative <strong>to</strong> the starting position. In this instance we simply create a variable dt<br />

and set it <strong>to</strong> the current time since the start of the motion divided by the duration of the motion.<br />

This number will vary over time between 0 and 1. At the start no time will have elapsed so the<br />

value for dt will be 0, in which case the x and y value is simply set <strong>to</strong> the starting position. As the<br />

variable time approaches the duration value, dt will get close <strong>to</strong> one so that x and y will be set <strong>to</strong><br />

the starting value plus the value for offset.<br />

Quadratic interpolation<br />

The graph of y = x 2 is shown in Figure B.1. As you can see this is not a straight line but a curve.<br />

We will let the x value represent a number between 0 and 1, being the elapsed time in proportion<br />

<strong>to</strong> the <strong>to</strong>tal duration of a move. The y parameter will be the <strong>to</strong>tal distance moved. As you can see<br />

from the shape of the curve there will be a smaller movement at first, gaining speed throughout<br />

the move. This will give an ease in <strong>to</strong> the motion.<br />

Such a motion is achieved using this code snippet.<br />

396<br />

var dt = time/duration;<br />

_x = offset.x * dt * dt + start.x;<br />

_y = offset.y * dt * dt + start.y;

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

Saved successfully!

Ooh no, something went wrong!