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

 

senocular.com ActionScript Library

TimeTweener.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package com.senocular.gyro {

	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.utils.getTimer;
	
	/**
	 * Defines a repeating action used to change one or more values over
	 * a pre-determined amount of time.
	 */
	public class TimeTweener
		extends AbstractTweener
		implements IStartable {
		
		protected var _time:int;
		protected var _currentTime:int = 0;
			
		// help calculating position
		private var startTime:int = -1;
		private var pauseTime:int = 0;
			
		public function get time():int {
			return _time;
		}
		public function set time(value:int):void {
			// _time should never be less than 1
			if (value < 1){
				value = 1;
			}
			
			// exit if _time already has this value
			if (value == _time) return;
			
			_time = value;
			
			// make sure _currentTime is within _time
			if (_currentTime > _time){
				_currentTime = _time;
			}
			
			// if updated, calculate _progress and draw
			_progress = _currentTime/_time;
			draw();
		}
		
		public function get currentTime():int {
			return _currentTime;
		}
		public function set currentTime(value:int):void {
			// clamp _currentTime between 0 and _time
			if (value < 0){
				value = 0;
			}else if (value > _time){
				value = _time;
			}
			
			// exit if _currnetTime already has this value
			if (value == _currentTime) return;
			
			_currentTime = value;
			
			// if updated, calculate _progress and draw
			_progress = _currentTime/_time;
			draw();
		}
		
		/**
		 * Constructor 
		 */
		public function TimeTweener(time:int = 1000, ease:IEase = null, eventDispatcher:IEventDispatcher = null, eventType:String = Event.ENTER_FRAME, eventPriority:int = 0){
			super(ease, eventDispatcher, eventType, eventPriority);
			_time = time;
		}
		override protected function startHandler(event:GyroEvent):void {
			if (startTime == -1){
				
				// starting from scratch then play
				_currentTime = 0;
				_progress = 0;
				_currentRepeat = 0;
				_reversed = false;
				startTime = getTimer();
			}else{
				
				// resuming playback
				// offset startTime
				startTime += getTimer() - pauseTime;
				
				// reset pauseTime to 0
				pauseTime = 0;
			}
			
			eventEnabled = true;
			draw();
		}
		override protected function pauseHandler(event:GyroEvent):void {
			eventEnabled = false;
			pauseTime = getTimer();
		}
		override protected function reverseHandler(event:GyroEvent):void {
			_reversed = !_reversed;
			
			if (_reversed){
				
				if (_currentTime >= _time){
						
					// offset start time to represent the new cycle time
					startTime += _time;

					// update _currentTime and _progress
					// for other reverse event handlers
					_currentTime = _time - (_currentTime - _time);
					_progress = _currentTime/_time;
					
					draw();
				}else{
					
					startTime = getTimer() - (_time - _currentTime);
				}
			}else{
				
				startTime = getTimer() - _currentTime;
			}
		}
		override protected function repeatHandler(event:GyroEvent):void {
			// only increment _currentRepeat if needed
			if (_repeatCount > 0) {
				_currentRepeat++;
			}
			
			// time-based always repeatSeemless
			startTime += _time;
			
			// flag to determine whether or not the repeat
			// event is also forcing a reversal of playback
			// this will happen at the end of reverse
			// playback if _autoReverse is true
			var sendReverseEvent:Boolean = false;
			
			if (_reversed){
				if (_autoReverse){
					
					// _currentTime just past 0 into the negatives
					// invert to wrap
					_currentTime = -_currentTime;
					
					// signal that the reverse event should be
					// called.  It is not called here since
					// progress should be defined first
					sendReverseEvent = true;
					
				}else{
					
					// _currentTime just past 0 into the negatives
					// add _time to wrap it at the end
					_currentTime += _time;
				}
			}else{
				
				// _currentTime just past _time in the positives
				// subtract _time to wrap it at the end
				_currentTime -= _time;
			}			
			
			// update _progress before reverse event
			_progress = _currentTime/_time;
			
			// call reverse event if coming out of reverse playback
			// changing _reversed to false
			if (sendReverseEvent) {
				dispatchEvent(new GyroEvent(GyroEvent.REVERSE, this));
			}
			draw();
		}
		override protected function stopHandler(event:GyroEvent):void {
			
			// clear the tween event
			// and reset all values
			eventEnabled = false;
			_currentTime = 0;
			_progress = 0;
			_currentRepeat = 0;
			startTime = -1;
			_reversed = false;
			draw();
		}
		override protected function completeHandler(event:GyroEvent):void {
			
			// clear the tween evetn
			eventEnabled = false;
			
			if (_reversed || _autoReverse){
				
				// when reversed, completion exists
				// at the start of the tween
				_currentTime = 0;
				_progress = 0;
				
			}else{
				
				// normal playback completes at the end
				_currentTime = _time;
				_progress = 1;
			}
			
			// startTime is cleared for completion
			startTime = -1;
			draw();
		}

		override protected function tweenHandler(event:GyroEvent) :void {
			
			if (_reversed){
				
				// _currentTime is inverted for reverse playback
				_currentTime = _time - (getTimer() - startTime);
				
				// completion of reversed time is when _currentTime
				// is equal to or less than 0
				if (_currentTime <= 0){
					
					// if the tween is repeating
					if (_repeatCount < 0 || _currentRepeat < _repeatCount){
						
						// repeating indefinitely or upto a count
						dispatchEvent(new GyroEvent(GyroEvent.REPEAT, this));
						
					}else{
						
						// tween has ended
						dispatchEvent(new GyroEvent(GyroEvent.COMPLETE, this));
					}
					
					// done; exit
					return;
				}
				
			// not reversed; normal playback
			}else{
				
				// current time based off difference of getTimer time from startTime
				_currentTime = getTimer() - startTime;
				
				// completion of normal time is when _currentTime
				// is equal to or greater than _time
				if (_currentTime >= _time){
						
					// check to see if the tween needs to reverse
					if (_autoReverse){
						
						// do not complete or repeat, but start new cycle backwards
						dispatchEvent(new GyroEvent(GyroEvent.REVERSE, this));
						
					// if the tween is repeating
					}else if (_repeatCount < 0 || _currentRepeat < _repeatCount){
						
						// repeating indefinitely or upto a count
						dispatchEvent(new GyroEvent(GyroEvent.REPEAT, this));
						
					}else{
						
						// tween has ended
						dispatchEvent(new GyroEvent(GyroEvent.COMPLETE, this));
					}
					
					// done; exit
					return;
				}
			}
			
			// normal/reversed tweening or in the process of being reversed
			_progress = _currentTime/_time;
			draw();
		}
	}
}