source: branches/jw6/src/flash/com/longtailvideo/jwplayer/utils/Animations.as @ 2196

Revision 2196, 2.9 KB checked in by pablo, 12 months ago (diff)
  • Removing SWF skins and V4 APIs from Flash player ([]);
  • Zip skin as default Flash player skin asset
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                /** The function to execute on enter_frame **/
28                private var frameHandler:Function;
29               
30                /** Constructor
31                 * @param tgt   The Movielip to animate.
32                 **/
33                public function Animations(tgt:MovieClip) {
34                        _tgt = tgt;
35                }
36               
37                /**
38                 * Fade function for MovieClip.
39                 *
40                 * @param end   The final alpha value.
41                 * @param spd   The amount of alpha change per frame.
42                 **/
43                public function fade(end:Number = 1, spd:Number = 0.25):void {
44                        _end = end;
45                        if (_tgt.alpha > _end) {
46                                _spd = -Math.abs(spd);
47                        } else {
48                                _spd = Math.abs(spd);
49                        }
50                        frameHandler = fadeHandler;
51                        _tgt.addEventListener(Event.ENTER_FRAME, frameHandler);
52                }
53               
54               
55                /** The fade enterframe function. **/
56                private function fadeHandler(evt:Event):void {
57                        if ((_tgt.alpha >= _end - _spd && _spd > 0) || (_tgt.alpha <= _end + _spd && _spd < 0)) {
58                                _tgt.removeEventListener(Event.ENTER_FRAME, frameHandler);
59                                _tgt.alpha = _end;
60                                if (_end == 0) {
61                                        _tgt.visible = false;
62                                }
63                                dispatchEvent(new Event(Event.COMPLETE));
64                        } else {
65                                _tgt.visible = true;
66                                _tgt.alpha += _spd;
67                        }
68                }
69               
70               
71                /**
72                 * Smoothly move a Movielip to a certain position.
73                 *
74                 * @param xps   The x destination.
75                 * @param yps   The y destination.
76                 * @param spd   The movement speed (1 - 2).
77                 **/
78                public function ease(xps:Number, yps:Number, spd:Number = 2):void {
79                        _spd = spd;
80                        if (!xps) {
81                                _xps = _tgt.x;
82                        } else {
83                                _xps = xps;
84                        }
85                        if (!yps) {
86                                _yps = _tgt.y;
87                        } else {
88                                _yps = yps;
89                        }
90                        frameHandler = easeHandler;
91                        _tgt.addEventListener(Event.ENTER_FRAME, easeHandler);
92                }
93               
94               
95                /** The ease enterframe function. **/
96                private function easeHandler(evt:Event):void {
97                        if (Math.abs(_tgt.x - _xps) < 1 && Math.abs(_tgt.y - _yps) < 1) {
98                                _tgt.removeEventListener(Event.ENTER_FRAME, frameHandler);
99                                _tgt.x = _xps;
100                                _tgt.y = _yps;
101                                dispatchEvent(new Event(Event.COMPLETE));
102                        } else {
103                                _tgt.x = _xps - (_xps - _tgt.x) / _spd;
104                                _tgt.y = _yps - (_yps - _tgt.y) / _spd;
105                        }
106                }
107
108                /** Stop executing the current animation **/
109                public function cancelAnimation():void {
110                        try {
111                                _tgt.removeEventListener(Event.ENTER_FRAME, frameHandler);
112                        } catch(e:Error) {}
113                }
114        }
115}
Note: See TracBrowser for help on using the repository browser.