tailieunhanh - ActionScript 3.0 Game Programming University, Second Edition phần 5

Ngoài ra, chúng tôi thêm một tài sản dragOffset các mảnh ghép. Chúng tôi sử dụng để vị trí của mỗi phần khoảng cách thích hợp từ con trỏ trong khi kéo: Đây là một một trong những trò chơi mà có thể được sử dụng cho nhiều mục đích khác nhau. Bạn có thể thay thế các bong bóng, cũng như đại bác, với đối tượng nào. | Jigsaw Puzzle Game Í213 Also we add a dragoffset property to the puzzle pieces. We use this to position each piece the proper distance from the cursor while dragging cut bitmap into pieces private function makePuzzlePieces bitmapData BitmapData puzzleobjects new Array for var x uint 0 x numPiecesHoriz x for var y uint 0 y numPiecesVert y create new puzzle piece bitmap and sprite var newPuzzlePieceBitmap Bitmap new Bitmap new BitmapData pieceWidth pieceHeight bitmapData new Rectangle x pieceWidth y pieceHeight pieceWidth pieceHeight new Point 0 0 var newPuzzlePiece Sprite new Sprite newPuzzlePieceBitmap place in bottom sprite newPuzzlePiece create object to store in array var newPuzzleobject object new object new Point x y location in puzzle null offset from cursor newPuzzlePiece clickPuzzlePiece newPuzzleObject randomize locations of pieces shufflePieces The shuffle function picks random locations for all the puzzle pieces. We don t care whether the pieces fall on top of each other or how they are distributed. It should be as if they all just fell out of the box. Figure shows such a random distribution. Wow eBook 214 Chapter 6 Picture Puzzles Sliding and Jigsaw Figure The pieces of the jigsaw puzzle are randomly arranged on the screen. random locations for the pieces public function shufflePieces pick random x and y for var i in puzzleObjects puzzleObjects i . 400 50 puzzleObjects i . 250 50 lock all pieces to 10x10 grid lockPiecesToGrid The last line of shufflePieces calls lockPieceToGrid. This function loops through all the puzzle pieces and moves them to the closest location to a 10x10 grid. For instance if a piece is at 43 87 it is moved to 40 90. The reason for using .