Random
Afternoon nothing to do, got a random cut jigsaw effect, not to make judgments, just a cutting method of calculation, later finishing should be a finished product, hehe ~ principle is to use bitmap to cut the map, used to do too much to cover the resources. The effect is as follows:
Main code: PUZZLE.AS
/*
* Puzzle CLASS
*
* @ CREATED by:ycccc8202
* @ Purpose: Easy to make puzzle games
* @ date:2007.6.10
* Usage Example:
* var url:string = "/uploadpic/2007-7/200778135631511.jpg";
* var puzzleuzzle = new Puzzle (this, URL);
* Set Placement
* Puzzle.setposition (30, 20);
* Set row/Column
* Puzzle.setrowandline (30, 10);
*/
Import Com.ycccc.PuzzleGame.MovieClipDrag;
Import Mx.events.EventDispatcher;
Import Flash.display.BitmapData;
Import Flash.geom.Point;
Import Flash.geom.Matrix;
Import Flash.geom.Rectangle;
Import Flash.filters.BevelFilter;
Class Com.ycccc.PuzzleGame.Puzzle {
/**
* Private Members
*/
Load Picture Long width///////////
private Var _imagew:number;
private Var _imageh:number;
Set max width and height///////////
private var _imagemaxw:number = 800;
private var _imagemaxh:number = 500;
////////////////////////////////
private Var dispatchevent:function;
public Var addeventlistener:function;
public Var removeeventlistener:function;
////////////////////////////////
private Var _oldurl:string;
private Var _newurl:string;
private Var _x:number;
private Var _y:number;
private Var _row:number;
private Var _line:number;
private Var _path:movieclip;
private Var _imagem:movieclip;
private Var _pieceboard:movieclip;
private Var _imageloader:moviecliploader;
private Var _imagebitmap:bitmapdata;
private Var _piecew:number;
private Var _pieceh:number;
private Var _pieceminwh:number;
private Var _pieced:number;
Inner-cut rectangle with wide height (approximate ellipse by rectangular drawing)///////////
private Var _pieceow:number;
private Var _pieceoh:number;
Proportional coefficient///////////////
private var _pieced_k:number = 10;
private var _pieceo_k:number = 4;
private var _pieceowh_k:number = 3/4;
/////////////////////////////////
/**
* Constructor
*/
Public Function Puzzle (Path:movieclip, imagelink:string) {
Eventdispatcher.initialize (this);
_path = path;
_newurl = ImageLink;
_imagem = _path.createemptymovieclip ("Imagem", _path.getnexthighestdepth ());
_imageloader = new Moviecliploader ();
_imageloader.addlistener (this);
LoadImage (_newurl);
}
/**
* Public methods
*/
Public function set _url (url:string) {
LoadImage (URL);
}
Public function Get _url (): String {
return _oldurl;
}
Public function set Row (R:number) {
_row = R;
}
Public function set Line (L:number) {
_line = l;
}
Public Function removeallpiece () {
For (var all in _pieceboard) {
_pieceboard[all].removemovieclip ();
}
}
Public Function Bitmapcut () {
Pieceset ();
Removeallpiece ();
for (var i:number = 0; i<_row; i++) {
for (var j:number = 0; j<_line; j + +) {
var Piece = _pieceboard.createemptymovieclip ("iece" + (_LINE*I+J), _pieceboard.getnexthighestdepth ());
Piece.beginbitmapfill (_imagebitmap, New Matrix (1, 0, 0, 1,-j*_piecew,-I*_PIECEH), true, true);
piece._x = J*_piecew;
piece._y = I*_PIECEH;
PIECE.I = i;
PIECE.J = j;
New Movieclipdrag (Piece);
Drawpiece (Piece, Getalldotarray (Piece));
Draw a small piece of shape
Piece.filters = [New Bevelfilter (3, 30)];
}
}
}
Public Function Setmaxwh (W:number, H:number) {
Sets the maximum allowable width and height
_imagemaxw = W;
_imagemaxh = h;
}
Public Function Setrowandline (Row:number, Line:number) {
Set the Cut Row/column
if (row<=0 | | line<=0) {
Trace ("Row/column cannot be 0, cut graph by default 10*10");
Return
}
if (row*line>900) {
Trace ("The quantity is too large, the operation is difficult, according to the default 10*10 to cut Diagram");
Return
}
_row = row;
_line = line;
}
Public Function SetPosition (X:number, Y:number) {
Set up
if (x<0 | | y<0) {
Trace ("Out of scene range, placed by default (0,0) position");
Return
}
_x = x;
_y = y;
if (_pieceboard<>undefined) {
_pieceboard._x = _x;
_pieceboard._y = _y;
}
}
Public Function Getpieceboard (): MovieClip {
Get the debris carrier
return _pieceboard;
}
Public Function toString (): String {
Return "Uzzle:: Concave-convex shape of jigsaw puzzle cutting";
}
/**
* Private Methods
*/
Private Function LoadImage (url:string) {
_newurl = URL;
try {
_imageloader.loadclip (_newurl, _imagem);
} catch (E:error) {
Trace (e);
}
}
Private Function Onloadstart (target:movieclip) {
Target._visible = false;
Dispatchevent ({type: "OnStart", target:this});
}
Private Function Onloaderror (Target:movieclip, errorcode:string) {
Dispatchevent ({type: "OnError", target:this});
Trace ("ErrorCode:" +errorcode);
Trace ("Error, Continue reading previous picture");
_newurl = _oldurl;
LoadImage (_newurl);
}
Private Function Onloadinit (target:movieclip) {
Dispatchevent ({type: "OnInit", target:this});
_oldurl = _newurl;
if (target._width<10 | | target._height<10) {
throw new Error ("The picture is too small, not suitable for cutting!");
}
if (isNaN (_IMAGEMAXW+_IMAGEMAXH)) {
_imagemaxw = _imagemaxh=600;
}
if (isNaN (_row+_line)) {
_row = 10;
_line = 10;
}
_imagew = Target._width>_imagemaxw? _imagemaxw:target._width;
_imageh = Target._height>_imagemaxh? _imagemaxh:target._height;
_pieceboard.removemovieclip ();
Mctobitmap (target);
}
Private Function Mctobitmap (mc:movieclip) {
_imagebitmap = new BitmapData (_imagew, _imageh, True, 0X00FFFFFF);
_imagebitmap.draw (MC);
Hidden away
_pieceboard = _path.createemptymovieclip ("Pieceboard", _path.getnexthighestdepth ());
_pieceboard._x = _x;
_pieceboard._y = _y;
Bitmapcut ();
}
Private Function Pieceset () {
_piecew = _imagew/_line;
_PIECEH = _imageh/_row;
_PIECEMINWH = Math.min (_piecew, _PIECEH);
_pieced = _pieceminwh/_pieced_k;
_pieceow = _pieceminwh/_pieceo_k;
_pieceoh = _pieceow/_pieceowh_k;
}
Private Function Getrndd (): number {
Returns the height that is staggered from the boundary
Return _pieced-math.random () *2*_pieced;
}
Private Function Drawpiece (Mc:movieclip, Dotarr:array): Void {
With (MC) {
LineStyle (0);
MoveTo (0, 0);
for (var k:number = 0; k<dotarr.length; k++) {
if (dotarr[k].x<>undefined) {
LineTo (dotarr[k].x, DOTARR[K].Y);
} else {
Curveto (dotarr[k][0].x, Dotarr[k][0].y, dotarr[k][1].x, DOTARR[K][1].Y);
}
}
Endfill ();
}
}
Private Function Getovaldotarray (Mc:movieclip, position:string): Array {
var rnd:number = random (2)? 1:-1;
var circledotarr:array = [];
Switch (position) {
Case ' right ':
var a0oint = new Point (_piecew+getrndd (), (_pieceh-_pieceow)/2+_pieceow/4-math.random () *_PIECEOW/2);
var a1:array = [New Point (a0.x+rnd* (_PIECEOH/2), A0.Y-_PIECEOW/2), New Point (A0.x+rnd*_pieceoh, a0.y)];
var a2:array = [New Point (a0.x+rnd* (_PIECEOH+_PIECEOW/3), A0.Y+_PIECEOW/2), New Point (A0.x+rnd*_pieceoh, A0.y+_pieceow )];
var a3:array = [New Point (A0.X+RND*_PIECEOH/2, A0.Y+_PIECEOW+_PIECEOW/2), New Point (a0.x, A0.y+_pieceow)];
Circledotarr = [A0, a1, A2, A3];
Break
Case "Down":
var a0oint = new Point (_piecew-(_piecew-_pieceow)/2+_pieceow/4-math.random () *_PIECEOW/2), _pieceh+getrndd ());
var a1:array = [New Point (A0.X+_PIECEOW/2, a0.y+rnd* (_PIECEOH/2)), New Point (a0.x, A0.y+rnd*_pieceoh)];
var a2:array = [New Point (A0.X-_PIECEOW/2, a0.y+rnd* (_PIECEOH+_PIECEOW/3)), New Point (A0.x-_pieceow, A0.y+rnd*_pieceoh )];
var a3:array = [New Point (A0.X-_PIECEOW-_PIECEOW/2, A0.Y+RND*_PIECEOH/2), New Point (A0.x-_pieceow, a0.y)];
Circledotarr = [A0, a1, A2, A3];
Break
}
return Circledotarr;
}
Private Function Getalldotarray (mc:movieclip): Array {
var alldotarray:array = [];
A,b,c,d
if (mc.i = = 0) {
MC.A = [];
} else {
var Temparray:array = mc._parent["iece" + (Number (MC._NAME.SUBSTR (5))-_line)].c;
var a:array = new Array (4);
A[0] = new Point (temparray[3][1].x, TEMPARRAY[3][1].Y-_PIECEH);
A[1] = [New Point (temparray[3][0].x, TEMPARRAY[3][0].Y-_PIECEH), New Point (temparray[2][1].x, temparray[2][1].y-_ PIECEH)];
A[2] = [New Point (temparray[2][0].x, TEMPARRAY[2][0].Y-_PIECEH), New Point (temparray[1][1].x, temparray[1][1].y-_ PIECEH)];
A[3] = [New Point (temparray[1][0].x, TEMPARRAY[1][0].Y-_PIECEH), New Point (temparray[0].x, TEMPARRAY[0].Y-_PIECEH)];
MC.A = A;
}
if (MC.J = = 0) {
MC.D = [];
} else {
var Temparray:array = mc._parent["Piece" + (Number (MC._NAME.SUBSTR (5))-1)].b;
var a:array = new Array (4);
A[0] = new Point (Temparray[3][1].x-_piecew, TEMPARRAY[3][1].Y);
A[1] = [New Point (Temparray[3][0].x-_piecew, temparray[3][0].y), New Point (Temparray[2][1].x-_piecew, temparray[2][1] . Y)];
A[2] = [New Point (Temparray[2][0].x-_piecew, temparray[2][0].y), New Point (Temparray[1][1].x-_piecew, temparray[1][1] . Y)];
A[3] = [New Point (Temparray[1][0].x-_piecew, temparray[1][0].y), New Point (Temparray[0].x-_piecew, temparray[0].y)];
MC.D = A;
}
if (mc.i = = _row-1) {
MC.C = [];
} else {
MC.C = Getovaldotarray (MC, "Down");
}
if (MC.J = = _line-1) {
MC.B = [];
} else {
MC.B = Getovaldotarray (MC, "right");
}
Alldotarray = Alldotarray.concat (MC.A);
Alldotarray.push (new Point (_piecew, 0));
Alldotarray = Alldotarray.concat (MC.B);
Alldotarray.push (new Point (_piecew, _PIECEH));
Alldotarray = Alldotarray.concat (MC.C);
Alldotarray.push (new Point (0, _PIECEH));
Alldotarray = Alldotarray.concat (MC.D);
return alldotarray;
}
}
The tangent method is shown in the diagram, and the algorithm can produce random effects by adding some random values.
The main difficulty may be only in how to cut the ellipse, here through the rectangular boundary plus control points to the Curveto curve, forming an oval
PS::: Compressed package in the Puzzle.exe Support text box to fill in the local picture and network picture address:, between the safety of the hourglass problem, can only be packaged into an EXE to facilitate the transfer of network pictures ~
Attachment Download:
Puzzel.rar