import flash.display.Sprite; import flash.display.Loader; import flash.net.URLRequest; import flash.display.BitmapData; import flash.events.Event; import flash.events.MouseEvent; import flash.filters.GlowFilter; import flash.filters.DropShadowFilter; import flash.display.Bitmap; import flash.display.MovieClip; //拖动拼图 数组 var spArray: Array = new Array(); //被触碰拼图 对象 var hit_Object: Object = {}; var rows, cols, w, h, i, j, m, n: Number; var filterArray: Array = new Array(); //当前拖动的拼图块 var nowDrag_mc:MovieClip; //定义行数列数 rows = 8; cols = 8; //画曲线的重要参数,curWidth为贝塞尔曲线的参数 var curWidth: Number; var ellipseA: Number; var ellipseB: Number; //加载外部图片 var url = new URLRequest("1.jpg"); var _loader = new Loader(); _loader.load(url); _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImgLoad); //定义滤镜 var color1: Number = 0x33CCFF; var alp1: Number = 0.8; var blurX1: Number = 2; var blurY1: Number = 2; var strength1: Number = 2; var quality1: Number = 3; var inner1: Boolean = false; var knockout1: Boolean = false; var filter1: GlowFilter = new GlowFilter(color, alp1, blurX1, blurY1, strength1, quality1, inner1, knockout1); filterArray.push(filter1); var splArray: Array = new Array(); var distance: Number = 3; var angleInDegrees: Number = 3; var color: Number = 0x000033; var alp: Number = 0.8; var blurX: Number = 3; var blurY: Number = 3; var strength: Number = 1; var quality: Number = 3; var inner: Boolean = false; var knockout: Boolean = false; var hideObject: Boolean = false; var filter: DropShadowFilter = new DropShadowFilter(distance, angleInDegrees, color, alp, blurX, blurY, strength, quality, inner, knockout, hideObject); filterArray.push(filter); //对每个格子的形状初始化,如果left, right, up, down全部为true表示四面有填充的椭圆 for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { //var ms: MySplit = new MySplit(i, j); var ms={row:i,col:j}; //首先调用随机数,确认本格右方和下方是否填充 m = int(Math.random() * 2); n = int(Math.random() * 2); if (m == 1 && j < cols - 1) { ms.right = true; } if (n == 1 && i < rows - 1) { ms.down = true; } //cuplayer.com检测本格左方是否需要填充 if (j > 0 && !splArray[i * cols + j - 1].right) { ms.left = true; } //cuplayer.com检测本格上方是否需要填充 if (i > 0 && !splArray[(i - 1) * cols + j].down) { ms.up = true; } splArray.push(ms); } } //cuplayer.com主处理函数 function onImgLoad(event: Event): void { var bitmap: BitmapData = new BitmapData(_loader.width, _loader.height); w = _loader.width / rows; curWidth = w / (cols-1); h = _loader.height / cols; //ellipseA始终较大,这样可以适应竖着或横着的椭圆 ellipseA = h > w ? h / rows : w / rows; ellipseB = h < w ? h / cols : w / cols; var sampleSprite: MovieClip ; //cuplayer.com逐个分割图片 for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { bitmap.draw(_loader); sampleSprite= new MovieClip(); sampleSprite.graphics.lineStyle(); sampleSprite.graphics.beginBitmapFill(bitmap); myDraw(sampleSprite, splArray[i * cols + j]); sampleSprite.graphics.endFill(); if (w * rows > 1000) { sampleSprite.scaleX = 1000 / (w * rows); sampleSprite.scaleY = 1000 / (w * rows) } sampleSprite.x = 2 * j; sampleSprite.y = 2 * i; //标识每块的唯一性 用来检测碰撞 sampleSprite.id=i.toString()+j.toString(); dragContainer_mc.addChild(sampleSprite); sampleSprite.filters = filterArray; spArray.push(sampleSprite); sampleSprite.addEventListener(MouseEvent.MOUSE_DOWN, onDown); } } for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { bitmap.draw(_loader); sampleSprite = new MovieClip(); sampleSprite.graphics.lineStyle(); sampleSprite.graphics.beginBitmapFill(bitmap); myDraw(sampleSprite, splArray[i * cols + j]); sampleSprite.graphics.endFill(); if (w * rows > 1000) { sampleSprite.scaleX = 1000 / (w * rows); sampleSprite.scaleY = 1000 / (w * rows) } sampleSprite.x = 2 * j; sampleSprite.y = 2 * i; hitContainer_mc.addChild(sampleSprite); sampleSprite.filters = filterArray; //标识每块的唯一性 用来检测碰撞 hit_Object[i.toString()+j.toString()]=sampleSprite; } } //被碰撞的拼图 hitContainer_mc.alpha=0; //原图 var bitmap_yuantu: BitmapData = new BitmapData(_loader.width, _loader.height); bitmap_yuantu.draw(_loader); var bitmap_yuantuBM=new Bitmap(bitmap_yuantu); var scaleNum=2 * cols/bitmap_yuantuBM.width+1; bitmap_yuantuBM.scaleX =scaleNum; bitmap_yuantuBM.scaleY =scaleNum; yuantuContainer_mc.addChild(bitmap_yuantuBM); bitmap_yuantuBM.filters = filterArray; } //cuplayer.com画图函数 function myDraw(sprite: Sprite, ms: Object) { i = ms.row; j = ms.col; sprite.graphics.drawRoundRect(j * w, i * h, w, h, 5, 5); if (ms.right) { sprite.graphics.moveTo(w * (j + 1), i * h); sprite.graphics.curveTo(w * (j + 1) - curWidth, i * h + h / 2, w * (j + 1), (i + 1) * h); sprite.graphics.drawEllipse(w * (j + 1) - 0.5 * curWidth, i * h + 0.5 * h - 0.5 * ellipseA, ellipseB, ellipseA); } else if (j < (cols-1)) { sprite.graphics.moveTo(w * (j + 1), i * h); sprite.graphics.curveTo(w * (j + 1) + curWidth, i * h + h / 2, w * (j + 1), (i + 1) * h); sprite.graphics.drawEllipse(w * (j + 1) + 0.5 * curWidth - ellipseB, i * h + 0.5 * h - 0.5 * ellipseA, ellipseB, ellipseA); } if (ms.left) { sprite.graphics.moveTo(w * j, i * h); sprite.graphics.curveTo(w * j + curWidth, i * h + h / 2, w * j, (i + 1) * h); sprite.graphics.drawEllipse(w * j + 0.5 * curWidth - ellipseB, i * h + 0.5 * h - 0.5 * ellipseA, ellipseB, ellipseA); } else if (j > 0) { sprite.graphics.drawEllipse(w * j - 0.5 * curWidth, i * h + 0.5 * h - 0.5 * ellipseA, ellipseB, ellipseA); sprite.graphics.moveTo(w * j, i * h); sprite.graphics.curveTo(w * j - curWidth, i * h + h / 2, w * j, (i + 1) * h); } if (ms.down) { sprite.graphics.moveTo(w * j, (i + 1) * h); sprite.graphics.curveTo(w * j + 0.5 * w, (i + 1) * h - curWidth, (j + 1) * w, (i + 1) * h); sprite.graphics.drawEllipse(w * j + 0.5 * w - 0.5 * ellipseA, (i + 1) * h - 0.5 * curWidth, ellipseA, ellipseB); } else if (i < (cols-1)) { sprite.graphics.moveTo(w * j, (i + 1) * h); sprite.graphics.curveTo(w * j + 0.5 * w, (i + 1) * h + curWidth, (j + 1) * w, (i + 1) * h); sprite.graphics.drawEllipse(w * j + 0.5 * w - 0.5 * ellipseA, (i + 1) * h + 0.5 * curWidth - ellipseB, ellipseA, ellipseB); } if (ms.up) { sprite.graphics.moveTo(w * j, i * h); sprite.graphics.curveTo(w * j + 0.5 * w, i * h + curWidth, (j + 1) * w, i * h); sprite.graphics.drawEllipse(w * j + 0.5 * w - 0.5 * ellipseA, i * h + 0.5 * curWidth - ellipseB, ellipseA, ellipseB); } else if (i > 0) { sprite.graphics.moveTo(w * j, i * h); sprite.graphics.curveTo(w * j + 0.5 * w, i * h - curWidth, (j + 1) * w, i * h); sprite.graphics.drawEllipse(w * j + 0.5 * w - 0.5 * ellipseA, i * h - 0.5 * curWidth, ellipseA, ellipseB); } } //cuplayer.com拖曳处理函数 function onDown(e: MouseEvent) { //获取当前被拖动 的对象 nowDrag_mc=MovieClip(e.currentTarget); nowDrag_mc.startDrag(); stage.addEventListener(MouseEvent.MOUSE_UP, onUp); } function onUp(e: MouseEvent) { stage.removeEventListener(MouseEvent.MOUSE_UP, onUp); nowDrag_mc.stopDrag(); }