source: branches/fl5_instream/src/com/longtailvideo/jwplayer/utils/Animations.as @ 2040

Revision 2040, 3.3 KB checked in by pablo, 17 months ago (diff)

First pass at InStream API (644). Implements JavaScript API and Flash version

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