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.

and if the curve section is between the penultimate and the final keyframe then<br />

T2 = 0.5 ∗ (K2.value − K1.value) ∗ ((1 − K2.tn)(1 + K2.bs)(1 − K1.ct)<br />

+ (1 − K2.tn)(1 − K2.bs)(1 + K2.ct))<br />

Appendix B<br />

In the tweening function we first test <strong>to</strong> see if the time passed as a parameter is within range. If not<br />

then we set the motion <strong>to</strong> either the first or last keyframe. Then we look through the keyframe<br />

list <strong>to</strong> see which section <strong>to</strong> interpolate. If the current time is an exact match with a keyframe time<br />

then no interpolation is required and so the position is set directly and the function returns. If<br />

interpolation is required then we precalculate the value of tcb coefficients and s<strong>to</strong>re them in the<br />

variables a − d. These are used <strong>to</strong> derive the values of T1 and T2. A section of the curve can be set<br />

<strong>to</strong> be linear if required in which case the interpolation calculation is simplified <strong>to</strong> starting position<br />

plus the product of delta time and the offset value for the curve section. This arithmetic is applied<br />

<strong>to</strong> all the channels in the keyframe.<br />

function tween_keyframe(name, channels, millisecs)<br />

{<br />

if (millisecskeys[keys.length-1].millisecs){<br />

setDirect(name, keys.length - 1);<br />

return;<br />

}<br />

//Must be within range<br />

var index = 0, i;<br />

for (i=0; ikeys[i].millisecs) index = i;<br />

if (millisecs == keys[index].millisecs){<br />

setDirect(name, index);<br />

return;<br />

}<br />

//Interpolation required<br />

var t, tt, ttt, h1, h2, h3, h4, a, b, c, d;<br />

var secDur, s1, s2, dd0, ds1, result, p0, p1, p2, p3, d10;<br />

secDur = keys[index+1].millisecs - keys[index].millisecs;<br />

t = (millisecs - keys[index].millisecs)/secDur;<br />

//Hermite coefficients<br />

tt = t*t; ttt = t*tt;<br />

407

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

Saved successfully!

Ooh no, something went wrong!