/
TimelineMax.as
executable file
·1467 lines (1409 loc) · 84.1 KB
/
TimelineMax.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* VERSION: 12.0.3
* DATE: 2013-02-28
* AS3 (AS2 version is also available)
* UPDATES AND DOCS AT: http://www.greensock.com/timelinemax/
**/
package com.greensock {
import com.greensock.core.SimpleTimeline;
import com.greensock.core.Animation;
import com.greensock.easing.Ease;
import com.greensock.events.TweenEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
/**
* TimelineMax extends TimelineLite, offering exactly the same functionality plus useful
* (but non-essential) features like repeat, repeatDelay, yoyo, currentLabel(), addCallback(),
* removeCallback(), tweenTo(), tweenFromTo(), getLabelAfter(), getLabelBefore(),
* getActive(), AS3 event dispatching (and probably more in the future). It is the ultimate
* sequencing tool that acts like a container for tweens and other timelines, making it
* simple to control them as a whole and precisely manage their timing. Without TimelineMax
* (or its little brother TimelineLite), building complex sequences would be far more cumbersome
* because you'd need to use the <code>delay</code> special property for everything which would
* make future edits far more tedius. Here is a basic example:
* <listing version="3.0">
TweenLite.to(mc, 1, {x:100});
TweenLite.to(mc, 1, {y:50, delay:1});
TweenLite.to(mc, 1, {alpha:0, delay:2});
</listing>
* The above code animates <code>mc.x</code> to 100, then <code>mc.y</code> to 50, and finally
* <code>mc.alpha</code> to 0 (notice the <code>delay</code> in all but the first tween). But
* imagine if you wanted to increase the duration of the first tween to 1.5 - you'd need to
* adjust every delay thereafter. And what if you want to <code>pause()</code> the whole
* sequence or <code>restart()</code> it or <code>reverse()</code> it on-the-fly or repeat
* it twice? This becomes quite messy (or flat-out impossible), but TimelineMax makes it
* incredibly simple:
*
* <listing version="3.0">
var tl = new TimelineMax({repeat:2, repeatDelay:1});
tl.add( TweenLite.to(mc, 1, {x:100}) );
tl.add( TweenLite.to(mc, 1, {y:50}) );
tl.add( TweenLite.to(mc, 1, {alpha:0}) );
//then later, control the whole thing...
tl.pause();
tl.resume();
tl.seek(1.5);
tl.reverse();
...
</listing>
* Or use the convenient <code>to()</code> method and chaining to make it even shorter:
* <listing version="3.0">
var tl = new TimelineMax();
tl.to(mc, 1, {x:100}).to(mc, 1, {y:50}).to(mc, 1, {alpha:0});
</listing>
*
* <p>Now you can feel free to adjust any of the tweens without worrying about trickle-down
* changes to delays. Increase the duration of that first tween and everything automatically
* adjusts.</p>
*
* <p>Here are some other benefits and features of TimelineMax:</p>
*
* <ul>
* <li> Things can overlap on the timeline as much as you want. You have complete control
* over where tweens/timelines are placed. Most other animation tools can only do basic
* one-after-the-other sequencing but can't allow things to overlap. Imagine appending
* a tween that moves an object and you want it to start fading out 0.5 seconds before the
* end of that tween? With TimelineMax it's easy.</li>
*
* <li> Add labels, callbacks, play(), stop(), seek(), restart(), and even reverse() smoothly anytime.</li>
*
* <li> Nest timelines within timelines as deeply as you want. This means you can modularize
* your code and make it far more efficient. Imagine building your app with common animateIn()
* and animateOut() methods that return a tween or timeline instance, then you can string
* things together like
* <code>myTimeline.add( myObject.animateIn() ).add( myObject.animateOut(), "+=4").add( myObject2.animateIn(), "-=0.5")...</code></li>
*
* <li> Speed up or slow down the entire timeline with its <code>timeScale()</code> method.
* You can even tween it to gradually speed up or slow down the animation smoothly.</li>
*
* <li> Get or set the progress of the timeline using its <code>progress()</code> or
* <code>totalProgress()</code> methods. For example, to skip to the halfway point,
* set <code>myTimeline.progress(0.5);</code></li>
*
* <li> Tween the <code>time, totalTime, progress,</code> or <code>totalProgress</code> to
* fastforward/rewind the timeline. You could even attach a slider to one of these to give the
* user the ability to drag forward/backward through the timeline.</li>
*
* <li> Add <code>onComplete, onStart, onUpdate, onRepeat</code> and/or <code>onReverseComplete</code>
* callbacks using the constructor's <code>vars</code> object like
* <code>var tl = new TimelineMax({onComplete:myFunction});</code></li>
*
* <li> Kill the tweens of a particular object inside the timeline with <code>killTweensOf()</code>
* or get the tweens of an object with <code>getTweensOf()</code> or get all the tweens/timelines
* in the timeline with <code>getChildren()</code></li>
*
* <li> Set the timeline to repeat any number of times or indefinitely. You can even set a delay
* between each repeat cycle and/or cause the repeat cycles to yoyo, appearing to reverse direction
* every other cycle. </li>
*
* <li> listen for START, UPDATE, REPEAT, REVERSE_COMPLETE, and COMPLETE events.</li>
*
* <li> get the active tweens in the timeline with getActive().</li>
*
* <li> By passing <code>useFrames:true</code> in the <code>vars</code> parameter, you can
* base the timing on frames instead of seconds. Please note, however, that
* the timeline's timing mode dictates its childrens' timing mode as well. </li>
*
* <li> Get the <code>currentLabel()</code> or find labels at various positions in the timeline
* using <code>getLabelAfter()</code> and <code>getLabelBefore()</code></li>
*
* <li> You can export all the tween/timelines from the root (master) timeline anytime into
* a TimelineLite instance using <code>TimelineLite.exportRoot()</code> so that
* you can <code>pause()</code> them all or <code>reverse()</code> or alter their
* <code>timeScale</code>, etc. without affecting tweens/timelines that you create in
* the future. Imagine a game that has all its animation driven by the GreenSock
* Animation Platform and it needs to pause or slow down while a status screen pops up.
* Very easy.</li>
*
* </ul>
*
*
* <p><strong>SPECIAL PROPERTIES:</strong></p>
* <p>You can optionally use the constructor's <code>vars</code> parameter to define any of
* the special properties below (syntax example: <code>new TimelineMax({onComplete:myFunction, repeat:2, repeatDelay:1, yoyo:true});</code></p>
*
* <ul>
* <li><strong> delay </strong>:<em> Number</em> -
* Amount of delay in seconds (or frames for frames-based tweens) before the timeline should begin.</li>
*
* <li><strong> paused </strong>:<em> Boolean</em> -
* If <code>true</code>, the timeline will pause itself immediately upon creation (by default,
* timelines automatically begin playing immediately). If you plan to create a TimelineMax and
* then populate it later (after one or more frames elapse), it is typically best to set
* <code>paused:true</code> and then <code>play()</code> after you populate it.</li>
*
* <li><strong> onComplete </strong>:<em> Function</em> -
* A function that should be called when the timeline has completed</li>
*
* <li><strong> onCompleteParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onComplete</code> function. For example,
* <code>new TimelineMax({onComplete:myFunction, onCompleteParams:["param1", "param2"]});</code>
* To self-reference the timeline instance itself in one of the parameters, use <code>"{self}"</code>,
* like: <code>onCompleteParams:["{self}", "param2"]</code></li>
*
* <li><strong> useFrames </strong>:<em> Boolean</em> -
* If <code>useFrames</code> is <code>true</code>, the timelines's timing will be
* based on frames instead of seconds because it is intially added to the root
* frames-based timeline. This causes both its <code>duration</code>
* and <code>delay</code> to be based on frames. An animations's timing mode is
* always determined by its parent <code>timeline</code>.</li>
*
* <li><strong> tweens </strong>:<em> Array</em> -
* To immediately insert several tweens into the timeline, use the <code>tweens</code>
* special property to pass in an Array of TweenLite/TweenMax/TimelineLite/TimelineMax
* instances. You can use this in conjunction with the <code>align</code> and
* <code>stagger</code> special properties to set up complex sequences with minimal code.
* These values simply get passed to the <code>add()</code> method.</li>
*
* <li><strong> align </strong>:<em> String</em> -
* Only used in conjunction with the <code>tweens</code> special property when multiple
* tweens are to be inserted immediately. The value simply gets passed to the
* <code>add()</code> method. The default is <code>"normal"</code>.
* Options are:
* <ul>
* <li><strong><code>"sequence"</code></strong>: aligns the tweens one-after-the-other in a sequence</li>
* <li><strong><code>"start"</code></strong>: aligns the start times of all of the tweens (ignores delays)</li>
* <li><strong><code>"normal"</code></strong>: aligns the start times of all the tweens (honors delays)</li>
* </ul>
* The <code>align</code> special property does <strong>not</strong> force all child
* tweens/timelines to maintain relative positioning, so for example, if you use
* <code>"sequence"</code> and then later change the duration of one of the nested tweens,
* it does <strong>not</strong> force all subsequent timelines to change their position.
* The <code>align</code> special property only affects the alignment of the tweens that are
* initially placed into the timeline through the <code>tweens</code> special property of
* the <code>vars</code> object.</li>
*
* <li><strong> stagger </strong>:<em> Number</em> -
* Only used in conjunction with the <code>tweens</code> special property when multiple
* tweens are to be inserted immediately. It staggers the tweens by a set amount of time
* in seconds (or in frames if <code>useFrames</code> is true). For example, if the
* stagger value is 0.5 and the "align" property is set to <code>"start"</code>, the
* second tween will start 0.5 seconds after the first one starts, then 0.5 seconds
* later the third one will start, etc. If the align property is <code>"sequence"</code>,
* there would be 0.5 seconds added between each tween. This value simply gets
* passed to the <code>add()</code> method. Default is 0.</li>
*
* <li><strong> onStart </strong>:<em> Function</em> -
* A function that should be called when the timeline begins (when its <code>time</code>
* changes from 0 to some other value which can happen more than once if the
* timeline is restarted multiple times).</li>
*
* <li><strong> onStartParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onStart</code> function. For example,
* <code>new TimelineMax({onStart:myFunction, onStartParams:["param1", "param2"]});</code>
* To self-reference the timeline instance itself in one of the parameters, use <code>"{self}"</code>,
* like: <code>onStartParams:["{self}", "param2"]</code></li>
*
* <li><strong> onUpdate </strong>:<em> Function</em> -
* A function that should be called every time the timeline updates
* (on every frame while the timeline is active)</li>
*
* <li><strong> onUpdateParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onUpdate</code> function. For example,
* <code>new TimelineMax({onUpdate:myFunction, onUpdateParams:["param1", "param2"]});</code>
* To self-reference the timeline instance itself in one of the parameters, use <code>"{self}"</code>,
* like: <code>onUpdateParams:["{self}", "param2"]</code></li>
*
* <li><strong> onReverseComplete </strong>:<em> Function</em> -
* A function that should be called when the timeline has reached its beginning again from the
* reverse direction. For example, if <code>reverse()</code> is called, the timeline will move
* back towards its beginning and when its <code>time</code> reaches 0, <code>onReverseComplete</code>
* will be called. This can also happen if the timeline is placed in a TimelineLite or TimelineMax
* instance that gets reversed and plays the timeline backwards to (or past) the beginning.</li>
*
* <li><strong> onReverseCompleteParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onReverseComplete</code> function. For example,
* <code>new TimelineMax({onReverseComplete:myFunction, onReverseCompleteParams:["param1", "param2"]});</code>
* To self-reference the timeline instance itself in one of the parameters, use <code>"{self}"</code>,
* like: <code>onReverseCompleteParams:["{self}", "param2"]</code></li>
*
* <li><strong> autoRemoveChildren </strong>:<em> Boolean</em> -
* If <code>autoRemoveChildren</code> is set to <code>true</code>, as soon as child
* tweens/timelines complete, they will automatically get killed/removed. This is normally
* undesireable because it prevents going backwards in time (like if you want to
* <code>reverse()</code> or set the <code>progress</code> lower, etc.). It can, however,
* improve speed and memory management. The root timelines use <code>autoRemoveChildren:true</code>.</li>
*
* <li><strong> smoothChildTiming </strong>:<em> Boolean</em> -
* Controls whether or not child tweens/timelines are repositioned automatically
* (changing their <code>startTime</code>) in order to maintain smooth playback when
* properties are changed on-the-fly. For example, imagine that the timeline's playhead is
* on a child tween that is 75% complete, moving mc.x from 0 to 100 and then that tween's
* <code>reverse()</code> method is called. If <code>smoothChildTiming</code> is <code>false</code>
* (the default except for the root timelines), the tween would flip in place, keeping its
* <code>startTime</code> consistent. Therefore the playhead of the timeline would now be
* at the tween's 25% completion point instead of 75%. Remember, the timeline's playhead
* position and direction are unaffected by child tween/timeline changes. mc.x would jump
* from 75 to 25, but the tween's position in the timeline would remain consistent. However,
* if <code>smoothChildTiming</code> is <code>true</code>, that child tween's
* <code>startTime</code> would be adjusted so that the timeline's playhead intersects
* with the same spot on the tween (75% complete) as it had immediately before
* <code>reverse()</code> was called, thus playback appears perfectly smooth. mc.x
* would still be 75 and it would continue from there as the playhead moves on, but
* since the tween is reversed now mc.x will travel back towards 0 instead of 100.
* Ultimately it's a decision between prioritizing smooth on-the-fly playback
* (<code>true</code>) or consistent position(s) of child tweens/timelines
* (<code>false</code>).
*
* Some examples of on-the-fly changes to child tweens/timelines that could cause their
* <code>startTime</code> to change when <code>smoothChildTiming</code> is <code>true</code>
* are: <code>reversed, timeScale, progress, totalProgress, time, totalTime, delay, pause,
* resume, duration,</code> and <code>totalDuration</code>.</li>
*
* <li><strong> repeat </strong>:<em> Number</em> -
* Number of times that the timeline should repeat after its first iteration. For example,
* if <code>repeat</code> is 1, the timeline will play a total of twice (the initial play
* plus 1 repeat). To repeat indefinitely, use -1. <code>repeat</code> should always be an integer.</li>
*
* <li><strong> repeatDelay </strong>:<em> Number</em> -
* Amount of time in seconds (or frames for frames-based timelines) between repeats. For example,
* if <code>repeat</code> is 2 and <code>repeatDelay</code> is 1, the timeline will play initially,
* then wait for 1 second before it repeats, then play again, then wait 1 second again before
* doing its final repeat.</li>
*
* <li><strong> yoyo </strong>:<em> Boolean</em> -
* If <code>true</code>, every other <code>repeat</code> cycle will run in the opposite
* direction so that the timeline appears to go back and forth (forward then backward).
* This has no affect on the "<code>reversed</code>" property though. So if <code>repeat</code>
* is 2 and <code>yoyo</code> is <code>false</code>, it will look like:
* start - 1 - 2 - 3 - 1 - 2 - 3 - 1 - 2 - 3 - end. But if <code>yoyo</code> is <code>true</code>,
* it will look like: start - 1 - 2 - 3 - 3 - 2 - 1 - 1 - 2 - 3 - end.</li>
*
* <li><strong> onRepeat </strong>:<em> Function</em> -
* A function that should be called each time the timeline repeats</li>
*
* <li><strong> onRepeatParams </strong>:<em> Array</em> -
* An Array of parameters to pass the onRepeat function. For example,
* <code>new TimelineMax({repeat:3, onRepeat:myFunction, onRepeatParams:[mc, "param2"]});</code>
* To self-reference the timeline instance itself in one of the parameters, use <code>"{self}"</code>,
* like: <code>onRepeatParams:["{self}", "param2"]</code></li>
*
* <li><strong> onStartListener </strong>:<em> Function</em> (AS3 only) -
* A function that should be called (and passed an event parameter) when the timeline begins
* (when its <code>totalTime</code> changes from 0 to some other value which can happen more
* than once if the timeline is restarted multiple times). Identical to <code>onStart</code> except
* that the function will always be passed an event parameter whose <code>target</code> property points
* to the timeline. It's the same as doing <code>myTimeline.addEventListener("start", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onStart</code>.</li>
*
* <li><strong> onUpdateListener </strong>:<em> Function</em> (AS3 only) -
* A function that should be called (and passed an event parameter) each time the timeline updates
* (on every frame while the timeline is active). Identical to <code>onUpdate</code> except
* that the function will always be passed an event parameter whose <code>target</code> property points
* to the timeline. It's the same as doing <code>myTimeline.addEventListener("update", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onUpdate</code>.</li>
*
* <li><strong> onCompleteListener </strong>:<em> Function</em> (AS3 only) -
* A function that should be called (and passed an event parameter) each time the timeline completes.
* Identical to <code>onComplete</code> except that the function will always be passed an event
* parameter whose <code>target</code> property points to the timeline. It's the same as doing
* <code>myTimeline.addEventListener("complete", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onComplete</code>.</li>
*
* <li><strong> onReverseCompleteListener </strong>:<em> Function</em> (AS3 only) -
* A function that should be called (and passed an event parameter) each time the timeline has reached
* its beginning again from the reverse direction. For example, if <code>reverse()</code> is called
* the timeline will move back towards its beginning and when its <code>totalTime</code> reaches 0,
* <code>onReverseCompleteListener</code> will be called. This can also happen if the timeline is placed
* in another TimelineLite or TimelineMax instance that gets reversed and plays the timeline backwards to
* (or past) the beginning. Identical to <code>onReverseComplete</code> except that the function
* will always be passed an event parameter whose <code>target</code> property points to the timeline.
* It's the same as doing <code>myTimeline.addEventListener("reverseComplete", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onReverseComplete</code>.</li>
*
* <li><strong> onRepeatListener </strong>:<em> Function</em> (AS3 only) -
* A function that should be called (and passed an event parameter) each time the timeline repeats.
* Identical to <code>onRepeat</code> except that the function will always be passed an event
* parameter whose <code>target</code> property points to the timeline. It's the same as doing
* <code>myTimeline.addEventListener("repeat", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onRepeat</code>.</li>
*
* </ul>
*
* @example Sample code:<listing version="3.0">
//create the timeline that repeats 3 times with 1 second between each repeat and then calls myFunction() when it completes
var tl = new TimelineMax({repeat:3, repeatDelay:1, onComplete:myFunction});
//add a tween
tl.add( new TweenLite(mc, 1, {x:200, y:100}) );
//add another tween at the end of the timeline (makes sequencing easy)
tl.add( new TweenLite(mc, 0.5, {alpha:0}) );
//append a tween using the convenience method (shorter syntax) and offset it by 0.5 seconds
tl.to(mc, 1, {rotation:30}, "+=0.5");
//reverse anytime
tl.reverse();
//Add a "spin" label 3-seconds into the timeline
tl.addLabel("spin", 3);
//insert a rotation tween at the "spin" label (you could also define the insertion point as the time instead of a label)
tl.add( new TweenLite(mc, 2, {rotation:"360"}), "spin");
//go to the "spin" label and play the timeline from there
tl.play("spin");
//nest another TimelineMax inside your timeline...
var nested = new TimelineMax();
nested.to(mc2, 1, {x:200}));
tl.add(nested);
</listing>
*
* <p><strong>Copyright 2008-2013, GreenSock. All rights reserved.</strong> This work is subject to the terms in <a href="http://www.greensock.com/terms_of_use.html">http://www.greensock.com/terms_of_use.html</a> or for <a href="http://www.greensock.com/club/">Club GreenSock</a> members, the software agreement that was issued with the membership.</p>
*
* @author Jack Doyle, jack@greensock.com
*
**/
public class TimelineMax extends TimelineLite implements IEventDispatcher {
/** @private **/
public static const version:String = "12.0.3";
/** @private **/
protected static var _listenerLookup:Object = {onCompleteListener:TweenEvent.COMPLETE, onUpdateListener:TweenEvent.UPDATE, onStartListener:TweenEvent.START, onRepeatListener:TweenEvent.REPEAT, onReverseCompleteListener:TweenEvent.REVERSE_COMPLETE};
/** @private **/
protected static var _easeNone:Ease = new Ease(null, null, 1, 0);
/** @private **/
protected var _repeat:int;
/** @private **/
protected var _repeatDelay:Number;
/** @private **/
protected var _cycle:int = 0;
/** @private **/
protected var _locked:Boolean;
/** @private **/
protected var _dispatcher:EventDispatcher;
/** @private **/
protected var _hasUpdateListener:Boolean;
/**
* @private
* Works in conjunction with the repeat property, determining the behavior of each cycle; when <code>yoyo</code> is true,
* the timeline will go back and forth, appearing to reverse every other cycle (this has no affect on the <code>reversed</code> property though).
* So if repeat is 2 and <code>yoyo</code> is false, it will look like: start - 1 - 2 - 3 - 1 - 2 - 3 - 1 - 2 - 3 - end.
* But if repeat is 2 and <code>yoyo</code> is true, it will look like: start - 1 - 2 - 3 - 3 - 2 - 1 - 1 - 2 - 3 - end.
**/
protected var _yoyo:Boolean;
/**
* Constructor.
*
* <p><strong>SPECIAL PROPERTIES</strong></p>
* <p>The following special properties may be passed in via the constructor's vars parameter, like
* <code>new TimelineMax({paused:true, onComplete:myFunction, repeat:2, yoyo:true})</code> </p>
*
* <ul>
* <li><strong> delay </strong>:<em> Number</em> -
* Amount of delay in seconds (or frames for frames-based tweens) before the timeline should begin.</li>
*
* <li><strong> paused </strong>:<em> Boolean</em> -
* If <code>true</code>, the timeline will pause itself immediately upon creation (by default,
* timelines automatically begin playing immediately). If you plan to create a TimelineMax and
* then populate it later (after one or more frames elapse), it is typically best to set
* <code>paused:true</code> and then <code>play()</code> after you populate it.</li>
*
* <li><strong> onComplete </strong>:<em> Function</em> -
* A function that should be called when the timeline has completed</li>
*
* <li><strong> onCompleteParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onComplete</code> function. For example,
* <code>new TimelineMax({onComplete:myFunction, onCompleteParams:["param1", "param2"]});</code></li>
*
* <li><strong> useFrames </strong>:<em> Boolean</em> -
* If <code>useFrames</code> is <code>true</code>, the timelines's timing will be
* based on frames instead of seconds because it is intially added to the root
* frames-based timeline. This causes both its <code>duration</code>
* and <code>delay</code> to be based on frames. An animations's timing mode is
* always determined by its parent <code>timeline</code>.</li>
*
* <li><strong> tweens </strong>:<em> Array</em> -
* To immediately insert several tweens into the timeline, use the <code>tweens</code>
* special property to pass in an Array of TweenLite/TweenMax/TimelineLite/TimelineMax
* instances. You can use this in conjunction with the <code>align</code> and
* <code>stagger</code> special properties to set up complex sequences with minimal code.
* These values simply get passed to the <code>add()</code> method.</li>
*
* <li><strong> align </strong>:<em> String</em> -
* Only used in conjunction with the <code>tweens</code> special property when multiple
* tweens are to be inserted immediately. The value simply gets passed to the
* <code>add()</code> method. The default is <code>"normal"</code>.
* Options are:
* <ul>
* <li><strong><code>"sequence"</code></strong>: aligns the tweens one-after-the-other in a sequence</li>
* <li><strong><code>"start"</code></strong>: aligns the start times of all of the tweens (ignores delays)</li>
* <li><strong><code>"normal"</code></strong>: aligns the start times of all the tweens (honors delays)</li>
* </ul>
* The <code>align</code> special property does <strong>not</strong> force all child
* tweens/timelines to maintain relative positioning, so for example, if you use
* <code>"sequence"</code> and then later change the duration of one of the nested tweens,
* it does <strong>not</strong> force all subsequent timelines to change their position.
* The <code>align</code> special property only affects the alignment of the tweens that are
* initially placed into the timeline through the <code>tweens</code> special property of
* the <code>vars</code> object.</li>
*
* <li><strong> stagger </strong>:<em> Number</em> -
* Only used in conjunction with the <code>tweens</code> special property when multiple
* tweens are to be inserted immediately. It staggers the tweens by a set amount of time
* in seconds (or in frames if <code>useFrames</code> is true). For example, if the
* stagger value is 0.5 and the "align" property is set to <code>"start"</code>, the
* second tween will start 0.5 seconds after the first one starts, then 0.5 seconds
* later the third one will start, etc. If the align property is <code>"sequence"</code>,
* there would be 0.5 seconds added between each tween. This value simply gets
* passed to the <code>add()</code> method. Default is 0.</li>
*
* <li><strong> onStart </strong>:<em> Function</em> -
* A function that should be called when the timeline begins (when its <code>time</code>
* changes from 0 to some other value which can happen more than once if the
* timeline is restarted multiple times).</li>
*
* <li><strong> onStartParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onStart</code> function. For example,
* <code>new TimelineMax({onStart:myFunction, onStartParams:["param1", "param2"]});</code></li>
*
* <li><strong> onUpdate </strong>:<em> Function</em> -
* A function that should be called every time the timeline updates
* (on every frame while the timeline is active)</li>
*
* <li><strong> onUpdateParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onUpdate</code> function. For example,
* <code>new TimelineMax({onUpdate:myFunction, onUpdateParams:["param1", "param2"]});</code></li>
*
* <li><strong> onReverseComplete </strong>:<em> Function</em> -
* A function that should be called when the timeline has reached its beginning again from the
* reverse direction. For example, if <code>reverse()</code> is called, the timeline will move
* back towards its beginning and when its <code>time</code> reaches 0, <code>onReverseComplete</code>
* will be called. This can also happen if the timeline is placed in a TimelineLite or TimelineMax
* instance that gets reversed and plays the timeline backwards to (or past) the beginning.</li>
*
* <li><strong> onReverseCompleteParams </strong>:<em> Array</em> -
* An Array of parameters to pass the <code>onReverseComplete</code> function. For example,
* <code>new TimelineMax({onReverseComplete:myFunction, onReverseCompleteParams:["param1", "param2"]});</code></li>
*
* <li><strong> autoRemoveChildren </strong>:<em> Boolean</em> -
* If <code>autoRemoveChildren</code> is set to <code>true</code>, as soon as child
* tweens/timelines complete, they will automatically get killed/removed. This is normally
* undesireable because it prevents going backwards in time (like if you want to
* <code>reverse()</code> or set the <code>progress</code> lower, etc.). It can, however,
* improve speed and memory management. The root timelines use <code>autoRemoveChildren:true</code>.</li>
*
* <li><strong> smoothChildTiming </strong>:<em> Boolean</em> -
* Controls whether or not child tweens/timelines are repositioned automatically
* (changing their <code>startTime</code>) in order to maintain smooth playback when
* properties are changed on-the-fly. For example, imagine that the timeline's playhead is
* on a child tween that is 75% complete, moving mc.x from 0 to 100 and then that tween's
* <code>reverse()</code> method is called. If <code>smoothChildTiming</code> is <code>false</code>
* (the default except for the root timelines), the tween would flip in place, keeping its
* <code>startTime</code> consistent. Therefore the playhead of the timeline would now be
* at the tween's 25% completion point instead of 75%. Remember, the timeline's playhead
* position and direction are unaffected by child tween/timeline changes. mc.x would jump
* from 75 to 25, but the tween's position in the timeline would remain consistent. However,
* if <code>smoothChildTiming</code> is <code>true</code>, that child tween's
* <code>startTime</code> would be adjusted so that the timeline's playhead intersects
* with the same spot on the tween (75% complete) as it had immediately before
* <code>reverse()</code> was called, thus playback appears perfectly smooth. mc.x
* would still be 75 and it would continue from there as the playhead moves on, but
* since the tween is reversed now mc.x will travel back towards 0 instead of 100.
* Ultimately it's a decision between prioritizing smooth on-the-fly playback
* (<code>true</code>) or consistent position(s) of child tweens/timelines
* (<code>false</code>).
*
* Some examples of on-the-fly changes to child tweens/timelines that could cause their
* <code>startTime</code> to change when <code>smoothChildTiming</code> is <code>true</code>
* are: <code>reversed, timeScale, progress, totalProgress, time, totalTime, delay, pause,
* resume, duration,</code> and <code>totalDuration</code>.</li>
*
* <li><strong> repeat </strong>:<em> Number</em> -
* Number of times that the timeline should repeat after its first iteration. For example,
* if <code>repeat</code> is 1, the timeline will play a total of twice (the initial play
* plus 1 repeat). To repeat indefinitely, use -1. <code>repeat</code> should always be an integer.</li>
*
* <li><strong> repeatDelay </strong>:<em> Number</em> -
* Amount of time in seconds (or frames for frames-based timelines) between repeats. For example,
* if <code>repeat</code> is 2 and <code>repeatDelay</code> is 1, the timeline will play initially,
* then wait for 1 second before it repeats, then play again, then wait 1 second again before
* doing its final repeat.</li>
*
* <li><strong> yoyo </strong>:<em> Boolean</em> -
* If <code>true</code>, every other <code>repeat</code> cycle will run in the opposite
* direction so that the timeline appears to go back and forth (forward then backward).
* This has no affect on the "<code>reversed</code>" property though. So if <code>repeat</code>
* is 2 and <code>yoyo</code> is <code>false</code>, it will look like:
* start - 1 - 2 - 3 - 1 - 2 - 3 - 1 - 2 - 3 - end. But if <code>yoyo</code> is <code>true</code>,
* it will look like: start - 1 - 2 - 3 - 3 - 2 - 1 - 1 - 2 - 3 - end.</li>
*
* <li><strong> onRepeat </strong>:<em> Function</em> -
* A function that should be called each time the timeline repeats</li>
*
* <li><strong> onRepeatParams </strong>:<em> Array</em> -
* An Array of parameters to pass the onRepeat function. For example,
* <code>new TimelineMax({repeat:3, onRepeat:myFunction, onRepeatParams:[mc, "param2"]});</code></li>
*
* <li><strong> onStartListener </strong>:<em> Function</em> -
* A function that should be called (and passed an event parameter) when the timeline begins
* (when its <code>totalTime</code> changes from 0 to some other value which can happen more
* than once if the timeline is restarted multiple times). Identical to <code>onStart</code> except
* that the function will always be passed an event parameter whose <code>target</code> property points
* to the timeline. It's the same as doing <code>myTimeline.addEventListener("start", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onStart</code>.</li>
*
* <li><strong> onUpdateListener </strong>:<em> Function</em> -
* A function that should be called (and passed an event parameter) each time the timeline updates
* (on every frame while the timeline is active). Identical to <code>onUpdate</code> except
* that the function will always be passed an event parameter whose <code>target</code> property points
* to the timeline. It's the same as doing <code>myTimeline.addEventListener("update", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onUpdate</code>.</li>
*
* <li><strong> onCompleteListener </strong>:<em> Function</em> -
* A function that should be called (and passed an event parameter) each time the timeline completes.
* Identical to <code>onComplete</code> except that the function will always be passed an event
* parameter whose <code>target</code> property points to the timeline. It's the same as doing
* <code>myTimeline.addEventListener("complete", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onComplete</code>.</li>
*
* <li><strong> onReverseCompleteListener </strong>:<em> Function</em> -
* A function that should be called (and passed an event parameter) each time the timeline has reached
* its beginning again from the reverse direction. For example, if <code>reverse()</code> is called
* the timeline will move back towards its beginning and when its <code>totalTime</code> reaches 0,
* <code>onReverseCompleteListener</code> will be called. This can also happen if the timeline is placed
* in another TimelineLite or TimelineMax instance that gets reversed and plays the timeline backwards to
* (or past) the beginning. Identical to <code>onReverseComplete</code> except that the function
* will always be passed an event parameter whose <code>target</code> property points to the timeline.
* It's the same as doing <code>myTimeline.addEventListener("reverseComplete", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onReverseComplete</code>.</li>
*
* <li><strong> onRepeatListener </strong>:<em> Function</em> -
* A function that should be called (and passed an event parameter) each time the timeline repeats.
* Identical to <code>onRepeat</code> except that the function will always be passed an event
* parameter whose <code>target</code> property points to the timeline. It's the same as doing
* <code>myTimeline.addEventListener("repeat", myFunction);</code>.
* Unless you need the event parameter, it's better/faster to use <code>onRepeat</code>.</li>
*
* </ul>
*
* @param vars optionally pass in special properties like useFrames, onComplete, onCompleteParams, onUpdate, onUpdateParams, onStart, onStartParams, tweens, align, stagger, delay, autoRemoveChildren, onCompleteListener, onStartListener, onUpdateListener, repeat, repeatDelay, and/or yoyo.
*/
public function TimelineMax(vars:Object=null) {
super(vars);
_repeat = this.vars.repeat || 0;
_repeatDelay = this.vars.repeatDelay || 0;
_yoyo = (this.vars.yoyo == true);
_dirty = true;
if (this.vars.onCompleteListener || this.vars.onUpdateListener || this.vars.onStartListener || this.vars.onRepeatListener || this.vars.onReverseCompleteListener) {
_initDispatcher();
}
}
/** @inheritDoc **/
override public function invalidate():* {
_yoyo = Boolean(this.vars.yoyo == true);
_repeat = this.vars.repeat || 0;
_repeatDelay = this.vars.repeatDelay || 0;
_hasUpdateListener = false;
_initDispatcher();
_uncache(true);
return super.invalidate();
}
/**
* Inserts a callback at a particular position. The callback is technically considered a
* zero-duration tween, so if you <code>getChildren()</code> there will be a tween returned for each callback.
* You can discern a callback from other tweens by the fact that its target is a function matching
* its <code>vars.onComplete</code> and its <code>duration</code> is zero.
*
* <p>If your goal is to append the callback to the end of the timeline, it would be easier
* (more concise) to use the <code>call()</code> method. Technically the <code>add()</code> method
* can accommodate adding a callback too (like <code>myTimeline.add(myFunction, 2)</code>
* or <code>myTimeline.add(myFunction, "+=2")</code>) but <code>add()</code> doesn't accommodate parameters.</p>
*
* <p><strong>JavaScript and AS2 note:</strong> - Due to the way JavaScript and AS2 don't
* maintain scope (what "<code>this</code>" refers to, or the context) in function calls,
* it can be useful to define the scope specifically. Therefore, in the JavaScript and AS2
* versions accept an extra (4th) parameter for <code>scope</code>.</p>
*
* @param function The function to be called
* @param position The time in seconds (or frames for frames-based timelines) or label at which the callback should be inserted. For example, <code>myTimeline.addCallback(myFunction, 3)</code> would call myFunction() 3 seconds into the timeline, and <code>myTimeline.addCallback(myFunction, "myLabel")</code> would call it at the "myLabel" label. <code>myTimeline.addCallback(myFunction, "+=2")</code> would insert the callback 2 seconds after the end of the timeline.
* @param params An Array of parameters to pass the callback
* @return self (makes chaining easier)
*
* @see #call()
* @see #add()
* @see #removeCallback()
*/
public function addCallback(callback:Function, position:*, params:Array=null):TimelineMax {
return add( TweenLite.delayedCall(0, callback, params), position) as TimelineMax;
}
/**
* Removes a callback. If the <code>position</code> parameter
* is null, all callbacks of that function are removed from the timeline.
*
* @param function callback function to be removed
* @param position the time in seconds (or frames for frames-based timelines) or label from which the callback should be removed. For example, <code>myTimeline.removeCallback(myFunction, 3)</code> would remove the callback from 3-seconds into the timeline, and <code>myTimeline.removeCallback(myFunction, "myLabel")</code> would remove it from the "myLabel" label, and <code>myTimeline.removeCallback(myFunction, null)</code> would remove ALL callbacks of that function regardless of where they are on the timeline.
* @return self (makes chaining easier)
*
* @see #addCallback()
* @see #call()
* @see #killTweensOf()
*/
public function removeCallback(callback:Function, position:*=null):TimelineMax {
if (position == null) {
_kill(null, callback);
} else {
var a:Array = getTweensOf(callback, false),
i:int = a.length,
time:Number = _parseTimeOrLabel(position);
while (--i > -1) {
if (a[i]._startTime === time) {
a[i]._enabled(false, false);
}
}
}
return this;
}
/**
* Creates a linear tween that essentially scrubs the playhead to a particular time or label and
* then stops. For example, to make the TimelineMax play to the "myLabel2" label, simply do:
*
* <p><code>
* myTimeline.tweenTo("myLabel2");
* </code></p>
*
* <p>If you want advanced control over the tween, like adding an onComplete or changing the ease or
* adding a delay, just pass in a <code>vars</code> object with the appropriate properties. For example,
* to tween to the 5-second point on the timeline and then call a function named <code>myFunction</code>
* and pass in a parameter that's references this TimelineMax and use a <code>Strong.easeOut</code> ease, you'd do:</p>
*
* <p><code>
* myTimeline.tweenTo(5, {onComplete:myFunction, onCompleteParams:[myTimeline], ease:Strong.easeOut});
* </code></p>
*
* <p>Remember, this method simply creates a TweenLite instance that pauses the timeline and then tweens
* the <code>time()</code> of the timeline. So you can store a reference to that tween if you want, and
* you can kill() it anytime. Also note that <code>tweenTo()</code> does <b>NOT</b> affect the timeline's
* <code>reversed</code> state. So if your timeline is oriented normally (not reversed) and you tween to
* a time/label that precedes the current time, it will appear to go backwards but the <code>reversed</code>
* state will <b>not</b> change to <code>true</code>. Also note that <code>tweenTo()</code>
* pauses the timeline immediately before tweening its <code>time()</code>, and it does not automatically
* resume after the tween completes. If you need to resume playback, you could always use an onComplete
* to call the timeline's <code>resume()</code> method.</p>
*
* <p>If you plan to sequence multiple playhead tweens one-after-the-other, it is typically better to use
* <code>tweenFromTo()</code> so that you can define the starting point and ending point, allowing the
* duration to be accurately determined immediately.</p>
*
* @param position The destination time in seconds (or frame if the timeline is frames-based) or label to which the timeline should play. For example, <code>myTimeline.tweenTo(5)</code> would play from wherever the timeline is currently to the 5-second point whereas <code>myTimeline.tweenTo("myLabel")</code> would play to wherever "myLabel" is on the timeline.
* @param vars An optional vars object that will be passed to the TweenLite instance. This allows you to define an onComplete, ease, delay, or any other TweenLite special property.
* @return A TweenLite instance that handles tweening the timeline to the desired time/label.
*
* @see #tweenFromTo()
* @see #seek()
*/
public function tweenTo(position:*, vars:Object=null):TweenLite {
vars = vars || {};
var copy:Object = {ease:_easeNone, overwrite:2, useFrames:usesFrames(), immediateRender:false};
for (var p:String in vars) {
copy[p] = vars[p];
}
copy.time = _parseTimeOrLabel(position);
var t:TweenLite = new TweenLite(this, (Math.abs(Number(copy.time) - _time) / _timeScale) || 0.001, copy);
copy.onStart = function():void {
t.target.paused(true);
if (t.vars.time != t.target.time()) { //don't make the duration zero - if it's supposed to be zero, don't worry because it's already initting the tween and will complete immediately, effectively making the duration zero anyway. If we make duration zero, the tween won't run at all.
t.duration( Math.abs( t.vars.time - t.target.time()) / t.target._timeScale );
}
if (vars.onStart) { //in case the user had an onStart in the vars - we don't want to overwrite it.
vars.onStart.apply(null, vars.onStartParams);
}
}
return t;
}
/**
* Creates a linear tween that essentially scrubs the playhead from a particular time or label
* to another time or label and then stops. If you plan to sequence multiple playhead tweens
* one-after-the-other, <code>tweenFromTo()</code> is better to use than <code>tweenTo()</code>
* because it allows the duration to be determined immediately, ensuring that subsequent tweens
* that are appended to a sequence are positioned appropriately. For example, to make the
* TimelineMax play from the label "myLabel1" to the "myLabel2" label, and then from "myLabel2"
* back to the beginning (a time of 0), simply do:
*
* <listing version="3.0">
var tl:TimelineMax = new TimelineMax();
tl.add( myTimeline.tweenFromTo("myLabel1", "myLabel2") );
tl.add( myTimeline.tweenFromTo("myLabel2", 0) );
</listing>
*
* <p>If you want advanced control over the tween, like adding an onComplete or changing the ease
* or adding a delay, just pass in a vars object with the appropriate properties. For example,
* to tween from the start (0) to the 5-second point on the timeline and then call a function
* named <code>myFunction</code> and pass in a parameter that references this TimelineMax and
* use a <code>Strong.easeOut</code> ease, you'd do: </p>
*
* <p><code>
* myTimeline.tweenFromTo(0, 5, {onComplete:myFunction, onCompleteParams:[myTimeline], ease:Strong.easeOut});
* </code></p>
*
* <p>Remember, this method simply creates a TweenLite instance that tweens the <code>time()</code>
* of your timeline. So you can store a reference to that tween if you want, and you can <code>kill()</code>
* it anytime. Also note that <code>tweenFromTo()</code> does <b>NOT</b> affect the timeline's
* <code>reversed</code> property. So if your timeline is oriented normally (not reversed) and you
* tween to a time/label that precedes the current time, it will appear to go backwards but the
* <code>reversed</code> property will <b>not</b> change to <code>true</code>. Also note that
* <code>tweenFromTo()</code> pauses the timeline immediately before tweening its <code>time()</code>,
* and it does not automatically resume after the tween completes. If you need to resume playback,
* you can always use an onComplete to call the <code>resume()</code> method.</p>
*
* @param fromPosition The beginning time in seconds (or frame if the timeline is frames-based) or label from which the timeline should play. For example, <code>myTimeline.tweenTo(0, 5)</code> would play from 0 (the beginning) to the 5-second point whereas <code>myTimeline.tweenFromTo("myLabel1", "myLabel2")</code> would play from "myLabel1" to "myLabel2".
* @param toPosition The destination time in seconds (or frame if the timeline is frames-based) or label to which the timeline should play. For example, <code>myTimeline.tweenTo(0, 5)</code> would play from 0 (the beginning) to the 5-second point whereas <code>myTimeline.tweenFromTo("myLabel1", "myLabel2")</code> would play from "myLabel1" to "myLabel2".
* @param vars An optional vars object that will be passed to the TweenLite instance. This allows you to define an onComplete, ease, delay, or any other TweenLite special property. onInit is the only special property that is not available (<code>tweenFromTo()</code> sets it internally)
* @return TweenLite instance that handles tweening the timeline between the desired times/labels.
*
* @see #tweenTo()
* @see #seek()
*/
public function tweenFromTo(fromPosition:*, toPosition:*, vars:Object=null):TweenLite {
vars = vars || {};
vars.startAt = {time:_parseTimeOrLabel(fromPosition)};
var t:TweenLite = tweenTo(toPosition, vars);
return t.duration((Math.abs( t.vars.time - t.vars.startAt.time) / _timeScale) || 0.001) as TweenLite;
}
/** @private **/
override public function render(time:Number, suppressEvents:Boolean=false, force:Boolean=false):void {
if (_gc) {
_enabled(true, false);
}
_active = !_paused;
var totalDur:Number = (!_dirty) ? _totalDuration : totalDuration(),
prevTime:Number = _time,
prevTotalTime:Number = _totalTime,
prevStart:Number = _startTime,
prevTimeScale:Number = _timeScale,
prevRawPrevTime:Number = _rawPrevTime,
prevPaused:Boolean = _paused,
prevCycle:int = _cycle,
tween:Animation, isComplete:Boolean, next:Animation, dur:Number, callback:String;
if (time >= totalDur) {
if (!_locked) {
_totalTime = totalDur;
_cycle = _repeat;
}
if (!_reversed) if (!_hasPausedChild()) {
isComplete = true;
callback = "onComplete";
if (_duration == 0) if (time == 0 || _rawPrevTime < 0) if (_rawPrevTime != time) { //In order to accommodate zero-duration timelines, we must discern the momentum/direction of time in order to render values properly when the "playhead" goes past 0 in the forward direction or lands directly on it, and also when it moves past it in the backward direction (from a postitive time to a negative time).
force = true;
}
}
_rawPrevTime = time;
if (_yoyo && (_cycle & 1) != 0) {
_time = 0;
time = -0.000001; //to avoid occasional floating point rounding errors in Flash - sometimes child tweens/timelines were not being rendered at the very beginning (their progress might be 0.000000000001 instead of 0 because when Flash performed _time - tween._startTime, floating point errors would return a value that was SLIGHTLY off)
} else {
_time = _duration;
time = _duration + 0.000001; //to avoid occasional floating point rounding errors in Flash - sometimes child tweens/timelines were not being fully completed (their progress might be 0.999999999999998 instead of 1 because when Flash performed _time - tween._startTime, floating point errors would return a value that was SLIGHTLY off)
}
} else if (time <= 0) {
if (!_locked) {
_totalTime = _cycle = 0;
}
_time = 0;
if (prevTime != 0 || (_duration == 0 && _rawPrevTime > 0 && !_locked)) {
callback = "onReverseComplete";
isComplete = _reversed;
}
if (time < 0) {
_active = false;
if (_duration == 0) if (_rawPrevTime >= 0) { //zero-duration timelines are tricky because we must discern the momentum/direction of time in order to determine whether the starting values should be rendered or the ending values. If the "playhead" of its timeline goes past the zero-duration tween in the forward direction or lands directly on it, the end values should be rendered, but if the timeline's "playhead" moves past it in the backward direction (from a postitive time to a negative time), the starting values must be rendered.
force = true;
}
} else if (!_initted) {
force = true;
}
_rawPrevTime = time;
time = (_duration == 0) ? 0 : -0.000001; //to avoid occasional floating point rounding errors in Flash - sometimes child tweens/timelines were not being rendered at the very beginning (their progress might be 0.000000000001 instead of 0 because when Flash performed _time - tween._startTime, floating point errors would return a value that was SLIGHTLY off)
} else {
_time = _rawPrevTime = time;
if (!_locked) {
_totalTime = time;
if (_repeat != 0) {
var cycleDuration:Number = _duration + _repeatDelay;
_cycle = (_totalTime / cycleDuration) >> 0; //originally _totalTime % cycleDuration but floating point errors caused problems, so I normalized it. (4 % 0.8 should be 0 but Flash reports it as 0.79999999!)
if (_cycle !== 0) if (_cycle === _totalTime / cycleDuration) {
_cycle--; //otherwise when rendered exactly at the end time, it will act as though it is repeating (at the beginning)
}
_time = _totalTime - (_cycle * cycleDuration);
if (_yoyo) if ((_cycle & 1) != 0) {
_time = _duration - _time;
}
if (_time > _duration) {
_time = _duration;
time = _duration + 0.000001; //to avoid occasional floating point rounding errors in Flash - sometimes child tweens/timelines were not being fully completed (their progress might be 0.999999999999998 instead of 1 because when Flash performed _time - tween._startTime, floating point errors would return a value that was SLIGHTLY off)
} else if (_time < 0) {
_time = time = 0;
} else {
time = _time;
}
}
}
}
if (_cycle != prevCycle) if (!_locked) {
/*
make sure children at the end/beginning of the timeline are rendered properly. If, for example,
a 3-second long timeline rendered at 2.9 seconds previously, and now renders at 3.2 seconds (which
would get transated to 2.8 seconds if the timeline yoyos or 0.2 seconds if it just repeats), there
could be a callback or a short tween that's at 2.95 or 3 seconds in which wouldn't render. So
we need to push the timeline to the end (and/or beginning depending on its yoyo value). Also we must
ensure that zero-duration tweens at the very beginning or end of the TimelineMax work.
*/
var backwards:Boolean = (_yoyo && (prevCycle & 1) !== 0),
wrap:Boolean = (backwards == (_yoyo && (_cycle & 1) !== 0)),
recTotalTime:Number = _totalTime,
recCycle:int = _cycle,
recRawPrevTime:Number = _rawPrevTime,
recTime:Number = _time;
_totalTime = prevCycle * _duration;
if (_cycle < prevCycle) {
backwards = !backwards;
} else {
_totalTime += _duration;
}
_time = prevTime; //temporarily revert _time so that render() renders the children in the correct order. Without this, tweens won't rewind correctly. We could arhictect things in a "cleaner" way by splitting out the rendering queue into a separate method but for performance reasons, we kept it all inside this method.
_rawPrevTime = prevRawPrevTime;
_cycle = prevCycle;
_locked = true; //prevents changes to totalTime and skips repeat/yoyo behavior when we recursively call render()
prevTime = (backwards) ? 0 : _duration;
render(prevTime, suppressEvents, false);
if (!suppressEvents) if (!_gc) {
if (vars.onRepeat) {
vars.onRepeat.apply(null, vars.onRepeatParams);
}
if (_dispatcher) {
_dispatcher.dispatchEvent(new TweenEvent(TweenEvent.REPEAT));
}
}
if (wrap) {
prevTime = (backwards) ? _duration + 0.000001 : -0.000001;
render(prevTime, true, false);
}
_time = recTime;
_totalTime = recTotalTime;
_cycle = recCycle;
_rawPrevTime = recRawPrevTime;
_locked = false;
}
if (_time == prevTime && !force) {
if (prevTotalTime !== _totalTime) if (_onUpdate != null) if (!suppressEvents) { //so that onUpdate fires even during the repeatDelay - as long as the totalTime changed, we should trigger onUpdate.
_onUpdate.apply(vars.onUpdateScope || this, vars.onUpdateParams);
}
return;
} else if (!_initted) {
_initted = true;
}
if (prevTotalTime == 0) if (vars.onStart) if (_totalTime != 0) if (!suppressEvents) {
vars.onStart.apply(this, vars.onStartParams);
}
if (_time > prevTime) {
tween = _first;
while (tween) {
next = tween._next; //record it here because the value could change after rendering...
if (_paused && !prevPaused) { //in case a tween pauses the timeline when rendering
break;
} else if (tween._active || (tween._startTime <= _time && !tween._paused && !tween._gc)) {
if (!tween._reversed) {
tween.render((time - tween._startTime) * tween._timeScale, suppressEvents, force);
} else {
tween.render(((!tween._dirty) ? tween._totalDuration : tween.totalDuration()) - ((time - tween._startTime) * tween._timeScale), suppressEvents, force);
}
}
tween = next;
}
} else {
tween = _last;
while (tween) {
next = tween._prev; //record it here because the value could change after rendering...
if (_paused && !prevPaused) { //in case a tween pauses the timeline when rendering
break;
} else if (tween._active || (tween._startTime <= prevTime && !tween._paused && !tween._gc)) {
if (!tween._reversed) {
tween.render((time - tween._startTime) * tween._timeScale, suppressEvents, force);
} else {
tween.render(((!tween._dirty) ? tween._totalDuration : tween.totalDuration()) - ((time - tween._startTime) * tween._timeScale), suppressEvents, force);
}
}
tween = next;
}
}
if (_onUpdate != null) if (!suppressEvents) {
_onUpdate.apply(null, vars.onUpdateParams);
}
if (_hasUpdateListener) if (!suppressEvents) {
_dispatcher.dispatchEvent(new TweenEvent(TweenEvent.UPDATE));
}
if (callback) if (!_locked) if (!_gc) if (prevStart === _startTime || prevTimeScale != _timeScale) if (_time === 0 || totalDur >= totalDuration()) { //if one of the tweens that was rendered altered this timeline's startTime (like if an onComplete reversed the timeline), it probably isn't complete. If it is, don't worry, because whatever call altered the startTime would complete if it was necessary at the new time. The only exception is the timeScale property. Also check _gc because there's a chance that kill() could be called in an onUpdate
if (isComplete) {
if (_timeline.autoRemoveChildren) {
_enabled(false, false);
}
_active = false;
}
if (!suppressEvents) {
if (vars[callback]) {
vars[callback].apply(null, vars[callback + "Params"]);
}
if (_dispatcher) {
_dispatcher.dispatchEvent(new TweenEvent(((callback == "onComplete") ? TweenEvent.COMPLETE : TweenEvent.REVERSE_COMPLETE)));
}
}
}
}
/**
* Returns the tweens/timelines that are currently active in the timeline, meaning the timeline's
* playhead is positioned on the child tween/timeline and the child isn't paused.
*
* @param nested Determines whether or not tweens and/or timelines that are inside nested timelines should be returned. If you only want the "top level" tweens/timelines, set this to <code>false</code>.
* @param tweens Determines whether or not tweens (TweenLite and TweenMax instances) should be included in the results
* @param timelines Determines whether or not child timelines (TimelineLite and TimelineMax instances) should be included in the results
* @return an Array of active tweens/timelines
*/
public function getActive(nested:Boolean=true, tweens:Boolean=true, timelines:Boolean=false):Array {
var a:Array = [],
all:Array = getChildren(nested, tweens, timelines),
cnt:int = 0,
l:int = all.length,
i:int, tween:Animation;
for (i = 0; i < l; i++) {
tween = all[i];
//note: we cannot just check tween.active because timelines that contain paused children will continue to have "active" set to true even after the playhead passes their end point (technically a timeline can only be considered complete after all of its children have completed too, but paused tweens are...well...just waiting and until they're unpaused we don't know where their end point will be).
if (!tween._paused) if (tween._timeline._time >= tween._startTime) if (tween._timeline._time < tween._startTime + tween._totalDuration / tween._timeScale) if (!_getGlobalPaused(tween._timeline)) {
a[cnt++] = tween;