flash 3.0拼图游戏

 

 

package {
	import flash.display.*;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.geom.*;
	import flash.utils.Timer;
	
	public class JigsawPuzzle extends MovieClip {
		// 小块数量
		const numPiecesHoriz:int = 8;
		const numPiecesVert:int = 6;
		
		// 小块尺寸
		var pieceWidth:Number;
		var pieceHeight:Number;

		// 游戏小块储存对象
		var puzzleObjects:Array;
		
		// 两类sprite
		var selectedPieces:Sprite;//储存被拖拽的所有小块
		var otherPieces:Sprite;//没被拖拽所有小块
		
		// 被拖拽的小块
		var beingDragged:Array  = new Array();
		
		// 导入图像 设置sprite
		public function startJigsawPuzzle() {
			// 导入图像
			loadBitmap("jigsawimage.jpg");
			
			// 设置两个 sprites
			otherPieces = new Sprite();
			selectedPieces = new Sprite();
			addChild(otherPieces);
			addChild(selectedPieces); // 放在顶部
		}
		
		// 从外部获得位图
		public function loadBitmap(bitmapFile:String) {
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
			var request:URLRequest = new URLRequest(bitmapFile);
			loader.load(request);
		}
		
		// 位图导入完成 切成小块
		private function loadingDone(event:Event):void {
			// 创建新的的图像来放置位图
			var image:Bitmap = Bitmap(event.target.loader.content);
			pieceWidth = Math.floor((image.width/numPiecesHoriz)/10)*10;
			pieceHeight = Math.floor((image.height/numPiecesVert)/10)*10;
			
			// 将导入的位图放入image类中
			var bitmapData:BitmapData = image.bitmapData;
			
			// 切割成拼图小块
			makePuzzlePieces(bitmapData);
			
			// 设置移动和鼠标事件
			addEventListener(Event.ENTER_FRAME,movePieces);
			stage.addEventListener(MouseEvent.MOUSE_UP,liftMouseUp);
		}
		
		// 将位图切成小片
		private function makePuzzlePieces(bitmapData:BitmapData) {
			puzzleObjects = new Array();
			for(var x:uint=0;x=0;i--) {
					var n:uint = sortedObjects[i].num; //实际的小块编号
					// 得到与被单击小块之间的相对位置
					var diffX:int = puzzleObjects[n].loc.x - puzzleObjects[clickedPiece].loc.x;
					var diffY:int = puzzleObjects[n].loc.y - puzzleObjects[clickedPiece].loc.y;
					// 查看该对象是否放置正确以连接在被单击的小块上
					if (puzzleObjects[n].piece.x == (puzzleObjects[clickedPiece].piece.x + pieceWidth*diffX)) {
						if (puzzleObjects[n].piece.y == (puzzleObjects[clickedPiece].piece.y + pieceHeight*diffY)) {
							//查看当前对象是否与被选择的对象相邻
							if (isConnected(puzzleObjects[n])) {
								// 添加进选择列表,并设置偏移量
								beingDragged.push(puzzleObjects[n]);
								puzzleObjects[n].dragOffset = new Point(clickLoc.x - puzzleObjects[n].piece.x, clickLoc.y - puzzleObjects[n].piece.y);
								// 移动到顶部的 sprite
								selectedPieces.addChild(puzzleObjects[n].piece);
								// 发现连接,从数组中移除
								oneLinkFound = true;
								sortedObjects.splice(i,1);
							}
						}
					}
				}
			} while (oneLinkFound);
		}
		
		// 得到一个小块,并判断是否与已选择的小块相邻
		public function isConnected(newPuzzleObject:Object):Boolean {
			for(var i in beingDragged) {
				var horizDist:int = Math.abs(newPuzzleObject.loc.x - beingDragged[i].loc.x);
				var vertDist:int = Math.abs(newPuzzleObject.loc.y - beingDragged[i].loc.y);
				if ((horizDist == 1) && (vertDist == 0)) return true;
				if ((horizDist == 0) && (vertDist == 1)) return true;
			}
			return false;
		}
		
		// 舞台发出鼠标释放事件,拖拽结束
		public function liftMouseUp(event:MouseEvent) {
			// 约束所有的小块
			lockPiecesToGrid();
			// 将小块移回到底部
			for(var i in beingDragged) {
				otherPieces.addChild(beingDragged[i].piece);
			}
			// 清除拖拽的数组
			beingDragged = new Array();
			
			// 判断游戏是否结束
			if (puzzleTogether()) {
				cleanUpJigsaw();
				gotoAndStop("gameover");
			}
		}
		
		// 将所有的拼图放置在最接近的10*10的位置上
		public function lockPiecesToGrid() {
			for(var i in puzzleObjects) {
				puzzleObjects[i].piece.x = 10*Math.round(puzzleObjects[i].piece.x/10);
				puzzleObjects[i].piece.y = 10*Math.round(puzzleObjects[i].piece.y/10);
			}
		}
		
		public function puzzleTogether():Boolean {
			for(var i:uint=1;i


 

你可能感兴趣的:(AS3小游戏)