source: trunk/fl5/src/com/longtailvideo/jwplayer/utils/Animations.as @ 1965

Revision 1965, 2.9 KB checked in by jeroen, 22 months ago (diff)

small bug that prevented the ease animation from working

Line 
1package com.longtailvideo.jwplayer.utils {
2        import flash.display.MovieClip;
3        import flash.events.Event;
4       
5       
6       
7        public class Animations {
8                /** Target MovieClip **/
9                private var _tgt:MovieClip;
10                /** Transition speed **/
11                private var _spd:Number;
12                /** Final Alpha **/
13                private var _end:Number;
14                /** X position **/
15                private var _xps:Number;
16                /** Y position **/
17                private var _yps:Number;
18                /** Text **/
19                private var _str:String;
20               
21                /** Constructor
22                 * @param tgt   The Movielip to animate.
23                 **/
24                public function Animations(tgt:MovieClip) {
25                        _tgt = tgt;
26                }
27               
28                /**
29                 * Fade function for MovieClip.
30                 *
31                 * @param end   The final alpha value.
32                 * @param spd   The amount of alpha change per frame.
33                 **/
34                public function fade(end:Number = 1, spd:Number = 0.25):void {
35                        _end = end;
36                        if (_tgt.alpha > _end) {
37                                _spd = -Math.abs(spd);
38                        } else {
39                                _spd = Math.abs(spd);
40                        }
41                        _tgt.addEventListener(Event.ENTER_FRAME, fadeHandler);
42                }
43               
44               
45                /** The fade enterframe function. **/
46                private function fadeHandler(evt:Event):void {
47                        if ((_tgt.alpha >= _end - _spd && _spd > 0) || (_tgt.alpha <= _end + _spd && _spd < 0)) {
48                                _tgt.removeEventListener(Event.ENTER_FRAME, fadeHandler);
49                                _tgt.alpha = _end;
50                                if (_end == 0) {
51                                        _tgt.visible = false;
52                                }
53                        } else {
54                                _tgt.visible = true;
55                                _tgt.alpha += _spd;
56                        }
57                }
58               
59               
60                /**
61                 * Smoothly move a Movielip to a certain position.
62                 *
63                 * @param xps   The x destination.
64                 * @param yps   The y destination.
65                 * @param spd   The movement speed (1 - 2).
66                 **/
67                public function ease(xps:Number, yps:Number, spd:Number = 2):void {
68                        _spd = spd;
69                        if (!xps) {
70                                _xps = _tgt.x;
71                        } else {
72                                _xps = xps;
73                        }
74                        if (!yps) {
75                                _yps = _tgt.y;
76                        } else {
77                                _yps = yps;
78                        }
79                        _tgt.addEventListener(Event.ENTER_FRAME, easeHandler);
80                }
81               
82               
83                /** The ease enterframe function. **/
84                private function easeHandler(evt:Event):void {
85                        if (Math.abs(_tgt.x - _xps) < 1 && Math.abs(_tgt.y - _yps) < 1) {
86                                _tgt.removeEventListener(Event.ENTER_FRAME, easeHandler);
87                                _tgt.x = _xps;
88                                _tgt.y = _yps;
89                        } else {
90                                _tgt.x = _xps - (_xps - _tgt.x) / _spd;
91                                _tgt.y = _yps - (_yps - _tgt.y) / _spd;
92                        }
93                }
94               
95               
96                /**
97                 * Typewrite text into a textfield.
98                 *
99                 * @param txt   The textstring to write.
100                 * @param spd   The speed of typing (1 - 2).
101                 **/
102                public function write(str:String, spd:Number = 1.5):void {
103                        _str = str;
104                        _spd = spd;
105                        _tgt.tf.text = '';
106                        _tgt.addEventListener(Event.ENTER_FRAME, writeHandler);
107                }
108               
109               
110                /** The write enterframe function. **/
111                private function writeHandler(evt:Event):void {
112                        var dif:Number = Math.floor((_str.length - _tgt.tf.text.length) / _spd);
113                        _tgt.tf.text = _str.substr(0, _str.length - dif);
114                        if (_tgt.tf.text == _str) {
115                                _tgt.tf.htmlText = _str;
116                                _tgt.removeEventListener(Event.ENTER_FRAME, easeHandler);
117                        }
118                }
119        }
120}
Note: See TracBrowser for help on using the repository browser.