-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tween.as
199 lines (180 loc) · 4.89 KB
/
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package net.flashpunk
{
import net.flashpunk.tweens.TweenInfo;
/**
* Base class for all Tween objects, can be added to any Core-extended classes.
*/
public class Tween
{
/**
* Persistent Tween type, will stop when it finishes.
*/
public static const PERSIST:uint = 0;
/**
* Looping Tween type, will restart immediately when it finishes.
*/
public static const LOOPING:uint = 1;
/**
* Oneshot Tween type, will stop and remove itself from its core container when it finishes.
*/
public static const ONESHOT:uint = 2;
/**
* If the tween should update.
*/
public var active:Boolean;
/**
* Tween completion callback.
*/
public var complete:Function;
/**
* Constructor. Specify basic information about the Tween.
* @param duration Duration of the tween (in seconds or frames).
* @param type Tween type, one of Tween.PERSIST (default), Tween.LOOPING, or Tween.ONESHOT.
* @param complete Optional callback for when the Tween completes.
* @param ease Optional easer function to apply to the Tweened value.
*/
public function Tween(duration:Number, type:uint = 0, complete:Function = null, ease:Function = null)
{
_target = duration;
_type = type;
this.complete = complete;
_ease = ease;
}
/**
* Updates the Tween, called by World.
*/
public function update():void
{
_time += FP.fixed ? 1 : FP.elapsed;
_t = _time / _target;
if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
if (_time >= _target)
{
_t = 1;
_finish = true;
}
}
/**
* Starts the Tween, or restarts it if it's currently running.
*/
public function start():void
{
_time = 0;
if (_target == 0)
{
active = false;
return;
}
active = true;
}
/**
* Immediately stops the Tween and removes it from its Tweener without calling the complete callback.
*/
public function cancel():void
{
active = false;
if (_parent) _parent.removeTween(this);
}
/** @private Called when the Tween completes. */
internal function finish():void
{
switch (_type)
{
case 0:
_time = _target;
active = false;
break;
case 1:
_time %= _target;
_t = _time / _target;
if (_ease != null && _t > 0 && _t < 1) _t = _ease(_t);
start();
break;
case 2:
_time = _target;
active = false;
_parent.removeTween(this);
break;
}
_finish = false;
if (complete != null) complete();
}
/**
* The completion percentage of the Tween.
*/
public function get percent():Number { return _time / _target; }
public function set percent(value:Number):void { _time = _target * value; }
/**
* The current time scale of the Tween (after easer has been applied).
*/
public function get scale():Number { return _t; }
/**
* Tweens the properties of an object.
* @param object Object to tween.
* @param duration Duration of the tween.
* @param values Properties to tween and their target values (eg. {x:100, y:200} ).
* @param complete Optional completion callback function.
* @param ease Optional easer function.
*/
public static function to(object:Object, duration:Number, values:Object, complete:Function = null, ease:Function = null):void
{
var t:TweenInfo = TweenInfo.create(object, duration, values, complete, ease),
i:int = _tweens.length;
while (i --)
{
if (!_tweens[i])
{
_tweens[i] = t;
return;
}
}
_tweens.push(t);
}
/**
* Clears any active static tweens called by Tween.to().
*/
public static function clear():void
{
for each (var tween:TweenInfo in _tweens) tween.destroy();
_tweens.length = 0;
}
/** @private Updates the static tweens. */
internal static function update():void
{
var e:Number, t:Number, i:String, tween:TweenInfo, j:int = _tweens.length, f:Function;
while (j --)
{
tween = _tweens[j];
if (tween)
{
tween.elapsed += FP.elapsed;
e = tween.elapsed / tween.duration;
if (e >= 1) e = 1;
t = tween.ease == null ? e : tween.ease(e);
for (i in tween.start) tween.object[i] = tween.start[i] + tween.range[i] * t;
if (e == 1)
{
f = tween.complete;
tween.destroy();
_tweens[j] = null;
if (f != null) f();
}
}
}
}
// Tween information.
/** @private */ private var _type:uint;
/** @private */ protected var _ease:Function;
/** @private */ protected var _t:Number = 0;
// Timing information.
/** @private */ protected var _time:Number;
/** @private */ protected var _target:Number;
// List information.
/** @private */ internal var _finish:Boolean;
/** @private */ internal var _parent:Tweener;
/** @private */ internal var _prev:Tween;
/** @private */ internal var _next:Tween;
// Static tweening information.
/** @private */ private static var _tweens:Vector.<TweenInfo> = new Vector.<TweenInfo>;
}
}