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

 

senocular.com ActionScript Library

SeekGravitate.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
48
49
50
51
52
53
54
55
package com.senocular.gyro {
	
	import flash.utils.Dictionary;
	/**
	 * Seekers follow target at a constant acceleration
	
	TODO: option for constant vector acceleration for x/y instead of individually
	option for matching if lower? do automatically?
	 */
	public class SeekGravitate extends Seek {
		
		protected var _acceleration:Number = .5;
		protected var velcities:Dictionary = new Dictionary();
		
		/**
		 * A value between 0 and 1 representing how quickly
		 * a seeker accelerationes the target with every step
		 */
		public function get acceleration():Number {
			return _acceleration;
		}
		public function set acceleration(n:Number):void {
			_acceleration = n;
		}
	
		/**
		 * Constructor
		 */
		public function SeekGravitate(acceleration:Number = .5, properties:Array = null){
			super(properties);
			this.acceleration = acceleration;
		}
		
		/**
		 * Seek method; matches seeker properties
		 * to target properties based on the properties list
		 */
		public override function follow(target:*, seeker:*):void {
			if (!_acceleration) return;
	
			var diff:Number;
			for each(var prop:* in _properties){

				diff = target[prop] - seeker[prop];

				if (!(prop in velcities)){
					velcities[prop] = 0;
				}

				velcities[prop] += _acceleration * diff;
				seeker[prop] += velcities[prop];
			}
		}
	}
}