15 puzzle flashrod forked:1favorite:5lines:82license : MIT License modified : 2008-12-29 08:21:21 Embed Tweet package { import flash.display.Sprite; import flash.events.MouseEvent; import caurina.transitions.Tweener; public class Fifteen extends Sprite { private static const W:int = 465; private static const H:int = 465; private static const U:int = int(W/4); private static const V:int = int(H/4); private var board:Array = []; public function Fifteen() { for (var k:int = 1; k < 17; ++k) { var p:Piece = new Piece(k, U, V); addChild(p); board.push(p); } var i:int, j:int = 3; for (k = 0; k < 20; ++k) { i = int(Math.random()*3); shift(i, j); j = int(Math.random()*3); shift(i, j); } stage.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void { shift(e.stageX/U, e.stageY/V); repaint(); }); repaint(); } private function shift(x:int, y:int):void { if (x>=0 && x<4 && y>=0 && y<4) { for (var i:int = 0; i < 4; ++i) { var p:Piece = board[y*4+i]; if (p.value == 16) { for (; i>x; --i) board[y*4+i] = board[y*4+i-1]; for (; i<x; ++i) board[y*4+i] = board[y*4+i+1]; board[y*4+x] = p; return; } } for (var j:int = 0; j<4; ++j) { p = board[j*4+x]; if (p.value == 16) { for (; j>y; --j) board[j*4+x] = board[(j-1)*4+x]; for (; j<y; ++j) board[j*4+x] = board[(j+1)*4+x]; board[y*4+x] = p; return; } } } } private function repaint():void { for (var j:int = 0; j < 4; ++j) { for (var i:int = 0; i < 4; ++i) { var p:Piece = board[j*4+i]; //p.x = i*U; //p.y = j*V; Tweener.addTween(p, {x:i*U, y:j*V, time:0.1, transition:"easeOutQuad"}); } } } } } import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; class Piece extends Sprite { public var value:int; public function Piece(value:int, w:int, h:int) { this.value = value; var fmt:TextFormat = new TextFormat(); fmt.font = "Verdana"; fmt.size = Math.min(w, h) * .6; var t:TextField = new TextField(); t.defaultTextFormat = fmt; t.text = value < 16 ? String(value) : ""; t.selectable = false; addChild(t); } } Code Fullscreen Preview Fullscreen FlashFit aobyrne : shuffleshuffling tenasaku : パズル katopz : game xor : puzzle game puzzle shuffle shuffling パズル shift stageY stageX MouseEvent.CLICK addChild Math.min Tweener.addTween MouseEvent time addEventListener push font size TextFormat text Math.random String Array Sprite int sort new page view favorite forked pv1430 forked from: 15 puzzle alpicola forked:8 favorite:6lines:104 (diff:107) tag: 15puzzle