File 访问本地文件夹加载变换图片

package view
{
	import comm.NodeConst;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.filesystem.File;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.utils.Timer;
	
	public class MainView extends Sprite
	{
		private var roX:int = 5;
		private var swX:int = 5;
		private var swY:int = 50;
		
		private var txtW:int = 400;
		private var txtH:int = 40;
		
		private var txtBgBorder:uint = 0xff0000;
		private var txtBgColor:uint = 0xcccccc;
		private var isBoolean:Boolean = true;
		private var txtType:String = TextFieldType.INPUT;
		
		/**
		 * isText true为人物,false为武器
		 */		
		private var isText:Boolean;
		private var roleText:TextField;
		
		private var aimFolderList	:Array = [];
//		private var aimFolderList	:Array = ["attack", "dead", "run", "stand", "cast"];
		private var swordText:TextField;
		
		private var sprite:Sprite;
		
		private var arr:Array;
		private var s_arr:Array;
		
		private var srcFile:File;
		
		private var num:int;
		
		private var nameArr:Array;
		
		private var pathArr:Array;
		
		private var imgNameArr:Array;
		
		private var maxCount:int;
		public function MainView()
		{
			init();
		}
		
		private function init():void
		{
			textBox_role();
			textBox_sword();
			btnRole();
			btnSword();
			btnPlay();
			s_imgContainer();
			imageContainer();
			
			change_frameRate_txt();
			
			for (var j:int = 0; j < 9; j++) 
			{
				var c_btn:SimpleButton = btnControlArr(j);
				c_btn.x = 570 + int(j % 3) * (c_btn.width + 30);
				c_btn.y = 380 + int(j / 3) * (c_btn.height+ 30);
				if(j > 4){
					c_btn.name = "btn" + (j-1);
				}else c_btn.name = "btn" + j;
				if(j == 4){"这个不要!"}
				else this.addChild( c_btn );
				c_btn.addEventListener(MouseEvent.CLICK,toBtnfaceHandler);
			}
			txtHelp();
		}
		private function txtHelp():void
		{
			var txt:TextField = new TextField();
			txt.text = "attack      cast     dead     run    stand";
			txt.textColor = 0xff0000;
			this.addChild( txt );
			txt.height = 20;
			txt.width = 200;
			txt.selectable = false;
			txt.x = 550,txt.y = 170;
		}
		/**
		 * 角色(文本)
		 * 选择角色文件路径的显示器
		 */		
		private function textBox_role():void
		{
			roleText = new TextField();
			this.addChild(roleText);
			
			roleText.x = roleText.y = roX;
			roleText.width = txtW;
			roleText.height = txtH;
			roleText.border = isBoolean;
			roleText.borderColor = txtBgBorder;
			roleText.background = isBoolean;
			roleText.backgroundColor = txtBgColor;
			roleText.type = txtType;
		}
		
		/**
		 * 角色武器(文本)
		 * 选择角色的武器图片路径的显示器
		 */		
		private function textBox_sword():void
		{
			swordText = new TextField();
			this.addChild(swordText);
			
			swordText.x = swX;
			swordText.y = swY;
			swordText.width = txtW;
			swordText.height = txtH;
			swordText.border = true;
			swordText.borderColor = txtBgBorder;
			swordText.background = isBoolean;
			swordText.backgroundColor = txtBgColor;
			swordText.type = txtType;
		}
		
		/**
		 *  浏览按钮
		 * 《选择角色》 
		 */		
		private function btnRole():void
		{
			var spr:Sprite = _graphics(0xFF00FF,0,0,70,40);
			_btnRole = new SimpleButton(spr,spr,spr,spr);
			var txt:TextField = new TextField();
			txt.selectable = false;
			txt.text = "选择角色";
			txt.width = 50;
			txt.height = 20;
			this.addChild( _btnRole );
			spr.addChild( txt );
			spr.x = 450;
			spr.y = 5;
			_btnRole.addEventListener(MouseEvent.CLICK,toBtnRoleHandler);
		}
		
		/**
		 * 选择角色按钮
		 * @param event
		 */		
		private function toBtnRoleHandler(event:MouseEvent):void
		{
			f_file();
			isText = true;
		}
		
		/**
		 *  浏览按钮
		 * 《选择角色武器》 
		 */		
		private function btnSword():void
		{
			var spr:Sprite = _graphics(0xFF00FF,0,0,70,40);
			_btnSword = new SimpleButton(spr,spr,spr,spr);
			this.addChild( _btnSword );
			var txt:TextField = new TextField();
			txt.selectable = false;
			txt.text = "选择武器";
			txt.height = 20;
			spr.addChild( txt );
			spr.x = 450;
			spr.y = 50;
			_btnSword.addEventListener(MouseEvent.CLICK,toBtnSwordHandler);
		}
		
		private function toBtnSwordHandler(event:MouseEvent):void
		{
			f_file();
			isText = false;
		}
		
		/**
		 * play按钮
		 * 播放动画的按钮
		 */		
		private function btnPlay():void
		{
			var spr:Sprite = _graphics(0xFF0000,0,0,85,85);
			btn = new SimpleButton(spr,spr,spr,spr);
			this.addChild( btn );
			var txt:TextField = new TextField();
			txt.text = "play Go";
			txt.height = 20;
			spr.addChild( txt );
			txt.selectable = false;
			spr.x = 550;
			spr.y = 5;
			btn.addEventListener(MouseEvent.CLICK,toBtnPlayHandler);
		}
		
		/**
		 * 图片的容器
		 * 角色的容器
		 */		
		private function imageContainer():void
		{
			sprite = new Sprite();
			this.addChild( sprite );
			sprite.x = 5;
			sprite.y = 100;
		}
		
		private function s_imgContainer():void
		{
			spr = new Sprite();
			this.addChild( spr );
			spr.x = 5;
			spr.y = 100;
		}
		
		/**
		 * 选择 状态 按钮
		 * 选择人物打斗的形式
		 */		
		private function selectBox():SimpleButton
		{
			var spr:Sprite = _graphics(0x0ffe0,0,0,20,20);
			var btn:SimpleButton = new SimpleButton(spr,spr,spr,spr);
			this.addChild( btn );
			return btn;
		}
		
		/**
		 * 帧频输入框
		 * 修改动作帧频速度
		 */		
		private function change_frameRate_txt():void
		{
			frameText = new TextField();
			frameText.text = String(frameNum);
			this.addChild(frameText);
			
			frameText.x = 550;
			frameText.y = 250;
			frameText.width = 150;
			frameText.height = 40;
			frameText.border = true;
			frameText.borderColor = txtBgBorder;
			frameText.background = isBoolean;
			frameText.backgroundColor = txtBgColor;
			frameText.type = txtType;
		}
		
		/**
		 * 8个按钮
		 * 控制人物和武器的方向
		 */		
		private function btnControlArr(_i:int):SimpleButton
		{
			var spr:Sprite = _graphics(0x0fef0e,0,0,30,30);
			var txt:TextField = new TextField();
			txt.width = spr.width;
			txt.height = spr.height;
			spr.addChild( txt );
			switch(_i)
			{
				case 0:
					txt.text = "↖";
					break;
				case 1:
					txt.text = "↑";
					break;
				case 2:
					txt.text = "↗";
					break;
				case 3:
					txt.text = "←";
					break;
				case 5:
					txt.text = "→";
					break;
				case 6:
					txt.text = "↙";
					break;
				case 7:
					txt.text = "↓";
					break;
				case 8:
					txt.text = "↘";
					break;
			}
			var btn:SimpleButton = new SimpleButton(spr,spr,spr,spr);
			return btn;
		}
		
		private function _graphics(_color:uint,_x:int,_y:int,_w:int,_h:int):Sprite
		{
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(_color);
			spr.graphics.drawRect(_x,_y,_w,_h);
			spr.graphics.endFill();
			this.addChild( spr );
			return spr;
		}
		
		private function toBtnPlayHandler(event:MouseEvent):void
		{
			var len:int = bitmapDataList.length;
			var _len:int= _bitmapDataList.length;
			if(len > 0 || _len > 0)	startTimer();
		}
		
		private function startTimer():void
		{
			btn.removeEventListener(MouseEvent.CLICK,toBtnPlayHandler);
			
			frameNum = Number(frameText.text);
			timer = new Timer(frameNum);
			timer.addEventListener(TimerEvent.TIMER,toTimerHandler);
			timer.start();
			isFit = frameNum;
		}
		
		private function toBtnHandler(event:MouseEvent):void
		{
			isScale = false;
			switch(event.target.name)
			{
				//"attack"  
				case "btn0":
					actionNum = 8;
					fileIndex = 0;
					rightNum = 0;
					cleanBitmapData();
					changeAction();
					_changeAction();
					break;
				//"cast"   
				case "btn1":
					actionNum = 8;
					fileIndex = 1;
					rightNum = 0;
					cleanBitmapData();
					changeAction();
					_changeAction();
					break;
				//"dead"  
				case "btn2":
					actionNum = 4;
					fileIndex = 2;
					rightNum = 0;
					cleanBitmapData();
					changeAction();
					_changeAction();
					break;
				//"run"  4
				case "btn3":
					actionNum = 12;
					fileIndex = 3;
					rightNum = 0;
					cleanBitmapData();
					changeAction();
					_changeAction();
					break;
				//"stand"  
				case "btn4":
					actionNum = 4;
					fileIndex = 4;
					rightNum = 0;
					cleanBitmapData();
					changeAction();
					_changeAction();
					break;
			}
		}

		/**
		 * ===========  8按钮  8个方向   ==========
		 * 	40-8      20-4	  60-12   20-4     40-8
		 * "attack", "dead", "run", "stand", "cast"
		 */	
		private function toBtnfaceHandler(event:MouseEvent):void
		{
			//  左下  1   下  2  上  3  左上  4  左  5
			var len:int = bitmapDataList.length;
			var result:int = len / NodeConst.TOTAL;
			switch(event.target.name)
			{
				case "btn0":
					isScale = false;
					left_right_loction();
					break;
				case "btn1":
					up_loction();
					break;
				case "btn2":
					isScale = true;
					left_right_loction();
					break;
				case "btn3":
					isScale = false;
					left_loction();
					break;
				case "btn4":
					isScale = true;
					left_loction();
					break;
				case "btn5":
					isScale = false;
					left_down_loction();
					break;
				case "btn6":
					down_loction();
					break;
				case "btn7":
					isScale = true;
					left_down_loction();
					break;
			}
		}
		
		private function count():int
		{
			var len:int;
			if(isText)len= bitmapDataList.length;
			else len = _bitmapDataList.length;
			return len / NodeConst.TOTAL;
		}
		
		/**
		 * left——up    right——up
		 */		
		private function left_right_loction():void
		{
			var result:int = count();
			num = result * NodeConst.THREE + 1;
			actionNum = result * NodeConst.FOUR;
			rightNum = num;
		}

		/**
		 * UP
		 */		
		private function up_loction():void
		{
			var result:int = count();
			num = result * NodeConst.TWO + 1;
			actionNum = result * NodeConst.THREE;
			rightNum = num;
		}
		
		/**
		 * DOWN
		 */		
		private function down_loction():void
		{
			var result:int = count();
			num = result * NodeConst.ONE + 1;
			actionNum = result * NodeConst.TWO;
			rightNum = num;
		}
		
		/**
		 * left        right
		 */		
		private function left_loction():void
		{
			var result:int = count();
			num = result * NodeConst.FOUR + 1;
			actionNum = result * NodeConst.FIVE;
			rightNum = num;
		}
		
		/**
		 * left_down     right_down
		 */		
		private function left_down_loction():void
		{
			var result:int = count();
			num = 0;
			actionNum = result * NodeConst.ONE;
			rightNum = num;
		}
		 
		/**
		 * 清理bitmapData
		 */		
		private function cleanBitmapData():void
		{
			bitmapDataList = [];
			_bitmapDataList = [];
		}
		
		/**
		 * 打开本地文件夹的代码
		 */		
		private function f_file():void
		{
			var file:File = new File();
			file.browseForDirectory("请选择您的文件夹");
			file.addEventListener(Event.SELECT,toSelectHandler);
		}
		/**
		 * 1.创建页面
		 * 2.选择文件夹按钮 添加监听 两个
		 * 3.打开文件把路径放入文本框
		 * 4.选择文件放到数组里保存起来
		 * 5.截取初始状态的图片放到sprite里
		 * 6.点击按钮播放
		 * 7.监听选择攻击、防御等各动作按钮
		 * 8.
		 */	
		private function toSelectHandler(event:Event):void
		{
			var eFile:File = event.currentTarget as File;
			var path:String = eFile.nativePath;
			
			var tmpDir:File = new File(path);
			maxCount = tmpDir.getDirectoryListing().length;
			
			var tmpArr:Array = tmpDir.getDirectoryListing();
			var len:int = tmpArr.length;
			for (var i:int = 0; i < len; i++) 
			{
				var tmpStr:String = tmpArr[i].nativePath;
				var _str:String = tmpArr[i].parent.nativePath;
				var _arr:Array = tmpStr.split(_str + "\\");
				aimFolderList[i] = _arr[1];
			}
			
			var aimLen:int = aimFolderList.length;
			if(aimLen < 5){
				if(aimFolderList[0] != null){
					if(aimFolderList[0] != "attack")aimFolderList.unshift("");
				}
				if(aimFolderList[1] != null){
					if(aimFolderList[1] != "cast")aimFolderList.splice(1,0,"");
				}
				if(aimFolderList[2] != null){
					if(aimFolderList[2] != "dead")aimFolderList.splice(2,0,"");
				}
				if(aimFolderList[3] != null){
					if(aimFolderList[3] != "run")aimFolderList.splice(3,0,"");
				}
				if(aimFolderList[4] != null){
					if(aimFolderList[4] != "stand")aimFolderList.splice(4,0,"");
				}
			}
			
			if(aimFolderList[0] != "attack" &&
				aimFolderList[1] != "cast"  &&
				aimFolderList[2] != "dead"  &&
				aimFolderList[3] != "run"   &&
				aimFolderList[4] != "stand"){
				trace("come on !!!");
				return;
			}
		
			if(isText){
				if(roleText.text == path || 
				   roleText.text != ""	 ||
				   swordText.text == path)return;
				roleText.text = path;
			}
			else {
				if(swordText.text == path || 
				   swordText.text != ""	  ||
				   roleText.text == path)return;
				swordText.text = path;
			}
			
			
			for (var j:int = 0; j < 5; j++) 
			{
				if(aimFolderList[j] == ""){
					s_btn = selectBox();
					s_btn.name = "btn" + j;
					s_btn.x = 550 + (s_btn.width + 20) * j;
					s_btn.y = 200;
					s_btn.visible = false;
				}else{
					s_btn = selectBox();
					s_btn.x = 550 + (s_btn.width + 20) * j;
					s_btn.name = "btn" + j;
					s_btn.y = 200;
					s_btn.addEventListener(MouseEvent.CLICK,toBtnHandler);
				}
			}
			
			//加载出来第一个图片,显示在img中
			fileDir = new File(path + "\\" + aimFolderList[fileIndex] + "\\");
			while(!fileDir.exists)
			{
				//防止某个目录不存在
				fileIndex ++;
				if(fileIndex >= maxCount)break;
				//				fileDir = new File(filePath.text + "\\" + aimFolderList[fileIndex] + "\\");
			}
			
			//检查是否合法的目录
			if (fileIndex >= maxCount)
			{
				trace("目录不合法!至少需要包含一个动作目录。");
				return ;
			}
			if(isText)changeAction();
			else _changeAction();
		}
		
		/**
		 * 改变人物的动作
		 */		
		private function changeAction():void
		{
			var showFile:File;
			showFile = new File(roleText.text + "\\"+ aimFolderList[fileIndex] +"\\");
			
			if(showFile.exists){
				arr = showFile.getDirectoryListing();
				var len:int = arr.length;
				for (var i:int = 0; i < len; i++) 
				{
					var f:File = arr[i];
					if(f.extension != "png")arr.pop();
				}
				loaderImgBitmap(arr);
			}
		}
		
		/**
		 * 改变武器动作
		 */		
		private function _changeAction():void
		{
			var showFile:File;
			showFile = new File(swordText.text + "\\"+ aimFolderList[fileIndex] +"\\");
			
			if(showFile.exists){
				s_arr = showFile.getDirectoryListing();
				var len:int = 5;
				for (var i:int = 0; i < len; i++) 
				{
					var f:File = s_arr[i];
					if(f.extension != "png")s_arr.pop();
				}
				_loaderImgBitmap(s_arr);
			}
		}
		
		/**
		 * 控制图片运动的 Timer
		 * @param event
		 */		
		private var rightNum:int = 0;
		private function toTimerHandler(event:TimerEvent):void
		{
			frameNum = Number(frameText.text);
			if(isFit != frameNum){
				btn.addEventListener(MouseEvent.CLICK,toBtnPlayHandler);
				timer.stop();
				timer.removeEventListener(TimerEvent.TIMER,toTimerHandler);
				timer = null;
			}
			num++;
			if(num >= actionNum)num = rightNum;
			 bitmapChild(( bitmapDataList[num] as BitmapData));
			_bitmapChild((_bitmapDataList[num] as BitmapData));
		}
		
		/**
		 * 加载人物数组内所有路径
		 * @param urlArr 所有人物图片路径的数组
		 */	
		private function loaderImgBitmap(urlArr:Array):void
		{
			var len:int = urlArr.length;
			for (var i:int = 0; i < len; i++)
			{
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler(i));
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				
				var request:URLRequest = new URLRequest((urlArr[i] as File).nativePath);
				loader.load(request);
			}
		}
		
		/**
		 * 加载武器数组内所有路径
		 * @param urlArr 所有武器图片路径的数组
		 */		
		private function _loaderImgBitmap(urlArr:Array):void
		{
			var len:int = urlArr.length;
			for (var i:int = 0; i < len; i++) 
			{
				var loader:Loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandlers(i));
				loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
				
				var request:URLRequest = new URLRequest((urlArr[i] as File).nativePath);
				loader.load(request);
			}
		}
		
		/**
		 * 人物  bitmapData Array
		 */		
		private var bitmapDataList:Array = [];
		/**
		 * 武器  bitmapData Array
		 */		
		private var _bitmapDataList:Array = [];
		
		private var timer:Timer;
		/**
		 * 本地文件夹    参数  "attack", "dead", "run", "stand", "cast"
		 */		
		private var fileIndex:int = 0;
		private var fileDir:File;
		/**
		 * 人物动作初始值
		 */		
		private var actionNum:int = 8;
		
		/**
		 * 武器的sprite
		 */		
		private var spr:Sprite;
		
		/**
		 * 人物 == 加载完成
		 * @param i 带来的参数
		 * @return Function
		 */		
		private function completeHandler(i:int):Function{
			return function (event:Event):void
			{
				var loader:Loader = Loader(event.target.loader);
				var image:Bitmap = Bitmap(loader.content);
				bitmapDataList[i] = image.bitmapData;
				bitmapChild(bitmapDataList[0]);
			}
		}
		
		/**
		 * 剑 == 加载完成
		 * @param i 带来的参数
		 * @return Function 
		 */		
		private function completeHandlers(i:int):Function{
			return function (event:Event):void
			{
				var loader:Loader = Loader(event.target.loader);
				var image:Bitmap = Bitmap(loader.content);
				_bitmapDataList[i] = image.bitmapData;
				_bitmapChild(_bitmapDataList[0]);
			}
		}
		
		
		/**
		 * 是否缩放
		 */		
		private var isScale:Boolean;
		private var frameNum:Number = 24;
		private var isFit:Number;

		private var frameText:TextField;

		private var btn:SimpleButton;

		private var _btnRole:SimpleButton;

		private var _btnSword:SimpleButton;

		private var s_btn:SimpleButton;

		/**
		 * 人
		 * @param bitmapData
		 */		
		private function bitmapChild(bitmapData:BitmapData):void
		{
			var bitmap:Bitmap = new Bitmap(bitmapData);
			if(isScale){
				bitmap.scaleX = -1;
				bitmap.x = bitmap.x + bitmap.width;
			}
			sprite.addChild( bitmap );
			if(sprite.numChildren==2)sprite.removeChildAt(0);
		}
		/**
		 * 剑
		 * @param bitmapData
		 */		
		private function _bitmapChild(bitmapData:BitmapData):void
		{
			var _bitmap:Bitmap = new Bitmap(bitmapData);
			if(isScale){
				_bitmap.scaleX = -1;
				_bitmap.x = _bitmap.x + _bitmap.width;
			}
			spr.addChild( _bitmap );
			if(spr.numChildren==2)spr.removeChildAt(0);
		}
		
		/**
		 * image加载发生错误
		 * @param event
		 */		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			//				var _urlStr:String = (event.currentTarget as LoaderInfo).url;
			//				var tempArr:Array = _urlStr.split("file:///");
			//				pathArr = (tempArr[1] as String).split("/");
			//				imgNameArr = (pathArr[4] as String).split(".");
			//				nameArr = (imgNameArr[0] as String).split("");
		}
	}
}

  

package comm
{
	public class NodeConst
	{
		public function NodeConst()
		{
		}
		public static const ONE:int = 1;
		public static const TWO:int = 2;
		public static const THREE:int = 3;
		public static const FOUR:int = 4;
		public static const FIVE:int = 5;
		
		public static const TOTAL:int = 5;
	}
}

  

package
{
	import flash.display.Sprite;
	import view.MainView;
	
	[SWF (width="760", height="620",backgroundColor="0x000000")]
	public class fileDemo extends Sprite
	{
		public function fileDemo()
		{
			var main:MainView = new MainView();
			this.addChild( main );
		}
	}
}

  

 

说明:自己写的代码,很乱!自己也值得,好好努力,以后会写好的。加油!只是为先备份一下。方便以后利用!

posted @ 2013-03-11 18:22  ChangeLi  阅读(521)  评论(1编辑  收藏  举报