※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


forked from : flashrod's 15 puzzle [diff(107)]

TALK
I solved it, but that last piece never loaded, and in fact it doesnt look like the code checks for completion at all.
at 2009/08/04 18:56:26 by
FORKED
  1. // forked from 5ivestar's forked from: 15 puzzle
  2. // forked from flashrod's 15 puzzle
  3. // photo: Ville Miettinen's Grundvik main house
  4. // http://www.flickr.com/photos/wili/214317898/
  5. package {
  6.     import flash.display.Sprite;
  7.     import flash.display.Loader;
  8.     import flash.display.BitmapData;
  9.     import flash.events.Event;
  10.     import flash.events.MouseEvent;
  11.     import flash.geom.Matrix;
  12.     import flash.net.URLRequest;
  13.     import flash.system.LoaderContext;
  14.     import caurina.transitions.Tweener;
  15.     public class Fifteen extends Sprite {
  16.         private static const W:int = 465;
  17.         private static const H:int = 465;
  18.         private static const U:int = int(W/4);
  19.         private static const V:int = int(H/4);
  20.         private var board:Array = [];
  21.         private var loader:Loader;
  22.         public function Fifteen() {
  23.             loader = new Loader();
  24.             var context:LoaderContext = new LoaderContext(true);
  25.             loader.contentLoaderInfo.addEventListener("complete", loadingComplete);
  26.             loader.load(new URLRequest("http://img.f.hatena.ne.jp/maton_1128/20091212173607"), context);
  27.         }
  28.         public function loadingComplete(e:Event):void {
  29.             var source:BitmapData = new BitmapData(W, H, false);
  30.             var sx:Number = W / loader.width;
  31.             var sy:Number = H / loader.height;
  32.             if (sx > sy) {
  33.                 source.draw(loader, new Matrix(sx, 00, sx, 0, (H - loader.height * sx) / 2), nullnullnulltrue);
  34.             } else {
  35.                 source.draw(loader, new Matrix(sy, 00, sy, (W - loader.width * sy) / 20), nullnullnulltrue);
  36.             }
  37.             for (var k:int = 1; k < 17; ++k) {
  38.                 var p:Piece = new Piece(k, source);
  39.                 addChild(p);
  40.                 board.push(p);
  41.             }
  42.             var i:int, j:int = 3;
  43.             for (k = 0; k < 20; ++k) {
  44.                 i = int(Math.random()*3);
  45.                 shift(i, j);
  46.                 j = int(Math.random()*3);
  47.                 shift(i, j);
  48.             }
  49.             stage.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
  50.                 shift(e.stageX/U, e.stageY/V);
  51.                 repaint();
  52.             });
  53.             repaint();
  54.         }
  55.         private function shift(x:int, y:int):void {
  56.             if (x>=0 && x<4 && y>=0 && y<4) {
  57.                 for (var i:int = 0; i < 4; ++i) {
  58.                     var p:Piece = board[y*4+i];
  59.                     if (p.value == 16) {
  60.                         for (; i>x; --i)
  61.                             board[y*4+i] = board[y*4+i-1];
  62.                         for (; i<x; ++i)
  63.                             board[y*4+i] = board[y*4+i+1];
  64.                         board[y*4+x] = p;
  65.                         return;
  66.                     }
  67.                 }
  68.                 for (var j:int = 0; j<4; ++j) {
  69.                     p = board[j*4+x];
  70.                     if (p.value == 16) {
  71.                         for (; j>y; --j)
  72.                             board[j*4+x] = board[(j-1)*4+x];
  73.                         for (; j<y; ++j)
  74.                             board[j*4+x] = board[(j+1)*4+x];
  75.                         board[y*4+x] = p;
  76.                         return;
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         private function repaint():void {
  82.             for (var j:int = 0; j < 4; ++j) {
  83.                 for (var i:int = 0; i < 4; ++i) {
  84.                     var p:Piece = board[j*4+i];
  85.                     //p.x = i*U;
  86.                     //p.y = j*V;
  87.                     Tweener.addTween(p, {x:i*U, y:j*V, time:0.1, transition:"easeOutQuad"});
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93. import flash.display.Sprite;
  94. import flash.display.Bitmap;
  95. import flash.display.BitmapData;
  96. import flash.geom.Point;
  97. import flash.geom.Rectangle;
  98. class Piece extends Sprite {
  99.     public var value:int;
  100.     public function Piece(value:int, source:BitmapData) {
  101.         this.value = value;
  102.         if (value == 16return;
  103.         var w:Number = source.width / 4;
  104.         var h:Number = source.height / 4;
  105.         var bitmap:BitmapData = new BitmapData(w, h, false);
  106.         var rect:Rectangle = new Rectangle((value-1)%4*w, Math.floor(value/4)*h, w, h);
  107.         bitmap.copyPixels(source, rect, new Point());
  108.         addChild(new Bitmap(bitmap));
  109.     }
  110. }
noswf
  1. // forked from 5ivestar's forked from: 15 puzzle
  2. // forked from flashrod's 15 puzzle
  3. // photo: Ville Miettinen's Grundvik main house
  4. // http://www.flickr.com/photos/wili/214317898/
  5. // コメントを付けたり、関数をまとめたりしながら勉強させてもらっています
  6. package {
  7.     import flash.display.Sprite;
  8.     import flash.display.Loader;
  9.     import flash.display.BitmapData;
  10.     import flash.events.Event;
  11.     import flash.events.MouseEvent;
  12.     import flash.geom.Matrix;
  13.     import flash.net.URLRequest;
  14.     import flash.system.LoaderContext;
  15.     import caurina.transitions.Tweener;
  16.     public class Fifteen extends Sprite {
  17.         // 画像サイズ(渡された画像が大きかったり小さかった場合は、圧縮をかけてこのサイズにする)
  18.     private static const W:int = 465;
  19.     private static const H:int = 465;
  20.         // ピースサイズ
  21.     private static const U:int = int(W/4);
  22.     private static const V:int = int(H/4);
  23.         
  24.         // ボード情報
  25.     private var board:Array = [];
  26.         
  27.         // ローダー
  28.     private var loader:Loader;
  29.         // コンストラクタ
  30.         // 
  31.         // Webから画像を読み込む
  32.         // 画像読み込みにはそれなりに制約があり、単純にURLを変えても絵が変わらない
  33.         // 
  34.         // Loader ... ロードする人
  35.         // LoaderContext ... ロード時のオプション設定等
  36.         // Loader.contentLoaderInfo ... ロードの進行状況に関する情報と、ロードされたファイルに関する統計
  37.         // URLRequest ... URL処理
  38.     public function Fifteen() {
  39.         loader = new Loader();
  40.         var context:LoaderContext = new LoaderContext(true);
  41.         loader.contentLoaderInfo.addEventListener("complete", loadingComplete);
  42.         loader.load(new URLRequest("http://farm1.static.flickr.com/57/214317898_596c96ecb6.jpg"), context);
  43.     }
  44.         // ロード完了後に呼び出されるコールバック
  45.         //
  46.     public function loadingComplete(e:Event):void {
  47.             // 大元の一枚絵
  48.         var source:BitmapData = new BitmapData(W, H, false);
  49.             // 画像のスケーリング
  50.             scalePicture(source);
  51.             // ピースの生成
  52.             createPiece(source);
  53.             // 適当な回数パネルを動かし、画像がグチャグチャになった状態から始める
  54.             shuffle(0);
  55.             
  56.             // マウスがクリックされた時に、スライドさせるためのコールバック
  57.         stage.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
  58.             shift(e.stageX/U, e.stageY/V);
  59.             repaint();
  60.         });
  61.             // 再描画
  62.         repaint();
  63.      }
  64.         // 画像のスケール調整
  65.         // 
  66.         // 色々な方法があると思うが、ここでは画像のアスペクト比を維持しながら、
  67.         // 余白を作らない方法でスケーリングしている。
  68.         //
  69.         // MaxOSXの言葉で言うと、'Aspect Fill'
  70.         // Aspect Fill以外の例もいくつか挙げておく
  71.         //
  72.         private function scalePicture(source:BitmapData):void {
  73.             // 目標画像サイズ / 元画像サイズ
  74.         var sx:Number = W / loader.width;
  75.         var sy:Number = H / loader.height;
  76.             // 一枚絵の生成
  77.             //
  78.             // Matrixは a, b, c, d, tx, ty の順番であることに注意、行列で書くと・・・
  79.             // [a, b, tx]
  80.             // [c, d, ty]
  81.             // えー。
  82.             var mtx:Matrix;
  83.             // 行列を使ってスケール調整
  84.             // Aspect Fill
  85.          if (sx > sy) {
  86.                  // [sx,  0, 0]
  87.                  // [ 0, sx, (H - loader.height * sx) / 2)]
  88.                  mtx = new Matrix(sx, 00, sx, 0, (H - loader.height * sx) / 2);
  89.          source.draw(loader, mtx, nullnullnulltrue);
  90.          } else {
  91.                  // [sy,  0, (W - loader.width * sy) / 2]
  92.                  // [ 0, sy, 0]
  93.                  mtx = new Matrix(sy, 00, sy, (W - loader.width * sy) / 20);
  94.          source.draw(loader, mtx, nullnullnulltrue);
  95.          }
  96. /*
  97.             // Scale To Fit
  98.             mtx = new Matrix(sx, 0, 0, sy, 0, 0);
  99.             source.draw(loader, mtx, null, null, null, true);
  100. */
  101. /*
  102.             // Aspect Fit
  103.          if (sx > sy) {
  104.                  mtx = new Matrix(sy, 0, 0, sy, 0, 0);
  105.          source.draw(loader, mtx, null, null, null, true);
  106.          } else {
  107.                 mtx = new Matrix(sx, 0, 0, sx, 0, 0);
  108.          source.draw(loader, mtx, null, null, null, true);
  109.          }
  110. */
  111.         }
  112.         // パネルの生成
  113.         private function createPiece(source:BitmapData):void {
  114.         for (var k:int = 1; k < 17; ++k) {
  115.         var p:Piece = new Piece(k, source);
  116.         addChild(p);
  117.         board.push(p);
  118.         }
  119.         }
  120.         // シャッフル
  121.         private function shuffle(num:int):void {
  122.         var i:int, j:int = 3;
  123.         for (var k:int = 0; k < num; ++k) {
  124.         i = int(Math.random()*3);
  125.         shift(i, j);
  126.         j = int(Math.random()*3);
  127.         shift(i, j);
  128.         }
  129.         }
  130.         
  131.         // パネルの移動処理
  132.     private function shift(x:int, y:int):void {
  133.         if (x>=0 && x<4 && y>=0 && y<4) {
  134.                 // 横方向へのスライド処理
  135.         for (var i:int = 0; i < 4; ++i) {
  136.             var p:Piece = getPiece(i, y);
  137.             if (p.value == 16) {
  138.             for (; i>x; --i) setPiece(i, y, getPiece(i - 1, y));
  139.             for (; i<x; ++i) setPiece(i, y, getPiece(i + 1, y));
  140.                         setPiece(x, y, p);
  141.             return;
  142.             }
  143.         }
  144.                 // 縦方向へのスライド処理
  145.         for (var j:int = 0; j<4; ++j) {
  146.             p = getPiece(x, j);
  147.             if (p.value == 16) {
  148.             for (; j>y; --j) setPiece(x, j, getPiece(x, j - 1));
  149.             for (; j<y; ++j) setPiece(x, j, getPiece(x, j + 1));
  150.                         setPiece(x, y, p);
  151.             return;
  152.             }
  153.         }
  154.         }
  155.     }
  156.         private function setPiece(x:int, y:int, p:Piece):void {
  157.             board[y*4+x] = p;
  158.         }
  159.         private function getPiece(x:int, y:int):Piece {
  160.             return board[y*4+x];
  161.         }
  162.         // 再描画
  163.     private function repaint():void {
  164.         for (var j:int = 0; j < 4; ++j) {
  165.         for (var i:int = 0; i < 4; ++i) {
  166.             var p:Piece = board[j*4+i];
  167.                     // こっちだとダイレクト移動
  168.             // p.x = i*U;
  169.             // p.y = j*V;
  170.                     
  171.                     // こっちだと補完付き移動
  172.             Tweener.addTween(p, {x:i*U, y:j*V, time:0.1, transition:"easeOutQuad"});
  173.         }
  174.         }
  175.     }
  176.     }
  177. }
  178. import flash.display.Sprite;
  179. import flash.display.Bitmap;
  180. import flash.display.BitmapData;
  181. import flash.geom.Point;
  182. import flash.geom.Rectangle;
  183. // 15パズルにおける、一つのピースを表す
  184. class Piece extends Sprite {
  185.     public var value:int;
  186.     // コンストラクタ
  187.     // 
  188.     // @param value 大元の絵を16分割した際、何番目のピースかを表すインデックス(1〜16)
  189.     // @param source 大元の一枚絵
  190.     public function Piece(value:int, source:BitmapData) {
  191.     this.value = value;
  192.         // 16は空の意味、ビットマップは作成しない
  193.     if (value == 16return;
  194.         // このピース用のビットマップのサイズ
  195.     var w:Number = source.width / 4;
  196.     var h:Number = source.height / 4;
  197.         
  198.         // このピース用のビットマップ
  199.     var bitmap:BitmapData = new BitmapData(w, h, false);
  200.         // 切り取り用の矩形
  201.         // 1〜15のインデックスに対して、切り取り開始位置を決定
  202.     var rect:Rectangle = new Rectangle((value-1)%4*w, Math.floor(value/4)*h, w, h);
  203.         // ビットマップを作成!!
  204.     bitmap.copyPixels(source, rect, new Point());
  205.         // 自身の子供に、作成したビットマップを接続
  206.     addChild(new Bitmap(bitmap));
  207.     }
  208. }
noswf

forked from: 15 puzzle 99 puzzle [diff(172)]

  1. package {
  2.   import flash.display.Sprite;
  3.   import flash.net.URLRequest;
  4.   import flash.events.Event;
  5.   import flash.display.BitmapData;
  6.   import flash.display.Loader;
  7.   import flash.geom.Matrix;
  8.   import flash.system.LoaderContext;
  9.   public class NinetyNinePuzzle extends Sprite {
  10.     public var puzzle:SlidingBlockPuzzle;
  11.     private var loader:Loader;
  12.     public static const IMGURL:String = "http://farm4.static.flickr.com/3605/3464779547_ee90cb1066.jpg";
  13.     public static const W:uint = 465, H:uint = 465;
  14.     public function NinetyNinePuzzle() {
  15.       addEventListener(Event.ADDED_TO_STAGE,init);
  16.     }
  17.     private function init(e:Event):void {
  18.       loader = new Loader();
  19.       var context:LoaderContext = new LoaderContext(true);
  20.       loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onImageLoaded);
  21.       loader.load(new URLRequest(IMGURL),context);
  22.     }
  23.     private function onImageLoaded(e:Event):void {
  24.       var source:BitmapData = new BitmapData(W,H,false);
  25.       var sx:Number = W / loader.width;
  26.       var sy:Number = H / loader.height;
  27.       if (sx > sy)
  28.         source.draw(loader, new Matrix(sx, 00, sx, 0, (H - loader.height * sx) / 2), nullnullnulltrue);
  29.       else
  30.         source.draw(loader, new Matrix(sy, 00, sy, (W - loader.width * sy) / 20), nullnullnulltrue);
  31.       puzzle = new SlidingBlockPuzzle(source,10,10,1);
  32.       Block.border = 0;
  33.       addChild(puzzle);
  34.     }
  35.   }
  36. }
  37. import flash.display.Sprite;
  38. import flash.display.BitmapData;
  39. import flash.events.Event;
  40. import flash.events.MouseEvent;
  41. import flash.geom.Matrix;
  42. import caurina.transitions.Tweener;
  43. class SlidingBlockPuzzle extends Sprite {
  44.   public var board:Array,difficult:uint;
  45.   private var _skip:uint,_src:BitmapData,dummy:Sprite,_cols:uint,_rows:uint;
  46.   public function SlidingBlockPuzzle(src:BitmapData,cols:uint,rows:uint,skip:uint) {
  47.     _cols = cols; _rows = rows; _skip = skip; _src = src;
  48.     dummy = new Sprite();
  49.     dummy.graphics.beginFill(0,0);
  50.     dummy.graphics.drawRect(0,0,src.width,src.height);
  51.     dummy.graphics.endFill();
  52.     addChild(dummy);
  53.     addEventListener(Event.ADDED_TO_STAGE,init);
  54.   }
  55.   public function get skip():uint { return _skip; }
  56.   public function get cols():uint { return _cols; }
  57.   public function get rows():uint { return _rows; }
  58.   public function get src():BitmapData { return _src; }
  59.   private function init(e:Event=null):void {
  60.     board = [];
  61.     width = src.width;
  62.     height = src.height;
  63.     var k:uint,i:uint,j:uint=cols-1;
  64.     for(k=1;k<=cols*rows;++k) {
  65.       var p:Block = new Block(k,this);
  66.       addChild(p);
  67.       board.push(p);
  68.     }
  69.     addChild(dummy);
  70.     difficult = difficult || cols*(rows+1)*2;
  71.     for(k=0;k<difficult;k++) {
  72.       i = ~~(Math.random()*cols);
  73.       shift(i,j);
  74.       j = ~~(Math.random()*rows);
  75.       shift(i,j);
  76.     }
  77.     addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
  78.       shift(e.localX/(src.width/cols), e.localY/(src.height/rows));
  79.       repaint();
  80.     });
  81.     repaint(true);
  82.     removeEventListener(Event.ADDED_TO_STAGE,init);
  83.   }
  84.   private function shift(x:uint,y:uint):void {
  85.     if (x>=0&&x<cols&&y>=0&&y<rows) {
  86.       for (var i:int=0;i<rows;++i) {
  87.         var p:Block = board[y*rows+i];
  88.         if (p.skip) {
  89.           for (; i>x; --i)
  90.             board[y*rows+i] = board[y*rows+i-1];
  91.           for (; i<x; ++i)
  92.             board[y*rows+i] = board[y*rows+i+1];
  93.           board[y*rows+x] = p;
  94.           return;
  95.         }
  96.       }
  97.       for (var j:int = 0; j<cols; ++j) {
  98.         p = board[j*cols+x];
  99.         if (p.skip) {
  100.           for (; j>y; --j)
  101.             board[j*cols+x] = board[(j-1)*cols+x];
  102.           for (; j<y; ++j)
  103.           board[j*cols+x] = board[(j+1)*cols+x];
  104.           board[y*cols+x] = p;
  105.           return;
  106.         }
  107.       }
  108.     }
  109.   }
  110.   private function checkComplete(e:Event=null):void {
  111.     for(var i:uint=1;i<=board.length;++i) {
  112.       var p:Block = board[i-1];
  113.       if(p.value!=i) return;
  114.     }
  115.     dispatchEvent(new Event(Event.COMPLETE));
  116.   }
  117.   private function repaint(notween:Boolean=false):void {
  118.     for (var j:int=0;j<rows;++j) {
  119.       for (var i:int=0;i<cols;++i) {
  120.         var p:Block = board[j*cols+i];
  121.         var tgx:uint = uint(i*src.width/cols);
  122.         var tgy:uint = uint(j*src.height/rows);
  123.         if(notween) {
  124.           p.x = tgx; p.y = tgy;
  125.         } else
  126.           Tweener.addTween(p, {x:tgx, y:tgy, time:0.1, transition:"easeOutQuad",onComplete:!(j+i)?checkComplete:null});
  127.       }
  128.     }
  129.   }
  130. }
  131. import flash.display.Sprite;
  132. import flash.display.Bitmap;
  133. import flash.display.BitmapData;
  134. import flash.geom.Rectangle;
  135. import flash.geom.Point;
  136. class Block extends Sprite {
  137.   public var value:uint;
  138.   public var skip:Boolean;
  139.   public static var border:uint = 1;
  140.   public static var embossBitmapData:BitmapData = null;
  141.   public function Block(value:uint,puzzle:SlidingBlockPuzzle) {
  142.     this.value = value;
  143.     this.skip = puzzle.skip == value;
  144.     var src:BitmapData = puzzle.src;
  145.     if(skip) return;
  146.     var w:uint = ~~(src.width/puzzle.cols);
  147.     var h:uint = ~~(src.height/puzzle.rows);
  148.     var bitmap:BitmapData = new BitmapData(w-border*2, h-border*2false);
  149.     var rect:Rectangle = new Rectangle((value-1)%puzzle.cols*w+border, Math.floor((value-1)/puzzle.rows)*h+border, w-border*2, h-border*2);
  150.     bitmap.copyPixels(src, rect, new Point());
  151.     var bmp:Bitmap = new Bitmap(bitmap);
  152.     bmp.x = border; bmp.y = border;
  153.     addChild(bmp);
  154.     if(embossBitmapData) {
  155.       addChild(new Bitmap(embossBitmapData));
  156.     }
  157.   }
  158. }
noswf
  1. // forked from 5ivestar's forked from: 15 puzzle
  2. // forked from flashrod's 15 puzzle
  3. // photo: Ville Miettinen's Grundvik main house
  4. // http://www.flickr.com/photos/wili/214317898/
  5. package {
  6.     import flash.display.Sprite;
  7.     import flash.display.Loader;
  8.     import flash.display.BitmapData;
  9.     import flash.events.Event;
  10.     import flash.events.MouseEvent;
  11.     import flash.geom.Matrix;
  12.     import flash.net.URLRequest;
  13.     import flash.system.LoaderContext;
  14.     import caurina.transitions.Tweener;
  15.     public class Fifteen extends Sprite {
  16.         private static const W:int = 465;
  17.         private static const H:int = 465;
  18.         private static const U:int = int(W/4);
  19.         private static const V:int = int(H/4);
  20.         private var board:Array = [];
  21.         private var loader:Loader;
  22.         public function Fifteen() {
  23.             loader = new Loader();
  24.             var context:LoaderContext = new LoaderContext(true);
  25.             loader.contentLoaderInfo.addEventListener("complete", loadingComplete);
  26.             loader.load(new URLRequest("http://farm1.static.flickr.com/57/214317898_596c96ecb6.jpg"), context);
  27.         }
  28.         public function loadingComplete(e:Event):void {
  29.             var source:BitmapData = new BitmapData(W, H, false);
  30.             var sx:Number = W / loader.width;
  31.             var sy:Number = H / loader.height;
  32.             if (sx > sy) {
  33.                 source.draw(loader, new Matrix(sx, 00, sx, 0, (H - loader.height * sx) / 2), nullnullnulltrue);
  34.             } else {
  35.                 source.draw(loader, new Matrix(sy, 00, sy, (W - loader.width * sy) / 20), nullnullnulltrue);
  36.             }
  37.             for (var k:int = 1; k < 17; ++k) {
  38.                 var p:Piece = new Piece(k, source);
  39.                 addChild(p);
  40.                 board.push(p);
  41.             }
  42.             var i:int, j:int = 3;
  43.             for (k = 0; k < 20; ++k) {
  44.                 i = int(Math.random()*3);
  45.                 shift(i, j);
  46.                 j = int(Math.random()*3);
  47.                 shift(i, j);
  48.             }
  49.             stage.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
  50.                 shift(e.stageX/U, e.stageY/V);
  51.                 repaint();
  52.             });
  53.             repaint();
  54.         }
  55.         private function shift(x:int, y:int):void {
  56.             if (x>=0 && x<4 && y>=0 && y<4) {
  57.                 for (var i:int = 0; i < 4; ++i) {
  58.                     var p:Piece = board[y*4+i];
  59.                     if (p.value == 16) {
  60.                         for (; i>x; --i)
  61.                             board[y*4+i] = board[y*4+i-1];
  62.                         for (; i<x; ++i)
  63.                             board[y*4+i] = board[y*4+i+1];
  64.                         board[y*4+x] = p;
  65.                         return;
  66.                     }
  67.                 }
  68.                 for (var j:int = 0; j<4; ++j) {
  69.                     p = board[j*4+x];
  70.                     if (p.value == 16) {
  71.                         for (; j>y; --j)
  72.                             board[j*4+x] = board[(j-1)*4+x];
  73.                         for (; j<y; ++j)
  74.                             board[j*4+x] = board[(j+1)*4+x];
  75.                         board[y*4+x] = p;
  76.                         return;
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         private function repaint():void {
  82.             for (var j:int = 0; j < 4; ++j) {
  83.                 for (var i:int = 0; i < 4; ++i) {
  84.                     var p:Piece = board[j*4+i];
  85.                     //p.x = i*U;
  86.                     //p.y = j*V;
  87.                     Tweener.addTween(p, {x:i*U, y:j*V, time:0.1, transition:"easeOutQuad"});
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93. import flash.display.Sprite;
  94. import flash.display.Bitmap;
  95. import flash.display.BitmapData;
  96. import flash.geom.Point;
  97. import flash.geom.Rectangle;
  98. class Piece extends Sprite {
  99.     public var value:int;
  100.     public function Piece(value:int, source:BitmapData) {
  101.         this.value = value;
  102.         if (value == 16return;
  103.         var w:Number = source.width / 4;
  104.         var h:Number = source.height / 4;
  105.         var bitmap:BitmapData = new BitmapData(w, h, false);
  106.         var rect:Rectangle = new Rectangle((value-1)%4*w, Math.floor(value/4)*h, w, h);
  107.         bitmap.copyPixels(source, rect, new Point());
  108.         addChild(new Bitmap(bitmap));
  109.     }
  110. }
noswf
  1. // forked from 5ivestar's forked from: 15 puzzle
  2. // forked from flashrod's 15 puzzle
  3. // photo: Ville Miettinen's Grundvik main house
  4. // http://www.flickr.com/photos/wili/214317898/
  5. package {
  6.     import flash.display.Sprite;
  7.     import flash.display.Loader;
  8.     import flash.display.BitmapData;
  9.     import flash.events.Event;
  10.     import flash.events.MouseEvent;
  11.     import flash.geom.Matrix;
  12.     import flash.net.URLRequest;
  13.     import flash.system.LoaderContext;
  14.     import caurina.transitions.Tweener;
  15.     public class Fifteen extends Sprite {
  16.         private static const W:int = 465;
  17.         private static const H:int = 465;
  18.         private static const U:int = int(W/4);
  19.         private static const V:int = int(H/4);
  20.         private var board:Array = [];
  21.         private var loader:Loader;
  22.         public function Fifteen() {
  23.             loader = new Loader();
  24.             var context:LoaderContext = new LoaderContext(true);
  25.             loader.contentLoaderInfo.addEventListener("complete", loadingComplete);
  26.             loader.load(new URLRequest("http://farm1.static.flickr.com/57/214317898_596c96ecb6.jpg"), context);
  27.         }
  28.         public function loadingComplete(e:Event):void {
  29.             var source:BitmapData = new BitmapData(W, H, false);
  30.             var sx:Number = W / loader.width;
  31.             var sy:Number = H / loader.height;
  32.             if (sx > sy) {
  33.                 source.draw(loader, new Matrix(sx, 00, sx, 0, (H - loader.height * sx) / 2), nullnullnulltrue);
  34.             } else {
  35.                 source.draw(loader, new Matrix(sy, 00, sy, (W - loader.width * sy) / 20), nullnullnulltrue);
  36.             }
  37.             for (var k:int = 1; k < 17; ++k) {
  38.                 var p:Piece = new Piece(k, source);
  39.                 addChild(p);
  40.                 board.push(p);
  41.             }
  42.             var i:int, j:int = 3;
  43.             for (k = 0; k < 20; ++k) {
  44.                 i = int(Math.random()*3);
  45.                 shift(i, j);
  46.                 j = int(Math.random()*3);
  47.                 shift(i, j);
  48.             }
  49.             stage.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
  50.                 shift(e.stageX/U, e.stageY/V);
  51.                 repaint();
  52.             });
  53.             repaint();
  54.         }
  55.         private function shift(x:int, y:int):void {
  56.             if (x>=0 && x<4 && y>=0 && y<4) {
  57.                 for (var i:int = 0; i < 4; ++i) {
  58.                     var p:Piece = board[y*4+i];
  59.                     if (p.value == 16) {
  60.                         for (; i>x; --i)
  61.                             board[y*4+i] = board[y*4+i-1];
  62.                         for (; i<x; ++i)
  63.                             board[y*4+i] = board[y*4+i+1];
  64.                         board[y*4+x] = p;
  65.                         return;
  66.                     }
  67.                 }
  68.                 for (var j:int = 0; j<4; ++j) {
  69.                     p = board[j*4+x];
  70.                     if (p.value == 16) {
  71.                         for (; j>y; --j)
  72.                             board[j*4+x] = board[(j-1)*4+x];
  73.                         for (; j<y; ++j)
  74.                             board[j*4+x] = board[(j+1)*4+x];
  75.                         board[y*4+x] = p;
  76.                         return;
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.         private function repaint():void {
  82.             for (var j:int = 0; j < 4; ++j) {
  83.                 for (var i:int = 0; i < 4; ++i) {
  84.                     var p:Piece = board[j*4+i];
  85.                     //p.x = i*U;
  86.                     //p.y = j*V;
  87.                     Tweener.addTween(p, {x:i*U, y:j*V, time:0.1, transition:"easeOutQuad"});
  88.                 }
  89.             }
  90.         }
  91.     }
  92. }
  93. import flash.display.Sprite;
  94. import flash.display.Bitmap;
  95. import flash.display.BitmapData;
  96. import flash.geom.Point;
  97. import flash.geom.Rectangle;
  98. class Piece extends Sprite {
  99.     public var value:int;
  100.     public function Piece(value:int, source:BitmapData) {
  101.         this.value = value;
  102.         if (value == 16return;
  103.         var w:Number = source.width / 4;
  104.         var h:Number = source.height / 4;
  105.         var bitmap:BitmapData = new BitmapData(w, h, false);
  106.         var rect:Rectangle = new Rectangle((value-1)%4*w, Math.floor(value/4)*h, w, h);
  107.         bitmap.copyPixels(source, rect, new Point());
  108.         addChild(new Bitmap(bitmap));
  109.     }
  110. }
noswf
Get Adobe Flash Player