Common Flash mini-game development Core thought notes--"jigsaw puzzles"

Source: Internet
Author: User

Well, today formally decided to open this pit, mainly used to record the development of Flash games I have mastered some of the careful. Because I write the small game is someone else's company outsourcing projects, so I can not release the source code, but the core of the algorithm recorded here, for those small game development novice to do a tutorial (Bo Master and TMD two disease hair).

——————————————————————————————————————————————————— Ideas First, we need to understand the gameplay of jigsaw puzzles, The player must stitch together a bunch of scattered images into a complete picture. So for developers, how to achieve the clutter of the image and detection stitching complete is the core of the puzzle game algorithm. So, the next step is a preliminary analysis of the two algorithms: {1. Broken pictures} If as a novice to the new game development, the first thought of the solution is to pre-cut the image with the revision software, and then in the batch import into the game, randomly shuffle the display . Yes, this is probably the easiest way to get started, but if you have a development experience with a game engineering lion, you don't just have to see this shallow, easy and anxious way to start writing code. Write code The most bogey cranky, sometimes might as well spend more time to think about the best solution for a program is what, and as a novice, more should go to spend this time. Back to the game, if you follow the above thinking to split the diagram out of the code before the import, it will bring a big problem: extensibility. If after you have done this game, the customer or the player put forward: "9 pictures, too little, play very simple ah, boring." "or" split into 120 pictures, a lunatic ah. This is a little game. "or" Alas, this picture is not good-looking, for a try ", this time you want to modify the game, there is no problem found. You have to open the revision software again, a picture of the proportion of the original, one or two times such a job may be you can feel all, in case the customer or the player proposed changes 10 times. That's when someone else played two or three innings lol you're still quietly repairing the map. Then what to do. The second idea, import a complete picture, use the code to cut it. If the can be implemented again, it will only need to use a few variables to control the size and number of the split, so the changes can be done easily. {2. Check whether the stitching is complete} so-called complete, that is, scattered pictures of the correct stitching into the original, if the image of the step to break the picture is good, the problem is easy to solve, just to break the sequence before the order to all the picture number, and then to fill in the small picture of the collision box also numbered, If you detect that the picture number dragged onto the collision box is the same as the collision box number and all are filled in, that means the game is cleared. (The following figure helps to understand)





——————————————————————————————————————————————————— Implementation {1. Cut the picture with code}
To do this, you must first understand the BitmapData type and the Bitmapdata.copypixels method. BitmapData type, you can simply understand, when you new a bitmapdata, it is equal to a new picture. The CopyPixels method is to copy the rectangular area of the source image to a rectangular area of the same size at the target point of the target BitmapData object. The prototype is as follows:

Public CopyPixels (Sourcebitmap:bitmapdata-	the input bitmap image from which to copy pixels. The source image can be another BitmapData instance, which can refer to the current BitmapData instance,
sourcerect:rectangle-			   	A rectangle that defines the area to use as the source image for the input, 
destpoint:				Point-the target points, which represent the upper-left corner of the rectangular area where the new pixels will be placed, 
[alphabitmap:bitmapdata][Optional]-the				    second Alpha BitmapData object source, 
[ alphapoint:point][Optional] 				―alpha points in BitmapData source corresponding to the upper-left corner of the sourcerect parameter, 
[mergealpha:boolean][optional] 			- Boolean value: To use the Alpha channel, set the value to true. To copy pixels without Alpha channel, set to False
): Void


After understanding these two things, it is good to do;
var playerbmp:array = new array  ; <span style= "White-space:pre" >	</span>//the array for the small image stored for
( var i:int=0; i< division number; i++)
{
	var pos:point = new Point (0,0);
	var rect:rectangle=new Rectangle (need to cut the upper left corner of the area X, need to cut the upper left corner y, rectangular area width, rectangular area height);
	var texturebuf:bitmapdata = new BitmapData (rectangular area width, rectangular area height);
	Texturebuf.copypixels (original bitmapdata,rect,pos);
	Playerbmp[i] = new Array  ;
	Playerbmp[i][0] = i;		Code
	playerbmp[i][1] = new Bitmap (texturebuf,pixelsnapping.auto,true);//Map
}
It is important to note that the next two parameters of new rectangle are not right and bottom, but width and height, which is the width of the rectangular area. Once these are completed, the playerbmp[i][1 is placed in a random position, and the order of disruption is achieved.
{2. Check whether the stitching is complete} The problem is mainly used in the Hittestpoint method, this method is one of the AS3.0 self-contained collision detection method, we should not be unfamiliar, combined with this method in judging playerbmp[i][0] Whether it is consistent with the Code of the collision box being filled.



——————————————————————————————————————————————————— Epilogue Look, actually, this little game is simple, Believe that even the first time to write this game of children's shoes, after reading this should also have a certain way of thinking. And the first time I wrote this kind of article (actually did not write the article. ), do not know whether the write is good or bad, if there are errors, please be sure to point out. Finally, I hope someone will look and hope someone likes it.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.