您的位置:首页 > 其它

Flex 图片自动播放(亦可手动控制)

2011-06-27 15:26 561 查看
   1. package {  
   2.     import flash.display.Sprite;  
   3.     import flash.text.*;  
   4.     import flash.system.System;  
   5.     import flash.events.MouseEvent;  
   6.     import flash.display.Stage;  
   7.     import flash.events.Event;  
   8.     import flash.display.Loader;  
   9.     import flash.net.URLLoader;  
  10.     import flash.net.URLRequest;  
  11.     import flash.filters.GlowFilter;  
  12.     import flash.utils.Timer;  
  13.     import flash.events.TimerEvent;  
  14.     import fl.controls.Button;  
  15.     import fl.controls.ComboBox;  
  16.     public class myPic extends Sprite {  
  17.         //定义属性  
  18.         private var _sprite:Sprite;//红色小方块  
  19.         //private var _rect:Sprite=new Sprite  ;  
  20.         private var _txt:TextField;//红色小方块上面的编号 标签  
  21.         private var _newTxt:TextField;  
  22.         private var _picTxt:TextField=new TextField  ;  
  23.         private var _loadTxt:TextField;  
  24.         private var _loader:Loader;  
  25.         private var _urlRequest:URLRequest;  
  26.         private var _urlLoader:URLLoader;  
  27.         private var _timer:Timer;  
  28.         private var _comBox:ComboBox;  
  29.         private var nextBtn:Button;  
  30.         private var prevBtn:Button;  
  31.         private var stopBtn:Button;  
  32.         private var playBtn:Button;  
  33.         //定义变量  
  34.         private var _picGlowFilter:GlowFilter=new GlowFilter(0x999999,1,3,3,3,2,false,false);  
  35.         private var _glowFilter:GlowFilter=new GlowFilter(0x000000,.8,2,2,4,1,false,false);  
  36.         private var _width:Number=160;//第一个红色小方块的x坐标  
  37.         private var _height:Number=290;//第一个红色小方块的y坐标  
  38.         private var _time:uint=1000;  
  39.         private var _num:uint=0;  
  40.         private var _txtAry:Array=["pic0","pic1","pic2","pic3","pic4","pic5","pic6","pic7"];//图片说明名字  
  41.         private var _picUrlAry:Array=["images/01.jpg","images/02.jpg","images/03.jpg","images/04.jpg","images/05.jpg","images/06.jpg","images/07.jpg","images/08.jpg"];  
  42.         public function myPic() {  
  43.             init();//初始化数据  
  44.             showCom();//显示组建  
  45.             showTxt();//显示文本  
  46.             loadPic(_num);//载入第一张图片  
  47.             getChildAt(0).filters=[_glowFilter];  
  48.             _picTxt.filters=null;  
  49.             //定义自动播放  
  50.             _timer=new Timer(_time,0);  
  51.             _timer.addEventListener("timer",timerHandler);  
  52.             _timer.start();  
  53.         }  
  54.         //=============绘制8个红色正方形,贴上标签号,并添加侦听=========================  
  55.         private function init():void {  
  56.             for (var i:uint=0; i<_picUrlAry.length; i++) {  
  57.                 _sprite=new Sprite  ;  
  58.                 _sprite.graphics.beginFill(0xff0044,1);  
  59.                 _sprite.graphics.drawRect(_width+i*25,_height,20,20);  
  60.                 _sprite.graphics.endFill();  
  61.                 _sprite.buttonMode=true;  
  62.                 _sprite.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);  
  63.                 _sprite.addEventListener(MouseEvent.MOUSE_UP,upHandler);  
  64.                 _txt=new TextField  ;  
  65.                 _txt.text=i.toString();  
  66.                 _txt.width=20;  
  67.                 _txt.height=20;  
  68.                 _txt.x=_width+i*25;  
  69.                 _txt.y=_height;  
  70.                 _txt.autoSize="center";  
  71.                 _txt.mouseEnabled=false;  
  72.                 _sprite.addChild(_txt);  
  73.                 addChild(_sprite);  
  74.             }  
  75.         }  
  76.         //================点击方格==========================================  
  77.         private function downHandler(e:MouseEvent):void {  
  78.             _timer.stop();  
  79.             getChildAt(_num+1).filters=null;  
  80.             _num=getChildIndex(getChildByName(e.target.name))-1;  
  81.             e.target.filters=[_glowFilter];  
  82.             loadPic(_num);  
  83.             showTxt();  
  84.         }  
  85.         private function upHandler(e:MouseEvent):void {  
  86.             _timer.start();  
  87.         }  
  88.         //===============显示组件========================================  
  89.         private function showCom() {  
  90.             _loadTxt=new TextField  ;  
  91.             _loadTxt.text="loading...";  
  92.             _loadTxt.x=200;  
  93.             _loadTxt.y=150;  
  94.             addChild(_loadTxt);  
  95.             nextBtn=new Button  ;  
  96.             nextBtn.label="下一页";  
  97.             nextBtn.move(330,330);  
  98.             addChild(nextBtn);  
  99.             nextBtn.addEventListener(MouseEvent.MOUSE_DOWN,onNextBtnDown,false,0,true);  
100.             nextBtn.addEventListener(MouseEvent.MOUSE_UP,onNextBtnUp,false,0,true);  
101.             prevBtn=new Button  ;  
102.             prevBtn.label="上一页";  
103.             prevBtn.move(100,330);  
104.             addChild(prevBtn);  
105.             prevBtn.addEventListener(MouseEvent.MOUSE_DOWN,onPrevBtnDown,false,0,true);  
106.             prevBtn.addEventListener(MouseEvent.MOUSE_UP,onPrevBtnUp,false,0,true);  
107.             stopBtn=new Button  ;  
108.             stopBtn.label="停止";  
109.             stopBtn.move(140,360);  
110.             addChild(stopBtn);  
111.             stopBtn.addEventListener(MouseEvent.CLICK,onStopBtnClicked,false,0,true);  
112.             playBtn=new Button  ;  
113.             playBtn.label="播放";  
114.             playBtn.move(260,360);  
115.             addChild(playBtn);  
116.             playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClicked,false,0,true);  
117.             _comBox=new ComboBox  ;  
118.             _comBox.addItem({data:1,label:"1000"});  
119.             _comBox.addItem({data:2,label:"3000"});  
120.             _comBox.addItem({data:3,label:"5000"});  
121.             _comBox.move(220,330);  
122.             addChild(_comBox);  
123.             _comBox.addEventListener(Event.CHANGE,changeHandler,false,0,true);  
124.         }  
125.         //==================================================================  
126.         //下一页  
127.         private function onNextBtnDown(e:MouseEvent):void {  
128.             getChildAt(_num+1).filters=null;  
129.             //判断当前图片是否是最后一张,如果是,则将图片索引号设置为0,即第一张图片  
130.             if (_num<_picUrlAry.length-1&&_num!=_picUrlAry.length-1) {  
131.                 _num++;  
132.             } else {  
133.                 _num=0;  
134.             }  
135.             loadPic(_num);  
136.             showTxt();  
137.             getChildAt(_num+1).filters=[_glowFilter];  
138.             _timer.stop();  
139.         }  
140.         private function onNextBtnUp(e:MouseEvent):void {  
141.             _timer.start();  
142.         }  
143.         //上一页;  
144.         private function onPrevBtnDown(e:MouseEvent):void {  
145.             getChildAt(_num+1).filters=null;  
146.             if (_num<_picUrlAry.length&&_num!=0) {  
147.                 _num--;  
148.             } else {  
149.                 _num=_picUrlAry.length-1;  
150.             }  
151.             loadPic(_num);  
152.             showTxt();  
153.             getChildAt(_num+1).filters=[_glowFilter];  
154.             _timer.stop();  
155.         }  
156.         private function onPrevBtnUp(e:MouseEvent):void {  
157.             _timer.start();  
158.         }  
159.         //控制自动播放时间。  
160.         private function changeHandler(e:Event):void {  
161.             _timer.removeEventListener("timer",timerHandler);  
162.             _time=e.target.selectedItem.label;  
163.             _timer=new Timer(_time,0);  
164.             _timer.addEventListener("timer",timerHandler);  
165.             _timer.start();  
166.         }  
167.         //停止自动播放  
168.         private function onStopBtnClicked(e:MouseEvent):void {  
169.             _timer.stop();  
170.         }  
171.         //开始自动播放  
172.         private function onPlayBtnClicked(e:MouseEvent):void {  
173.             _timer.start();  
174.         }  
175.         //==================================================================  
176.         //载入图片  
177.         private function loadPic(_num:uint):void {  
178.             _urlRequest=new URLRequest(_picUrlAry[_num]);  
179.             _loader=new Loader  ;  
180.             _loader.x=150;  
181.             _loader.y=50;  
182.             _loader.load(_urlRequest);  
183.             _loader.filters=[_picGlowFilter];  
184.             addChild(_loader);  
185.         }  
186.         //显示文本  
187.         private function showTxt():void {  
188.             _picTxt.text=_txtAry[_num];  
189.             _picTxt.x=240;  
190.             _picTxt.y=260;  
191.             _picTxt.width=50;  
192.             _picTxt.height=30;  
193.             _picTxt.autoSize="center";  
194.             _picTxt.selectable=false;  
195.             addChildAt(_picTxt,0);  
196.         }  
197.         //==================自动播放=======================================  
198.         private function timerHandler(e:TimerEvent):void {  
199.             getChildAt(_num+1).filters=null;  
200.             if (_num<_picUrlAry.length-1) {  
201.                 _num++;  
202.             } else {  
203.                 _num=0;  
204.             }  
205.             getChildAt(_num+1).filters=[_glowFilter];  
206.             loadPic(_num);  
207.             showTxt();  
208.         }  
209.     }  
210. }  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息