动画入门,用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

你可能感兴趣的:(动画入门,用actionscript3.0实现【拼图游戏】)