动画入门,用actionscript3.0实现【拼图游戏】

最近看书看到as对位图的操作,为了练手,实现了拼图游戏

主要思路:将图片加载后分割,然后打乱图片顺序,遍历位置判断成功。

整理了一些源码,加了注释,提供给大家共享

还是先看效果,图片用的是我们公司开发的游戏(warriorepic),有空大家玩玩哦
 

 开始游戏,如果没反应,请用鼠标点击一下flash再试。

package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.geom.Point; import flash.geom.Rectangle; import flash.net.URLRequest; import flash.text.TextField; import flash.ui.Keyboard; [SWF(width="500",height="350")] public class GetTogether extends Sprite { private var _loader:Loader; private var _rowNum:int = 3; private var _columnNum:int = 4; private var _imgArr:Array; private var _blockWidth:Number; private var _blockHeight:Number; private var _padding:int = 1; private var _lastBlock:BitmapWithIndex; public function GetTogether():void { //载入图片 _loader = new Loader(); _loader.load(new URLRequest("HTTP://WWW.GAOXIAOS.COM/MYTEST/1.jpg")); _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadedImg); } private function onLoadedImg(e:Event):void { //把图片分割写入数组 var loadImg:Bitmap = Bitmap(_loader.content); _blockWidth = loadImg.width/_columnNum; _blockHeight = loadImg.height/_rowNum; _imgArr = new Array(); for(var i:int=0;i<_rowNum;i++) { _imgArr[i] = new Array(); for(var j:int=0;j<_columnNum;j++) { var tempBitmap:BitmapData = new BitmapData(_blockWidth,_blockHeight); tempBitmap.copyPixels(loadImg.bitmapData,new Rectangle(_blockWidth*j,_blockHeight*i,_blockWidth,_blockHeight),new Point()); _imgArr[i][j] = new BitmapWithIndex(tempBitmap,i,j); if(i != (_rowNum-1) || j != (_columnNum-1)) { this.addChild(_imgArr[i][j]); } else { _lastBlock = _imgArr[i][j]; } } } Shuffle(); Show(); this.stage.addEventListener(KeyboardEvent.KEY_UP,OnMove); } //洗牌 private function Shuffle():void { for(var i:int=0;i<100;i++) { var r:Number = Math.random(); if(r<0.25) { Move(Keyboard.UP); } else if(r<0.5) { Move(Keyboard.DOWN); } else if(r<0.75) { Move(Keyboard.LEFT); } else { Move(Keyboard.RIGHT); } } } private function OnMove(e:KeyboardEvent):void{ Move(e.keyCode); //判断成功 var isSuccess:Boolean = true; for(var i:int=0;i<this._rowNum;i++) { for(var j:int=0;j<this._columnNum;j++) { if(this._imgArr[i][j].i!= i || this._imgArr[i][j].j != j) { isSuccess = false; break; } } } if(isSuccess) { this.addChild(_lastBlock); this.stage.removeEventListener(KeyboardEvent.KEY_UP,OnMove); var field:TextField = new TextField(); field.text = "成功(Success)!!!!!"; field.textColor = 0xFF0000; this.addChild(field); } Show(); } //移动 private function Move(e:uint):void { //找到空位置 var emptyI:int; var emptyJ:int; for(var i:int=0;i<this._rowNum;i++) { for(var j:int=0;j<this._columnNum;j++) { if(this._imgArr[i][j].i== _lastBlock.i && this._imgArr[i][j].j == _lastBlock.j) { emptyI = i; emptyJ = j; } } } //根据方向移动到空位置 if(e == Keyboard.UP) { if(emptyI != this._rowNum-1) { this._imgArr[emptyI][emptyJ]=this._imgArr[emptyI+1][emptyJ] this._imgArr[emptyI+1][emptyJ]=this._lastBlock; } } else if(e == Keyboard.DOWN) { if(emptyI != 0) { this._imgArr[emptyI][emptyJ]=this._imgArr[emptyI-1][emptyJ] this._imgArr[emptyI-1][emptyJ]=this._lastBlock; } } else if(e == Keyboard.LEFT) { if(emptyJ != this._columnNum-1) { this._imgArr[emptyI][emptyJ]=this._imgArr[emptyI][emptyJ+1] this._imgArr[emptyI][emptyJ+1]=this._lastBlock; } } else if(e == Keyboard.RIGHT) { if(emptyJ != 0) { this._imgArr[emptyI][emptyJ]=this._imgArr[emptyI][emptyJ-1] this._imgArr[emptyI][emptyJ-1]=this._lastBlock; } } } private function Show():void { for(var i:int=0;i<this._rowNum;i++) { for(var j:int=0;j<this._columnNum;j++) { this._imgArr[i][j].x = j*this._blockWidth+j*_padding; this._imgArr[i][j].y = i*this._blockHeight+i*_padding; } } } } }

你可能感兴趣的:(游戏,function,Flash,Class,import,actionscript)