Get started with animation, and use actionscript3.0 to implement [jigsaw puzzle]

Source: Internet
Author: User

Recently, I read a book and saw as's operations on bitmap. In order to practice, I implemented a jigsaw puzzle game.

The main idea is to load and split the image, disrupt the image order, and traverse the image to determine the position.

I sorted out some source code, added comments, and provided them for sharing.

Let's take a look at the results first. The pictures use a game developed by our company (warriorepic). If you are free to play
 

Start the game. If the game does not respond, click flash and try again.


Package {<br/> Import flash. display. bitmap; <br/> Import flash. display. bitmapdata; <br/> Import flash. display. loader; <br/> Import flash. display. sprite; <br/> Import flash. events. event; <br/> Import flash. events. keyboardevent; <br/> Import flash. geom. point; <br/> Import flash. geom. rectangle; <br/> Import flash.net. URLRequest; <br/> Import flash. text. textfield; <br/> Import flash. UI. keyboard; </P> <p> [SWF (width = "500", Height = "350")] <br/> public class gettogether extends sprite <br/>{< br/> private VaR _ Loader: loader; <br/> private VaR _ rownum: Int = 3; <br/> private VaR _ columnnum: Int = 4; <br/> private VaR _ imgarr: array; <br/> private VaR _ blockwidth: Number; <br/> private VaR _ blockheight: Number; <br/> private VaR _ padding: Int = 1; <br/> private VaR _ lastblock: bitmapwithindex; <br/> Public Function gettogether (): void <br/>{< br/> // load the image <br/> _ loader = new loader (); <br/> _ loader. load (New URLRequest ("http: // www. gaoxiaos. COM/mytest/1.jpg"); <br/> _ loader. contentloaderinfo. addeventlistener (event. complete, onloadedimg); <br/>}< br/> private function onloadedimg (E: Event ): void <br/> {<br/> // splits the image and writes it to an array <br/> var loadimg: bitmap = Bitmap (_ loader. content); <br/> _ blockwidth = loadimg. widt H/_ columnnum; <br/> _ blockheight = loadimg. height/_ rownum; <br/> _ imgarr = new array (); <br/> for (var I: Int = 0; I <_ rownum; I ++) <br/>{< br/> _ imgarr [I] = new array (); <br/> for (VAR J: Int = 0; j <_ columnnum; j ++) <br/>{< br/> var tempbitmap: bitmapdata = new bitmapdata (_ blockwidth, _ blockheight); <br/> tempbitmap. copypixels (loadimg. bitmapdata, new rectangle (_ blockwidth * j, _ blockheight * I, _ blockwidth, _ blockhei Ght), new point (); <br/> _ imgarr [I] [J] = new bitmapwithindex (tempbitmap, I, j); <br/> if (I! = (_ RowNum-1) | j! = (_ ColumnNum-1) <br/>{< br/> This. addchild (_ imgarr [I] [J]); <br/>}< br/> else <br/> {<br/> _ lastblock = _ imgarr [I] [J]; <br/>}< br/> shuffle (); <br/> show (); <br/> This. stage. addeventlistener (keyboardevent. key_up, onmove); <br/>}< br/> // shuffling <br/> private function shuffle (): void <br/> {<br/> for (var I: Int = 0; I <100; I ++) <br/>{< br/> var R: number = math. random (); <br/> If (r <0.25) <BR/>{< br/> move (Keyboard. UP); <br/>}< br/> else if (r <0.5) <br/>{< br/> move (Keyboard. down); <br/>}< br/> else if (r <0.75) <br/>{< br/> move (Keyboard. left); <br/>}< br/> else <br/>{< br/> move (Keyboard. right); <br/>}< br/> private function onmove (E: keyboardevent ): void {<br/> move (E. keycode); <br/> // judgment successful <br/> var issuccess: Boolean = true; <br/> for (var I: Int = 0; I <this. _ rown Um; I ++) <br/>{< br/> for (VAR J: Int = 0; j <this. _ columnnum; j ++) <br/>{< br/> If (this. _ imgarr [I] [J]. i! = I | this. _ imgarr [I] [J]. J! = J) <br/>{< br/> issuccess = false; <br/> break; <br/>}< br/> If (issuccess) <br/>{< br/> This. addchild (_ lastblock); <br/> This. stage. removeeventlistener (keyboardevent. key_up, onmove); <br/> var field: textfield = new textfield (); <br/> field. TEXT = "successful (SUCCESS )!!!!! "; <Br/> field. textcolor = 0xff0000; <br/> This. addchild (field); <br/>}< br/> show (); <br/>}< br/> // move <br/> private function move (E: uint): void <br/>{< br/> // locate the empty position <br/> var emptyi: int; <br/> var emptyj: int; <br/> for (var I: Int = 0; I <this. _ rownum; I ++) <br/>{< br/> for (VAR J: Int = 0; j <this. _ columnnum; j ++) <br/>{< br/> If (this. _ imgarr [I] [J]. I = _ lastblock. I & this. _ imgarr [I] [J]. j = _ lastblock. j) <Br/>{< br/> emptyi = I; <br/> emptyj = J; <br/>}< br/> // move to an empty location based on the direction <br/> If (E = keyboard. up) <br/>{< br/> If (emptyi! = This. _ rowNum-1) <br/>{< br/> This. _ imgarr [emptyi] [emptyj] = This. _ imgarr [emptyi + 1] [emptyj] <br/> This. _ imgarr [emptyi + 1] [emptyj] = This. _ lastblock; <br/>}< br/> else if (E = keyboard. down) <br/>{< br/> If (emptyi! = 0) <br/>{< br/> This. _ imgarr [emptyi] [emptyj] = This. _ imgarr [emptyI-1] [emptyj] <br/> This. _ imgarr [emptyI-1] [emptyj] = This. _ lastblock; <br/>}< br/> else if (E = keyboard. left) <br/> {<br/> If (emptyj! = This. _ columnNum-1) <br/>{< br/> This. _ imgarr [emptyi] [emptyj] = This. _ imgarr [emptyi] [emptyj + 1] <br/> This. _ imgarr [emptyi] [emptyj + 1] = This. _ lastblock; <br/>}< br/> else if (E = keyboard. right) <br/>{< br/> If (emptyj! = 0) <br/>{< br/> This. _ imgarr [emptyi] [emptyj] = This. _ imgarr [emptyi] [emptyJ-1] <br/> This. _ imgarr [emptyi] [emptyJ-1] = This. _ lastblock; <br/>}< br/> private function show (): void <br/> {<br/> for (var I: Int = 0; I <this. _ rownum; I ++) <br/>{< br/> for (VAR J: Int = 0; j <this. _ columnnum; j ++) <br/>{< br/> This. _ imgarr [I] [J]. X = J * This. _ blockwidth + J * _ padding; <br/> This. _ imgarr [I] [J]. y = I * This. _ blockheight + I * _ padding; <br/>}< br/>

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.