使用以下类文件,可以裁剪出拼图游戏 的图片块
如下图:
Tile.as类文件
package zkl.as3.game.pintu { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BlendMode; import flash.display.Shape; import flash.display.Sprite; import flash.geom.Matrix; /** * ... * @author ZKL(Pointer) QQ:344209679 */ public class Tile2 extends Sprite { /** * 0:无 * 1:补 * -1:切 * up * ----------------- * | | * left | | right * | | * ----------------- * down */ public var left:int = 0; public var up:int = 0; public var right:int = 0; public var down:int = 0; /** * 矩形的宽 */ public var rectWidth:Number = 0; /** * 矩形的高 */ public var rectHeight:Number = 0; /** * 圆半径 */ public var circleRadius:Number = 0; /** * 圆中心点与矩形的偏移量 */ public var offset:Number = 0; /** * 矩形焦点与当前实例的焦点的横坐标偏量 */ public var offsetX:Number = 0; /** * 矩形焦点与当前实例的焦点的竖坐标偏量 */ public var offsetY:Number = 0; public var bitmap:Bitmap; public function Tile2() { } public function draw():void { offset = circleRadius * 0.7; offsetX = circleRadius + offset; offsetY = circleRadius + offset; var _offsetNum:Number = (circleRadius + offset) * 2 + 2;// var bmp:BitmapData = new BitmapData(rectWidth + _offsetNum , rectHeight + _offsetNum, true, 0x00000000); bmp.draw(_createRect(), new Matrix(1, 0, 0, 1, offsetX, offsetY)); if (left != 0) { bmp.draw(_createCircle(), new Matrix(1, 0, 0, 1, offsetX - left * offset, offsetY + rectHeight * .5), null, left == 1?null:BlendMode.ERASE); } if (up != 0) { bmp.draw(_createCircle(), new Matrix(1, 0, 0, 1, offsetX + rectWidth * .5, offsetX - up * offset), null, up == 1?null:BlendMode.ERASE); } if (right != 0) { bmp.draw(_createCircle(), new Matrix(1, 0, 0, 1, rectWidth + offsetX + right * offset, offsetY + rectHeight * .5), null, right == 1?null:BlendMode.ERASE); } if (down != 0) { bmp.draw(_createCircle(), new Matrix(1, 0, 0, 1, offsetX + rectWidth * .5, rectHeight + offsetY + down * offset), null, down == 1?null:BlendMode.ERASE); } bitmap = new Bitmap(bmp); bitmap.smoothing = true; addChild(bitmap); } public function dispose():void { if (bitmap == null) return; removeChild(bitmap); bitmap.bitmapData.dispose(); bitmap = null; } protected function _createRect():Shape { var shape:Shape = new Shape(); shape.graphics.beginFill(0xFFFFFF, 1); shape.graphics.drawRect(0, 0, rectWidth, rectHeight); shape.graphics.endFill(); return shape; } protected function _createCircle():Shape { var shape:Shape = new Shape(); shape.graphics.beginFill(0xFFFFFF, 1); shape.graphics.drawCircle(0, 0, circleRadius); shape.graphics.endFill(); return shape; } } }
TileRandVars.as可以生成Tile需要的随机参数
package zkl.as3.game.pintu { /** * ... * @author ZKL(Pointer) QQ:344209679 */ public class TileRandVars { /** * * @param $cols 行数 * @param $rows 列数 */ public function createVars($cols:uint, $rows:uint):Array { var arr:Array = new Array(); var i:int, j:int; for (i = 0; i < $cols; i++) { for (j = 0; j < $rows; j++) { var vars:Vars = new Vars(); vars.colId = i + 1; vars.rowId = j + 1; arr.push(vars); } } var len:int = $cols * $rows; for (i = 0; i < len; i++) { var _tempVars:Vars = arr[i]; _tempVars.leftVars = _tempVars.rowId == 1 ? null : arr[i - 1]; _tempVars.rightVars = _tempVars.rowId == $rows ? null : arr[i + 1]; _tempVars.upVars = _tempVars.colId == 1 ? null : arr[i - $rows]; _tempVars.downVars = _tempVars.colId == $cols ? null : arr[i + $rows]; if (_tempVars.rightVars != null) { _tempVars.right = getRandVar(); _tempVars.rightVars.left = -_tempVars.right; } if (_tempVars.downVars != null) { _tempVars.down = getRandVar(); _tempVars.downVars.up = -_tempVars.down; } //trace(_tempVars.left + ":" + _tempVars.up + ":" + _tempVars.right + ":" + _tempVars.down); } return arr; } protected function getRandVar():int { var rand:int = Math.floor(Math.random() * 3) - 1; return rand; } } } class Vars { //所在的行ID public var colId:uint = 0; //所在的列ID public var rowId:uint = 0; public var left:int = 0; public var up:int = 0; public var right:int = 0; public var down:int = 0; public var leftVars:Vars; public var upVars:Vars; public var rightVars:Vars; public var downVars:Vars; }