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

 

senocular.com ActionScript Library

Tween.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
package com.senocular.motion {

	import flash.events.Event;
	import flash.display.Shape;
	import flash.events.IEventDispatcher;
	
	public class Tween extends Shape {
		
		protected var _target:*;
		protected var _property:String;
		protected var _start:Number;
		protected var _end:Number;
		protected var _range:Number;
		protected var _currentFrame:int;
		protected var _totalFrames:int;
		protected var _value:Number;
		protected var direction:int = 1;
		protected var _enabled:Boolean = true;
		protected var isValid:Boolean = false;
		protected var _easeMethod:Function;
		private var autoRemove:Boolean = true;
		private var eventTarget:IEventDispatcher;
		private var eventID:String;
			
		public function get target():* {
			return _target;
		}
		public function set target(o:*):void {
			_target = o;
		}
		public function get property():String {
			return _property;
		}
		public function set property(s:String):void {
			_property = s;
		}
		public function get start():Number {
			return _start;
		}
		public function set start(n:Number):void {
			_start = n;
			_range = _end - _start;
			isValid = false;
		}
		public function get end():Number {
			return _end;
		}
		public function set end(n:Number):void {
			_end = n;
			_range = _end - _start;
			isValid = false;
		}
		public function get currentFrame():int {
			return _currentFrame;
		}
		public function set currentFrame(n:int):void {
			if (n < 0) {
				n = 0;
			}else if (n > _totalFrames) {
				n = _totalFrames;
			}
			_currentFrame = n;
			isValid = false;
		}
		public function get totalFrames():int {
			return _totalFrames;
		}
		public function set totalFrames(n:int):void {
			_totalFrames = n;
			if (_currentFrame > _totalFrames) {
				_currentFrame = _totalFrames;
			}
			isValid = false;
		}
		public function get value():Number {
			if (!isValid) {
				var t:Number = _currentFrame/_totalFrames;
				if (Boolean(_easeMethod)) {
					t = _easeMethod(t);
				}
				_value = _start + _range*t;
				isValid = true;
			}
			return _value;
		}
		public function get playBackwards():Boolean {
			return (direction < 0);
		}
		public function set playBackwards(b:Boolean):void {
			direction = b ? -1 : 1;
		}
		
		public function get easeMethod():Function {
			return _easeMethod;
		}
		public function set easeMethod(f:Function):void {
			_easeMethod = f;
		}
		
		public function get enabled():Boolean {
			return _enabled;
		}
		public function set enabled(b:Boolean):void {
			_enabled = b;
		}
		
		/**
		 * Constructor 
		 */
		public function Tween(target:* = null, property:String = null, start:Number = 0, end:Number = 100, totalFrames:int = 100, easeMethod:Function = null) {
			_target = target;
			_property = property;
			_start = start;
			_end = end;
			_range = _end - _start;
			_totalFrames = totalFrames;
			_currentFrame = 0;
			_easeMethod = easeMethod;
			_value = value;
			
		}
		public function nextFrame(evt:Event = null):Number {
			if (!_enabled) {
				return _value;
			}
			var lastValue:Number = _value;
			currentFrame += direction;
			var v:Number = value;
			if (v == lastValue) {
				return _value;
			}
			draw();
			dispatchEvent(new Event(Event.CHANGE));
			if (_currentFrame == 0 || _currentFrame == _totalFrames) {
				if (eventTarget && autoRemove) {
					removeNextFrameEvent();
					eventTarget = null;
				}
				dispatchEvent(new Event(Event.COMPLETE));
			}
			return v;
		}
		public function nextFrameEvent(eventTarget:IEventDispatcher, eventID:String = Event.ENTER_FRAME, autoRemove:Boolean = true):void {
			removeNextFrameEvent();
			this.eventTarget = eventTarget;
			this.eventID = eventID;
			eventTarget.addEventListener(eventID, nextFrame);
			this.autoRemove = autoRemove;
		}
		public function removeNextFrameEvent():void {
			if (this.eventTarget) {
				eventTarget.removeEventListener(eventID, nextFrame);
				this.eventTarget = null;
			}
		}
		public function draw(evt:Event = null):void {
			if (_target && _property) {
				_target[_property] = value;
			}
		}
		public function complete(evt:Event = null):void {
			removeNextFrameEvent();
			_currentFrame = _totalFrames;
			isValid = false;
			draw();
		}
		public function reset(evt:Event = null):void {
			removeNextFrameEvent();
			_currentFrame = 0;
			isValid = false;
		}
		public function reverse(evt:Event = null):void {
			playBackwards = !playBackwards;
		}
	}
}