Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

InterpolateAngleProperty.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.senocular.gyro {
	
	public class InterpolateAngleProperty extends InterpolateProperty {
			
		/**
		 * Constructor. Creates a new InterpolateAngleProperty instance.
		 * InterpolateAngleProperty instances are different from 
		 * InterpolateProperty instances in that they treat the property
		 * value as an angle within a "wrapping" 360 degree circle.  When
		 * going from one value to another, it determines the shortest
		 * distance to the new value within those degrees of rotation
		 * and follows that direction rather than interpolating the 
		 * numeric values directly.
		 * @param	start The start value for interpolation.
		 * @param	end The end value for interpolation.
		 * @param	owner The owner of the property to interpolate.
		 * @param	property The name of the property to interpolate.
		 */
		public function InterpolateAngleProperty(start:Number = NaN, end:Number = NaN, owner:* = null, property:Object = null){
			super(start, end, owner, property);
		}
		
		/**
		 * 
		 * Determines a value between start and end based on t where
		 * 0 is start and 1 is end.
		 * @param	t The amount to interpolate the property.
		 */
		public override function interpolate(t:Number):void {
			if (_owner && _property != null) {
				
				// angles get special treatment to account
				// for wrapping of 360 back down to 0
				var angleDiff:Number = _end - _start;
				
				// make sure the difference is a valid number
				if (isNaN(angleDiff) == false){
					
					if (angleDiff < -180) angleDiff += 360;
					else if (angleDiff > 180) angleDiff -= 360;
					
					_owner[_property] = _start + angleDiff*t;
				}
			}
		}
	}
}