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

package {
	import flash.display.Sprite;
	import flash.text.*;
	import flash.system.System;
	import flash.events.MouseEvent;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.display.Loader;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import fl.controls.Button;
	import fl.controls.ComboBox;

	public class myPic extends Sprite {
		//定义属性
		private var _sprite:Sprite;//红色小方块
		//private var _rect:Sprite=new Sprite  ;
		private var _txt:TextField;//红色小方块上面的编号 标签
		private var _newTxt:TextField;
		private var _picTxt:TextField=new TextField  ;
		private var _loadTxt:TextField;
		private var _loader:Loader;
		private var _urlRequest:URLRequest;
		private var _urlLoader:URLLoader;
		private var _timer:Timer;
		private var _comBox:ComboBox;
		private var nextBtn:Button;
		private var prevBtn:Button;
		private var stopBtn:Button;
		private var playBtn:Button;

		//定义变量
		private var _picGlowFilter:GlowFilter=new GlowFilter(0x999999,1,3,3,3,2,false,false);
		private var _glowFilter:GlowFilter=new GlowFilter(0x000000,.8,2,2,4,1,false,false);

		private var _width:Number=160;//第一个红色小方块的x坐标
		private var _height:Number=290;//第一个红色小方块的y坐标
		private var _time:uint=1000;
		private var _num:uint=0;
		private var _txtAry:Array=["pic0","pic1","pic2","pic3","pic4","pic5","pic6","pic7"];//图片说明名字
		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"];

		public function myPic() {
			init();//初始化数据

			showCom();//显示组建

			showTxt();//显示文本

			loadPic(_num);//载入第一张图片

			getChildAt(0).filters=[_glowFilter];
			_picTxt.filters=null;
			//定义自动播放
			_timer=new Timer(_time,0);
			_timer.addEventListener("timer",timerHandler);
			_timer.start();
		}

		//=============绘制8个红色正方形,贴上标签号,并添加侦听=========================

		private function init():void {
			for (var i:uint=0; i<_picUrlAry.length; i++) {
				_sprite=new Sprite  ;
				_sprite.graphics.beginFill(0xff0044,1);
				_sprite.graphics.drawRect(_width+i*25,_height,20,20);
				_sprite.graphics.endFill();
				_sprite.buttonMode=true;
				_sprite.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
				_sprite.addEventListener(MouseEvent.MOUSE_UP,upHandler);

				_txt=new TextField  ;
				_txt.text=i.toString();
				_txt.width=20;
				_txt.height=20;
				_txt.x=_width+i*25;
				_txt.y=_height;
				_txt.autoSize="center";
				_txt.mouseEnabled=false;
				_sprite.addChild(_txt);
				addChild(_sprite);
			}
		}

		//================点击方格==========================================

		private function downHandler(e:MouseEvent):void {
			_timer.stop();
			getChildAt(_num+1).filters=null;
			_num=getChildIndex(getChildByName(e.target.name))-1;
			e.target.filters=[_glowFilter];
			loadPic(_num);
			showTxt();
		}

		private function upHandler(e:MouseEvent):void {
			_timer.start();
		}

		//===============显示组件========================================

		private function showCom() {
			_loadTxt=new TextField  ;
			_loadTxt.text="loading...";
			_loadTxt.x=200;
			_loadTxt.y=150;
			addChild(_loadTxt);

			nextBtn=new Button  ;
			nextBtn.label="下一页";
			nextBtn.move(330,330);
			addChild(nextBtn);
			nextBtn.addEventListener(MouseEvent.MOUSE_DOWN,onNextBtnDown,false,0,true);
			nextBtn.addEventListener(MouseEvent.MOUSE_UP,onNextBtnUp,false,0,true);

			prevBtn=new Button  ;
			prevBtn.label="上一页";
			prevBtn.move(100,330);
			addChild(prevBtn);
			prevBtn.addEventListener(MouseEvent.MOUSE_DOWN,onPrevBtnDown,false,0,true);
			prevBtn.addEventListener(MouseEvent.MOUSE_UP,onPrevBtnUp,false,0,true);

			stopBtn=new Button  ;
			stopBtn.label="停止";
			stopBtn.move(140,360);
			addChild(stopBtn);
			stopBtn.addEventListener(MouseEvent.CLICK,onStopBtnClicked,false,0,true);

			playBtn=new Button  ;
			playBtn.label="播放";
			playBtn.move(260,360);
			addChild(playBtn);
			playBtn.addEventListener(MouseEvent.CLICK,onPlayBtnClicked,false,0,true);

			_comBox=new ComboBox  ;
			_comBox.addItem({data:1,label:"1000"});
			_comBox.addItem({data:2,label:"3000"});
			_comBox.addItem({data:3,label:"5000"});
			_comBox.move(220,330);
			addChild(_comBox);
			_comBox.addEventListener(Event.CHANGE,changeHandler,false,0,true);
		}

		//==================================================================

		//下一页
		private function onNextBtnDown(e:MouseEvent):void {
			getChildAt(_num+1).filters=null;

			//判断当前图片是否是最后一张,如果是,则将图片索引号设置为0,即第一张图片
			if (_num<_picUrlAry.length-1&&_num!=_picUrlAry.length-1) {
				_num++;
			} else {
				_num=0;
			}

			loadPic(_num);
			showTxt();
			getChildAt(_num+1).filters=[_glowFilter];
			_timer.stop();
		}

		private function onNextBtnUp(e:MouseEvent):void {
			_timer.start();
		}

		//上一页;
		private function onPrevBtnDown(e:MouseEvent):void {
			getChildAt(_num+1).filters=null;

			if (_num<_picUrlAry.length&&_num!=0) {
				_num--;
			} else {
				_num=_picUrlAry.length-1;
			}

			loadPic(_num);
			showTxt();
			getChildAt(_num+1).filters=[_glowFilter];
			_timer.stop();
		}

		private function onPrevBtnUp(e:MouseEvent):void {
			_timer.start();
		}

		//控制自动播放时间。
		private function changeHandler(e:Event):void {
			_timer.removeEventListener("timer",timerHandler);
			_time=e.target.selectedItem.label;
			_timer=new Timer(_time,0);
			_timer.addEventListener("timer",timerHandler);
			_timer.start();
		}

		//停止自动播放
		private function onStopBtnClicked(e:MouseEvent):void {
			_timer.stop();
		}
		//开始自动播放
		private function onPlayBtnClicked(e:MouseEvent):void {
			_timer.start();
		}

		//==================================================================

		//载入图片
		private function loadPic(_num:uint):void {
			_urlRequest=new URLRequest(_picUrlAry[_num]);
			_loader=new Loader  ;
			_loader.x=150;
			_loader.y=50;
			_loader.load(_urlRequest);
			_loader.filters=[_picGlowFilter];
			addChild(_loader);
		}

		//显示文本
		private function showTxt():void {
			_picTxt.text=_txtAry[_num];
			_picTxt.x=240;
			_picTxt.y=260;
			_picTxt.width=50;
			_picTxt.height=30;
			_picTxt.autoSize="center";
			_picTxt.selectable=false;
			addChildAt(_picTxt,0);
		}

		//==================自动播放=======================================

		private function timerHandler(e:TimerEvent):void {
			getChildAt(_num+1).filters=null;

			if (_num<_picUrlAry.length-1) {
				_num++;
			} else {
				_num=0;
			}

			getChildAt(_num+1).filters=[_glowFilter];
			loadPic(_num);
			showTxt();
		}
	}
}

 

posted @ 2010-06-23 17:38  LT世纪  阅读(391)  评论(0编辑  收藏  举报