背包 拖拽、换位 功能

Demo 背包结构

 

主类:

package
{
	import com.ItemManage;
	
	import flash.display.Sprite;
	
	public class PackTest extends Sprite
	{

		private var idList1:Array;

		private var idList2:Array;

		public function PackTest()
		{
			init();
		}
		
		/**
		 * 初始化
		 */		
		public function init():void
		{
			idList1 = new Array();
			idList2 = new Array();
			
			var box:DragItemBlock;
			
			//  初始16个防止道具的位置
			for (var i:int = 0; i < 16; i++) 
			{
				box = new DragItemBlock();
				box.location = i;
				box.type = 0;
				box.x = int(i % 4) * 60 + 20;
				box.y = int(i / 4) * 60 + 20;
				addChild( box );
				idList1.push( box );
				
				box = new DragItemBlock();
				box.location = i;
				box.type = 1;
				box.x = int(i % 4) * 60 + 300;
				box.y = int(i / 4) * 60 + 20;
				addChild( box );
				idList2.push( box );
			}
			
			var item:ItemManage;
			var mc:Class;
			
			//  swc 内的20个 图像
			var arr:Array = [lol0,lol1,lol2,lol3,lol4,lol5,lol6,lol7,lol8,lol9,lol10,lol11,lol12,lol13,lol14,lol15,lol16,lol17,lol18,lol19];
			for (var j:int = 0; j < 10; j++) 
			{
				mc = arr[j] as Class;
				item = new ItemManage(1000 + j,int(Math.random() * 20) + 1, 0, new mc());
				idList1[j]._itemEntity = item;
				
				mc = arr[j] as Class;
				item = new ItemManage(1050 + j, int(Math.random() * 20) + 1,1,new mc());
				idList2[j]._itemEntity = item;
			}
		}
	}
}

 

 

DragItemBlock :↓
package
{
	import com.ItemManage;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class DragItemBlock extends Sprite
	{
		public static var itemBlockSrc:DragItemBlock;
		public static var itemBlockDis:DragItemBlock;
		
		public static var pointSrc:Point;
		public static var pointDis:Point;
		
		public static var sprite:Sprite;
		
		public static var sBmp:Bitmap;
		
		public static var sPoint:Point;
		
		public static var isDrag:Boolean;
		
		private var itemEntity:ItemManage;
		public var bmp:Bitmap = new Bitmap();
		public var location:int;
		public var type:int;
		public function DragItemBlock()
		{
			bmp.x = bmp.y = 2;
			var bmpContainer:BmpContainers = new BmpContainers();
			bmpContainer.addChild( bmp );
			addChild( bmpContainer );
			addEventListener(MouseEvent.MOUSE_DOWN,toDown);
			addEventListener(MouseEvent.MOUSE_UP,toUp);
		}
		
		/**
		 * 开始拖拽
		 * 
		 */		
		private function beginDrag():void
		{
			if(!sprite){
				sprite = new Sprite();
				sBmp = new Bitmap();
				sprite.addChild( sBmp );
				
				sprite.cacheAsBitmap = true;
				
				sprite.mouseEnabled = false;
				stage.addChild( sprite );
			}
			
			sPoint = localToGlobal( new Point(mouseX,mouseY));
			sprite.x = sPoint.x - pointSrc.x + 3;
			sprite.y = sPoint.y - pointSrc.y + 3;
			
			sBmp.bitmapData = itemBlockSrc._itemEntity.bmpd;
			itemBlockSrc.bmp.bitmapData = null;
			isDrag = true;
			sprite.startDrag();
			sprite.visible = true;
			stage.addEventListener(MouseEvent.MOUSE_UP,toStageUp);
		}
		
		/**
		 * 结束 舞台 抬起鼠标
		 * @param event
		 * 
		 */		
		private function toStageUp(event:MouseEvent):void
		{
			if(isDrag){
				itemBlockSrc.update();
				
				endDrag();
			}
		}
		
		/**
		 * 结束拖拽
		 * 
		 */		
		private function endDrag():void
		{
			sprite.stopDrag();
			itemBlockDis = null;
			itemBlockSrc = null;
			pointSrc	 = null;
			pointDis	 = null;
			isDrag		 = false;
			sprite.visible = false;
		}
		/**
		 * 鼠标按下
		 * @param event
		 * 
		 */		
		private function toDown(event:MouseEvent):void
		{
			itemBlockSrc = this;
			if(itemBlockSrc._itemEntity == null)return;
			pointSrc = new Point(event.localX,event.localY);
			
			// start Drag
			beginDrag();
		}
		/**
		 * 鼠标抬起
		 * @param event
		 * 
		 */
		private function toUp(event:MouseEvent):void
		{
			if(isDrag){
				pointDis = new Point(event.localX,event.localY);
				
				itemBlockDis = this;
				var itemSrc:ItemManage;
				var itemDis:ItemManage;
				trace(itemBlockSrc != itemBlockDis);
				if((itemBlockSrc != itemBlockDis) && (itemBlockSrc.type == itemBlockDis.type)){
					itemSrc = itemBlockSrc._itemEntity;
					itemDis = itemBlockDis._itemEntity;
					
					itemBlockSrc._itemEntity = itemDis;
					itemBlockDis._itemEntity = itemSrc;
				}else itemBlockSrc.update();
				sprite.visible = false;
				itemBlockDis.removeListener();
				itemBlockSrc.removeListener();
			}
		}
		
		public function update():void
		{
			if(itemEntity == null)return;
			bmp.bitmapData = itemEntity.bmpd;
		}
		
		public function get _itemEntity():ItemManage
		{
			return itemEntity;
		}
		
		public function set _itemEntity(value:ItemManage):void
		{
			itemEntity = value;
			if(value == null){
				bmp.bitmapData = null;
			}else{
				bmp.bitmapData = value.bmpd;
			}
		}
		
		/**
		 * 删掉舞台侦听器
		 * 
		 */		
		public function removeListener():void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP,toStageUp);
		}
	}
}

  

 

ItemManage:↓
package com
{
	import flash.display.BitmapData;

	public class ItemManage
	{
		public var id:int;
		public var count:int;
		public var location:int;
		public var bmpd:BitmapData;
		
		public function ItemManage(_id:int, _count:int, _location:int, _bmpd:BitmapData)
		{
			id = _id;
			count = _count;
			location = _location;
			bmpd = _bmpd;
		}
	}
}

  

 

posted @ 2013-01-31 11:09  ChangeLi  阅读(403)  评论(0编辑  收藏  举报