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

 

senocular.com ActionScript Library

SeekRecording.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.senocular.gyro {
	
	import flash.utils.Dictionary;
	
	/**
	 * Seekers follow a target based on a factor where
	 * with each step the follow moves factor% closer to the target
	 */
	public class SeekRecording extends Seek {
		
		protected var _recording:Array = [];
		protected var _properties:Array = [];
		protected var _delay:uint = 1;
		
		/**
		 * A collection of properties to be recorded
		 * and then reassigned to the owner after a delay
		 */
		public function get properties():Array {
			return _properties.length ? _properties.concat() : null;
		}
		public function set properties(a:Array):void {
			_properties = a.length ? a.concat() : [];
		}
		
		/**
		 * The number of steps each seeker is behind
		 * the target.  Seekers will not start following
		 * until at least delay steps have first been recorded
		 */
		public function get delay():uint {
			return _delay;
		}
		public function set delay(i:uint):void {
			_delay = i;
		}
	
		/**
		 * Constructor
		 */
		public function SeekRecording(delay:uint = 1, properties:Array = null){
			this.properties = properties;
			this.delay = delay;
		}
		
		/**
		 * Seek method; matches seeker properties
		 * to target properties based on the properties list
		 */
		public override function call(interpolate:IInterpolate):void {
			var prop:*;
			var matches:Boolean = true;
			
			// use a Dictionary for a record since property
			// keys may be object keys
			var newRecord:Dictionary = new Dictionary();
			
			// copy targets property values into record
			for each(prop in _properties){
				newRecord[prop] = target[prop];
			}
			
			// add the record into the recording
			_recording.push(newRecord);
			
			// if we've recorded enough to reach the delay
			// records from the recording can be used to
			// set values within the seekers
			if (_recording.length > _delay) {
				
				// remove the first record from the recordigns
				var record:Dictionary = Dictionary(_recording.shift());
				
				// copy the record to the seeker
				for (prop in record){
					seeker[prop] = record[prop];
					if (matches && record[prop] != newRecord[prop]){
						matches = false;
					}
				}
			}
			if (matches){
				if (!_atDestination) atDestination = true;
			}else if (_atDestination){
				_atDestination = false;
			}
		}
	}
}