用代码实现儿童拼图玩具中的那种图形的分割。
效果如下:
代码如下:
package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.DisplayObject; import flash.display.Graphics; import flash.display.MovieClip; import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Matrix; public class Demo01 extends MovieClip { public function Demo01() { // constructor code var bmd:BitmapData = new Bmd(); var bmp:Bitmap = new Bitmap(bmd); var rows:int, cols:int, w:Number, h:Number, i:int, j:int, m:int, n:int; rows = 16; cols = 20; w = 800 / cols; h = 500 / rows; var curWidth:Number, ellipseA:Number, ellipseB:Number; //curWidth始终设为较小边的四分之一。以适应长宽比很不和谐的画面 curWidth = h < w? h / 4 : w /4; //ellipseA始终较大,这样可以适应竖着或横着的椭圆 ellipseA = h > w ? h / 5:w / 5; ellipseB = h < w? h / 5: w /5; //addChild(bmp); var arr:Array = []; for (i = 0; i < cols; i++ ) { arr[i] = []; for (j = 0; j < rows;j++ ) { arr[i][j] = {"down":Boolean(Math.random() > 0.5), "right":Boolean(Math.random()>0.5) }; } } for (i = 0; i < cols; i++ ) { for (j = 0; j < rows;j++ ) { var sp:Sprite = new Sprite(); var gra:Graphics = sp.graphics; gra.beginBitmapFill(bmd); // gra.drawRoundRect(i * w, j * h, w, h, 5, 5); //top gra.moveTo(w * i, h * j); if (j != 0) { if (arr[i][j-1].down) { gra.curveTo(w * (i + 0.5), h * j + curWidth, w * (i + 1), h * j); gra.drawEllipse( w * (i + 0.5) - ellipseA * 0.5, h * j + curWidth * 0.5, ellipseA, ellipseB); } else { gra.curveTo(w * (i + 0.5), h * j - curWidth, w * (i + 1), h * j); gra.drawEllipse( w * (i + 0.5) - ellipseA * 0.5, h * j - curWidth * 0.5-ellipseB, ellipseA, ellipseB); } } else { gra.lineTo(w*(i+1), h*j); } //right gra.moveTo(w*(i+1), h*j); if (i != cols - 1) { if (arr[i][j].right) { gra.curveTo(w * (i +1) + curWidth, h * (j + 0.5), w * (i + 1), h * (j + 1)); gra.drawEllipse( w * (i + 1) + curWidth * 0.5, h * (j + 0.5) - ellipseA * 0.5, ellipseB, ellipseA); } else { gra.curveTo(w * (i +1) - curWidth, h * (j + 0.5), w * (i + 1), h * (j + 1)); gra.drawEllipse( w * (i + 1) - curWidth * 0.5 - ellipseB, h * (j + 0.5) - ellipseA * 0.5, ellipseB, ellipseA); } } else { gra.lineTo(w * (i + 1), h * (j+1)); } //bottom gra.moveTo(w * (i + 1), h * (j+1)); if (j != rows - 1) { if (arr[i][j].down) { gra.curveTo(w * (i + 0.5), h * (j +1) + curWidth, w * i , h * (j + 1)); gra.drawEllipse( w * (i + 0.5)-ellipseA*0.5, h * (j +1)+curWidth*0.5, ellipseA, ellipseB); } else { gra.curveTo(w * (i + 0.5), h * (j +1) - curWidth, w * i , h * (j + 1)); gra.drawEllipse( w * (i + 0.5)-ellipseA*0.5, h * (j +1)-curWidth*0.5-ellipseB, ellipseA, ellipseB); } } else { gra.lineTo(w*i, h*(j+1)); } //left gra.moveTo(w*i, h*(j+1)); if (i != 0) { if (arr[i - 1][j].right) { gra.curveTo(w * i + curWidth, h * (j + 0.5), w * i , h * j); gra.drawEllipse( w * i + curWidth * 0.5, h * (j + 0.5) - ellipseA * 0.5, ellipseB, ellipseA); } else { gra.curveTo(w * i - curWidth, h * (j + 0.5), w * i , h * j); gra.drawEllipse( w * i - curWidth * 0.5 - ellipseB, h * (j + 0.5) - ellipseA * 0.5, ellipseB, ellipseA); } } else { gra.lineTo(w*i, h*j); } gra.endFill(); addChild(sp); sp.x = i; sp.y = j; sp.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); sp.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); } } function mouseDownHandler(e:MouseEvent):void { e.currentTarget.startDrag(); setChildIndex(e.currentTarget as DisplayObject, numChildren - 1); } function mouseUpHandler(e:MouseEvent):void { e.currentTarget.stopDrag(); } } } }
代码就不多解释了,因为过了好长时间,现在我自己也看不懂写的是什么了。直接去看那篇参考文章吧。需要注意的是,gra.drawRoundRect(i * w, j * h, w, h, 5, 5)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。