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

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

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


FORKED
  1. // forked from Aquioux's Wonderwall のパチモン
  2. package {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.Loader;
  6.     import flash.display.Sprite;
  7.     import flash.events.Event;
  8.     import flash.net.URLRequest;
  9.     import flash.system.LoaderContext;
  10.     [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#000000")]
  11.     /**
  12.      * Wonderwall のパチモン
  13.      * @author YOSHIDA, Akio (Aquioux)
  14.      */
  15.     public class Main extends Sprite {
  16.         private const DISTANCE_OF_REACTION:int = 125;    // マウスに反応する距離
  17.         private const SPRING:Number   = 0.01;            // バネ係数
  18.         private const FRICTION:Number = 0.9;            // 摩擦係数
  19.         private const NUM_OF_ROW:int = 3;    // 横方向の数
  20.         private const NUM_OF_COL:int = 3;    // 縦方向の数
  21.         private const IMAGE_WIDTH:int  = 100;
  22.         private const IMAGE_HEIGHT:int = 100;
  23.         
  24.         private var imageUrls:Vector.<String> = Vector.<String>([
  25.             "http://assets.wonderfl.net/images/related_images/5/59/59ce/59ce4dc0471ca9405ef2d0ed2dc1cea5e70c91c1",
  26.             "http://assets.wonderfl.net/images/related_images/7/71/716a/716a033cf4afbec77f72d124e1903b1ff05cbbda",
  27.             "http://assets.wonderfl.net/images/related_images/8/87/871a/871a53382ac812425d86de3888971e86fb3b956b",
  28.             "http://assets.wonderfl.net/images/related_images/7/74/74f2/74f2c57ef48e0d2b88be0bb3999ad15c110bcaf2",
  29.             "http://assets.wonderfl.net/images/related_images/c/c5/c5ef/c5efc60973f19249b625ca1b0672b2b359d63eee",
  30.             "http://assets.wonderfl.net/images/related_images/b/b4/b43b/b43bf737d1fe33d1c083c8e9e04f97467751df96",
  31.             "http://assets.wonderfl.net/images/related_images/3/3d/3dcd/3dcd1527c2f968c5b4e39471303b690d28e28687",
  32.             "http://assets.wonderfl.net/images/related_images/7/76/765f/765f0fef8e179cb6d384b85e972879be8e003f69",
  33.             "http://assets.wonderfl.net/images/related_images/e/ee/ee94/ee94f456247434eee366c2c6a6362b8a7db03f8e"
  34.         ]);
  35.         private var images:Vector.<BitmapData> = new Vector.<BitmapData>(9true);
  36.         private var cnt:uint = 0;
  37.         private const NUM_OF_IMAGE:uint = NUM_OF_ROW * NUM_OF_COL;
  38.         
  39.         public function Main() {
  40.             imageLoad();
  41.         }
  42.         
  43.         private function imageLoad():void {
  44.             var loader:Loader = new Loader();
  45.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  46.             var url:String = imageUrls[cnt];
  47.             loader.load(new URLRequest(url), new LoaderContext(true));
  48.         }
  49.         private function completeHandler(event:Event):void {
  50.             var loader:Loader = event.target.loader;
  51.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
  52.             images[cnt] = Bitmap(loader.content).bitmapData;
  53.             cnt++;
  54.             cnt >= NUM_OF_IMAGE ? next() : imageLoad();
  55.         }
  56.             
  57.         private function next():void {
  58.             // Coordinate 初期化
  59.             Coordinate.spring   = SPRING;
  60.             Coordinate.friction = FRICTION;
  61.             Coordinate.distanceOfReaction = DISTANCE_OF_REACTION;
  62.         
  63.             // データ生成
  64.             var dataFactory:DataFactory = new DataFactory();
  65.             dataFactory.numOfCol = NUM_OF_COL;
  66.             dataFactory.numOfRow = NUM_OF_ROW;
  67.             dataFactory.cellWidth  = IMAGE_WIDTH;
  68.             dataFactory.cellHeight = IMAGE_HEIGHT;
  69.             dataFactory.stageWidth  = stage.stageWidth;
  70.             dataFactory.stageHeight = stage.stageHeight;
  71.             dataFactory.start();
  72.             
  73.             // Model 生成
  74.             var model:Model = new Model();
  75.             model.coordinates = dataFactory.coordinates;
  76.             // View 生成
  77.             var viewLayer:Sprite = new Sprite();
  78.             addChild(viewLayer);
  79.             var n:uint = NUM_OF_ROW * NUM_OF_COL;
  80.             for (var i:int = 0; i < n; i++) {
  81.                 var view:View = new View(model);
  82.                 view.bitmapData = images[i];
  83.                 view.setBounds(i, dataFactory.bounds);
  84.                 view.setup();
  85.                 viewLayer.addChild(view);
  86.             }
  87.             // Controller 生成
  88.             var controller:Controller = new Controller(model);
  89.         }
  90.     }
  91. }
  92.     import flash.geom.Point;
  93.     /**
  94.      * データ生成クラス
  95.      * @author YOSHIDA, Akio (Aquioux)
  96.      */
  97.     class DataFactory {
  98.         // 外部へ出力するデータ
  99.         public function get coordinates():Vector.<Coordinate> { return _coordinates; }
  100.         private var _coordinates:Vector.<Coordinate>;    // すべての頂点(for Model)
  101.         
  102.         public function get bounds():Vector.<int> { return _bounds; }
  103.         private var _bounds:Vector.<int>;                // セルの四隅の頂点の組み合わせ(for View)
  104.         
  105.         // 外部から入力するデータ
  106.         public function set numOfRow(value:int):void { _numOfRow = value; }
  107.         private var _numOfRow:uint;        // 横方向のセル数
  108.         public function set numOfCol(value:int):void { _numOfCol = value; }
  109.         private var _numOfCol:uint;        // 縦方向のセル数
  110.         public function set cellWidth(value:Number):void { _cellWidth = value; }
  111.         private var _cellWidth:Number;    // セル幅
  112.         public function set cellHeight(value:Number):void { _cellHeight = value; }
  113.         private var _cellHeight:Number;    // セル高
  114.         
  115.         public function set stageWidth(value:Number):void { _stageWidth = value; }
  116.         private var _stageWidth:Number;        // ステージ幅
  117.         public function set stageHeight(value:Number):void { _stageHeight = value; }
  118.         private var _stageHeight:Number;    // ステージ高
  119.         
  120.         
  121.         // 内部だけで使用するデータ
  122.         private var offsetX:Number;    // X座標オフセット値
  123.         private var offsetY:Number;    // Y座標オフセット値
  124.         
  125.         public function DataFactory() {}
  126.         
  127.         // 出力データ作成開始
  128.         // このメソッドは入力データをすべて設定してから実行すること
  129.         public function start():void {
  130.             var totalWidth:int  = _numOfRow * _cellWidth;
  131.             var totalHeight:int = _numOfCol * _cellHeight;
  132.             var offsetX:Number = (_stageWidth - totalWidth) / 2;
  133.             var offsetY:Number = (_stageHeight - totalHeight) / 2;
  134.             
  135.             // 全頂点
  136.             // 左上端からZ順に各頂点を Coordinete クラスで設定し、その Coordinate インスタンスを Vector に格納
  137.             _coordinates = new Vector.<Coordinate>();
  138.             for (var i:int = 0; i < _numOfCol + 1; i++) {
  139.                 for (var j:int = 0; j < _numOfRow + 1; j++) {
  140.                     var posX:Number = j * _cellWidth  + offsetX + Math.random() * 30 - 15;
  141.                     var posY:Number = i * _cellHeight + offsetY + Math.random() * 30 - 15;
  142.                     var coordinate:Coordinate = new Coordinate(posX, posY);
  143.                     _coordinates.push(coordinate);
  144.                 }
  145.             }
  146.             _coordinates.fixed = true;
  147.             
  148.             // セルの四隅の頂点の組み合わせ
  149.             // 各セルの四隅(左上から時計回り)が全頂点のうちそれぞれ何番目になるのかを、Vector に格納(dtawTriangle の indices と似ている)
  150.             _bounds = new Vector.<int>();
  151.             var cnt:uint = 0;
  152.             for (i = 0; i < _numOfCol; i++) {
  153.                 for (j = 0; j < _numOfRow; j++) {
  154.                     var leftTop:uint     =  i      * (_numOfRow + 1) + j;
  155.                     var rightTop:uint    =  i      * (_numOfRow + 1) + j + 1;
  156.                     var leftBottom:uint  = (i + 1) * (_numOfRow + 1) + j;
  157.                     var rightBottom:uint = (i + 1) * (_numOfRow + 1) + j + 1;
  158.                     _bounds[cnt++] = leftTop;
  159.                     _bounds[cnt++] = rightTop;
  160.                     _bounds[cnt++] = rightBottom;
  161.                     _bounds[cnt++] = leftBottom;
  162.                 }
  163.             }
  164.             _bounds.fixed = true;
  165.         }
  166.     }
  167.     import flash.geom.Point;
  168.     /**
  169.      * 頂点生成クラス
  170.      * @author YOSHIDA, Akio (Aquioux)
  171.      */
  172.     class Coordinate {
  173.         // 外部から入力するデータ
  174.         // 静的変数
  175.         // バネ係数
  176.         static public function set spring(value:Number):void { _spring = value; }
  177.         static private var _spring:Number = 0.01;
  178.         // 抵抗
  179.         static public function set friction(value:Number):void { _friction = value; }
  180.         static private var _friction:Number = 0.9;
  181.         // 反応距離
  182.         static public function set distanceOfReaction(value:Number):void { _distanceOfReaction = value; }
  183.         static private var _distanceOfReaction:Number = 100;
  184.         // 外部へ出力するデータ
  185.         // 現在座標
  186.         public function get x():Number { return _x; }
  187.         private var _x:Number;
  188.         public function get y():Number { return _y; }
  189.         private var _y:Number;
  190.         
  191.         // 内部だけで使用するデータ
  192.         // 既定座標
  193.         private var localX:Number;
  194.         private var localY:Number;
  195.         // 速度
  196.         private var vx:Number = 0.0;
  197.         private var vy:Number = 0.0;
  198.     
  199.         public function Coordinate(valueX:Number, valueY:Number) {
  200.             _x = localX = valueX;
  201.             _y = localY = valueY;
  202.         }
  203.     
  204.         public function update(mousePoint:Point):void {
  205.             // マウスの位置と自分との距離を求める
  206.             var distance:Number = Point.distance(mousePoint, new Point(localX, localY));
  207.         
  208.             // 到達値
  209.             var dx:Number;
  210.             var dy:Number;
  211.             // 到達値の計算
  212.             if (distance < _distanceOfReaction) {
  213.                 var diff:Number     = -distance * (_distanceOfReaction - distance) / _distanceOfReaction;
  214.                 var radian:Number   = Math.atan2(mousePoint.y - localY, mousePoint.x - localX);
  215.                 var diffPoint:Point = Point.polar(diff*2, radian);
  216.                 dx = localX + diffPoint.x;
  217.                 dy = localY + diffPoint.y;
  218.             } else{    // 位置を元に戻す
  219.                 dx = localX;
  220.                 dy = localY;
  221.             }
  222.         
  223.             vx += (dx - _x) * _spring;
  224.             vy += (dy - _y) * _spring;
  225.             vx *= _friction;
  226.             vy *= _friction;
  227.             _x += vx;
  228.             _y += vy;
  229.         }
  230.     }
  231.     import flash.events.Event;
  232.     import flash.events.EventDispatcher;
  233.     import flash.geom.Point;
  234.     /**
  235.      * Model クラス
  236.      * @author YOSHIDA, Akio (Aquioux)
  237.      */
  238.     class Model extends EventDispatcher {
  239.         // 外部へ出力するデータ
  240.         // 全頂点のXY座標値
  241.         public function get vertices():Vector.<Number> { return _vertices; }
  242.         private var _vertices:Vector.<Number>;
  243.         // 外部から入力するデータ
  244.         // 全頂点データ
  245.         public function set coordinates(value:Vector.<Coordinate>):void {
  246.             _coordinates = value;
  247.             _vertices = new Vector.<Number>(_coordinates.length * 2true);
  248.         }
  249.         private var _coordinates:Vector.<Coordinate>;
  250.         
  251.         
  252.         public function Model() {}
  253.         public function update(mousePoint:Point):void {
  254.             var n:uint = _coordinates.length;
  255.             for (var i:int = 0; i < n; i++) {
  256.                 var c:Coordinate = _coordinates[i];
  257.                 c.update(mousePoint);
  258.                 _vertices[i * 2]     = c.x;
  259.                 _vertices[i * 2 + 1] = c.y;
  260.             }
  261.             dispatchEvent(new Event(Event.CHANGE));
  262.         }
  263.     }
  264.     import flash.display.Bitmap;
  265.     import flash.display.BitmapData;
  266.     import flash.display.GraphicsBitmapFill;
  267.     import flash.display.GraphicsEndFill;
  268.     import flash.display.GraphicsTrianglePath;
  269.     import flash.display.IGraphicsData;
  270.     import flash.display.Sprite;
  271.     import flash.events.Event;
  272.     import flash.events.MouseEvent;
  273.     import flash.geom.ColorTransform;
  274.     import flash.geom.Point;
  275.     /**
  276.      * ...
  277.      * @author YOSHIDA, Akio (Aquioux)
  278.      */
  279.     class View extends Sprite {
  280.         // 外部から入力するデータ
  281.         // 四隅の頂点の、全頂点中の順番
  282.         public function setBounds(idx:uint, value:Vector.<int>):void {
  283.             idx *= 4;
  284.             _bounds = value.slice(idx, idx + 4);
  285.         }
  286.         private var _bounds:Vector.<int>;
  287.         
  288.         // スキンとなる BitmapData
  289.         public function set bitmapData(value:BitmapData):void { _bitmapData = value; }
  290.         private var _bitmapData:BitmapData;
  291.         private var model:Model;
  292.         
  293.         // 内部だけで使用するデータ
  294.         // for drawTriangles
  295.         private var graphicsData:Vector.<IGraphicsData>;
  296.         private var path:GraphicsTrianglePath;
  297.         private var vertices:Vector.<Number>;
  298.         private var indices:Vector.<int>;
  299.         private var uvtData:Vector.<Number>;
  300.         
  301.         static private var topIndex:uint = 0;
  302.         
  303.         private var isVisited:Boolean = false;
  304.         private const visitedColor:ColorTransform = new ColorTransform(0001, Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), 0);
  305.         private const noVisitedColor:ColorTransform = new ColorTransform(11110000);
  306.         
  307.         public function View(model:Model) {
  308.             // 対 Model
  309.             this.model = model;
  310.             model.addEventListener(Event.CHANGE, update);
  311.             addEventListener(MouseEvent.ROLL_OVER, overHandler);
  312.             addEventListener(MouseEvent.ROLL_OUT, outHandler);
  313.             addEventListener(MouseEvent.CLICK, clickHandler);
  314.         }
  315.         
  316.         private function overHandler(event:MouseEvent):void {
  317.             if (topIndex == 0)
  318.                 topIndex = parent.numChildren - 1;
  319.             parent.setChildIndex(this, topIndex);
  320.             
  321.             if (isVisited)
  322.                 transform.colorTransform = noVisitedColor;
  323.         }
  324.         private function outHandler(event:MouseEvent):void {
  325.             if (isVisited)
  326.                 transform.colorTransform = visitedColor;
  327.         }
  328.         
  329.         
  330.         private function clickHandler(event:MouseEvent):void {
  331.             isVisited = true;
  332.             transform.colorTransform = visitedColor;
  333.         }
  334.         
  335.         
  336.         public function setup():void {
  337.             // drawTriangles で使う Vector の設定
  338.             vertices = new Vector.<Number>(8true);
  339.             indices = Vector.<int>([
  340.                 013,
  341.                 123
  342.             ]);
  343.             indices.fixed = true;
  344.             uvtData = Vector.<Number>([
  345.                 00,
  346.                 10,
  347.                 11,
  348.                 01
  349.             ]);
  350.             uvtData.fixed = true;
  351.             
  352.             // graphicsData の設定
  353.             graphicsData = new Vector.<IGraphicsData>();
  354.             graphicsData.push(new GraphicsBitmapFill(_bitmapData));
  355.             graphicsData.push(path = new GraphicsTrianglePath(vertices, indices, uvtData));
  356.             graphicsData.push(new GraphicsEndFill());
  357.         }
  358.         
  359.         private function update(event:Event):void {
  360.             var allVertices:Vector.<Number> = model.vertices;
  361.             for (var i:int = 0; i < 4; i++) {
  362.                 var idx:int = _bounds[i];
  363.                 vertices[i * 2]     = allVertices[idx * 2];
  364.                 vertices[i * 2 + 1] = allVertices[idx * 2 + 1];
  365.             }
  366.             path.vertices = vertices;
  367.             draw();
  368.         }
  369.         private function draw():void {
  370.             graphics.clear();
  371.             graphics.drawGraphicsData(graphicsData);
  372.         }
  373.     }
  374.     import flash.display.Sprite;
  375.     import flash.events.Event;
  376.     import flash.geom.Point;
  377.     /**
  378.      * Controller
  379.      * マウス座標を Model に渡すだけ
  380.      * マウス座標を取得するので Sprite を継承する
  381.      * @author YOSHIDA, Akio (Aquioux)
  382.      */
  383.     class Controller extends Sprite {
  384.         private var model:Model;
  385.         
  386.         public function Controller(model:Model) {
  387.             this.model = model;
  388.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  389.         }
  390.         
  391.         private function enterFrameHandler(event:Event):void {
  392.             var mousePoint:Point = new Point(mouseX, mouseY);
  393.             model.update(mousePoint);
  394.         }
  395.     }
noswf
  1. // forked from Aquioux's Wonderwall のパチモン
  2. package {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.Loader;
  6.     import flash.display.Sprite;
  7.     import flash.events.Event;
  8.     import flash.net.URLRequest;
  9.     import flash.system.LoaderContext;
  10.     [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#000000")]
  11.     /**
  12.      * Wonderwall のパチモン
  13.      * @author YOSHIDA, Akio (Aquioux)
  14.      */
  15.     public class Main extends Sprite {
  16.         private const DISTANCE_OF_REACTION:int = 125;    // マウスに反応する距離
  17.         private const SPRING:Number   = 0.01;            // バネ係数
  18.         private const FRICTION:Number = 0.9;            // 摩擦係数
  19.         private const NUM_OF_ROW:int = 3;    // 横方向の数
  20.         private const NUM_OF_COL:int = 3;    // 縦方向の数
  21.         private const IMAGE_WIDTH:int  = 100;
  22.         private const IMAGE_HEIGHT:int = 100;
  23.         
  24.         private var imageUrls:Vector.<String> = Vector.<String>([
  25.             "http://assets.wonderfl.net/images/related_images/5/59/59ce/59ce4dc0471ca9405ef2d0ed2dc1cea5e70c91c1",
  26.             "http://assets.wonderfl.net/images/related_images/7/71/716a/716a033cf4afbec77f72d124e1903b1ff05cbbda",
  27.             "http://assets.wonderfl.net/images/related_images/8/87/871a/871a53382ac812425d86de3888971e86fb3b956b",
  28.             "http://assets.wonderfl.net/images/related_images/7/74/74f2/74f2c57ef48e0d2b88be0bb3999ad15c110bcaf2",
  29.             "http://assets.wonderfl.net/images/related_images/c/c5/c5ef/c5efc60973f19249b625ca1b0672b2b359d63eee",
  30.             "http://assets.wonderfl.net/images/related_images/b/b4/b43b/b43bf737d1fe33d1c083c8e9e04f97467751df96",
  31.             "http://assets.wonderfl.net/images/related_images/3/3d/3dcd/3dcd1527c2f968c5b4e39471303b690d28e28687",
  32.             "http://assets.wonderfl.net/images/related_images/7/76/765f/765f0fef8e179cb6d384b85e972879be8e003f69",
  33.             "http://assets.wonderfl.net/images/related_images/e/ee/ee94/ee94f456247434eee366c2c6a6362b8a7db03f8e"
  34.         ]);
  35.         private var images:Vector.<BitmapData> = new Vector.<BitmapData>(9true);
  36.         private var cnt:uint = 0;
  37.         private const NUM_OF_IMAGE:uint = NUM_OF_ROW * NUM_OF_COL;
  38.         
  39.         public function Main() {
  40.             imageLoad();
  41.         }
  42.         
  43.         private function imageLoad():void {
  44.             var loader:Loader = new Loader();
  45.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  46.             var url:String = imageUrls[cnt];
  47.             loader.load(new URLRequest(url), new LoaderContext(true));
  48.         }
  49.         private function completeHandler(event:Event):void {
  50.             var loader:Loader = event.target.loader;
  51.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
  52.             images[cnt] = Bitmap(loader.content).bitmapData;
  53.             cnt++;
  54.             cnt >= NUM_OF_IMAGE ? next() : imageLoad();
  55.         }
  56.             
  57.         private function next():void {
  58.             // Coordinate 初期化
  59.             Coordinate.spring   = SPRING;
  60.             Coordinate.friction = FRICTION;
  61.             Coordinate.distanceOfReaction = DISTANCE_OF_REACTION;
  62.         
  63.             // データ生成
  64.             var dataFactory:DataFactory = new DataFactory();
  65.             dataFactory.numOfCol = NUM_OF_COL;
  66.             dataFactory.numOfRow = NUM_OF_ROW;
  67.             dataFactory.cellWidth  = IMAGE_WIDTH;
  68.             dataFactory.cellHeight = IMAGE_HEIGHT;
  69.             dataFactory.stageWidth  = stage.stageWidth;
  70.             dataFactory.stageHeight = stage.stageHeight;
  71.             dataFactory.start();
  72.             
  73.             // Model 生成
  74.             var model:Model = new Model();
  75.             model.coordinates = dataFactory.coordinates;
  76.             // View 生成
  77.             var viewLayer:Sprite = new Sprite();
  78.             addChild(viewLayer);
  79.             var n:uint = NUM_OF_ROW * NUM_OF_COL;
  80.             for (var i:int = 0; i < n; i++) {
  81.                 var view:View = new View(model);
  82.                 view.bitmapData = images[i];
  83.                 view.setBounds(i, dataFactory.bounds);
  84.                 view.setup();
  85.                 viewLayer.addChild(view);
  86.             }
  87.             // Controller 生成
  88.             var controller:Controller = new Controller(model);
  89.         }
  90.     }
  91. }
  92.     import flash.geom.Point;
  93.     /**
  94.      * データ生成クラス
  95.      * @author YOSHIDA, Akio (Aquioux)
  96.      */
  97.     class DataFactory {
  98.         // 外部へ出力するデータ
  99.         public function get coordinates():Vector.<Coordinate> { return _coordinates; }
  100.         private var _coordinates:Vector.<Coordinate>;    // すべての頂点(for Model)
  101.         
  102.         public function get bounds():Vector.<int> { return _bounds; }
  103.         private var _bounds:Vector.<int>;                // セルの四隅の頂点の組み合わせ(for View)
  104.         
  105.         // 外部から入力するデータ
  106.         public function set numOfRow(value:int):void { _numOfRow = value; }
  107.         private var _numOfRow:uint;        // 横方向のセル数
  108.         public function set numOfCol(value:int):void { _numOfCol = value; }
  109.         private var _numOfCol:uint;        // 縦方向のセル数
  110.         public function set cellWidth(value:Number):void { _cellWidth = value; }
  111.         private var _cellWidth:Number;    // セル幅
  112.         public function set cellHeight(value:Number):void { _cellHeight = value; }
  113.         private var _cellHeight:Number;    // セル高
  114.         
  115.         public function set stageWidth(value:Number):void { _stageWidth = value; }
  116.         private var _stageWidth:Number;        // ステージ幅
  117.         public function set stageHeight(value:Number):void { _stageHeight = value; }
  118.         private var _stageHeight:Number;    // ステージ高
  119.         
  120.         
  121.         // 内部だけで使用するデータ
  122.         private var offsetX:Number;    // X座標オフセット値
  123.         private var offsetY:Number;    // Y座標オフセット値
  124.         
  125.         public function DataFactory() {}
  126.         
  127.         // 出力データ作成開始
  128.         // このメソッドは入力データをすべて設定してから実行すること
  129.         public function start():void {
  130.             var totalWidth:int  = _numOfRow * _cellWidth;
  131.             var totalHeight:int = _numOfCol * _cellHeight;
  132.             var offsetX:Number = (_stageWidth - totalWidth) / 2;
  133.             var offsetY:Number = (_stageHeight - totalHeight) / 2;
  134.             
  135.             // 全頂点
  136.             // 左上端からZ順に各頂点を Coordinete クラスで設定し、その Coordinate インスタンスを Vector に格納
  137.             _coordinates = new Vector.<Coordinate>();
  138.             for (var i:int = 0; i < _numOfCol + 1; i++) {
  139.                 for (var j:int = 0; j < _numOfRow + 1; j++) {
  140.                     var posX:Number = j * _cellWidth  + offsetX + Math.random() * 30 - 15;
  141.                     var posY:Number = i * _cellHeight + offsetY + Math.random() * 30 - 15;
  142.                     var coordinate:Coordinate = new Coordinate(posX, posY);
  143.                     _coordinates.push(coordinate);
  144.                 }
  145.             }
  146.             _coordinates.fixed = true;
  147.             
  148.             // セルの四隅の頂点の組み合わせ
  149.             // 各セルの四隅(左上から時計回り)が全頂点のうちそれぞれ何番目になるのかを、Vector に格納(dtawTriangle の indices と似ている)
  150.             _bounds = new Vector.<int>();
  151.             var cnt:uint = 0;
  152.             for (i = 0; i < _numOfCol; i++) {
  153.                 for (j = 0; j < _numOfRow; j++) {
  154.                     var leftTop:uint     =  i      * (_numOfRow + 1) + j;
  155.                     var rightTop:uint    =  i      * (_numOfRow + 1) + j + 1;
  156.                     var leftBottom:uint  = (i + 1) * (_numOfRow + 1) + j;
  157.                     var rightBottom:uint = (i + 1) * (_numOfRow + 1) + j + 1;
  158.                     _bounds[cnt++] = leftTop;
  159.                     _bounds[cnt++] = rightTop;
  160.                     _bounds[cnt++] = rightBottom;
  161.                     _bounds[cnt++] = leftBottom;
  162.                 }
  163.             }
  164.             _bounds.fixed = true;
  165.         }
  166.     }
  167.     import flash.geom.Point;
  168.     /**
  169.      * 頂点生成クラス
  170.      * @author YOSHIDA, Akio (Aquioux)
  171.      */
  172.     class Coordinate {
  173.         // 外部から入力するデータ
  174.         // 静的変数
  175.         // バネ係数
  176.         static public function set spring(value:Number):void { _spring = value; }
  177.         static private var _spring:Number = 0.01;
  178.         // 抵抗
  179.         static public function set friction(value:Number):void { _friction = value; }
  180.         static private var _friction:Number = 0.9;
  181.         // 反応距離
  182.         static public function set distanceOfReaction(value:Number):void { _distanceOfReaction = value; }
  183.         static private var _distanceOfReaction:Number = 100;
  184.         // 外部へ出力するデータ
  185.         // 現在座標
  186.         public function get x():Number { return _x; }
  187.         private var _x:Number;
  188.         public function get y():Number { return _y; }
  189.         private var _y:Number;
  190.         
  191.         // 内部だけで使用するデータ
  192.         // 既定座標
  193.         private var localX:Number;
  194.         private var localY:Number;
  195.         // 速度
  196.         private var vx:Number = 0.0;
  197.         private var vy:Number = 0.0;
  198.     
  199.         public function Coordinate(valueX:Number, valueY:Number) {
  200.             _x = localX = valueX;
  201.             _y = localY = valueY;
  202.         }
  203.     
  204.         public function update(mousePoint:Point):void {
  205.             // マウスの位置と自分との距離を求める
  206.             var distance:Number = Point.distance(mousePoint, new Point(localX, localY));
  207.         
  208.             // 到達値
  209.             var dx:Number;
  210.             var dy:Number;
  211.             // 到達値の計算
  212.             if (distance < _distanceOfReaction) {
  213.                 var diff:Number     = -distance * (_distanceOfReaction - distance) / _distanceOfReaction;
  214.                 var radian:Number   = Math.atan2(mousePoint.y - localY, mousePoint.x - localX);
  215.                 var diffPoint:Point = Point.polar(diff*2, radian);
  216.                 dx = localX + diffPoint.x;
  217.                 dy = localY + diffPoint.y;
  218.             } else{    // 位置を元に戻す
  219.                 dx = localX;
  220.                 dy = localY;
  221.             }
  222.         
  223.             vx += (dx - _x) * _spring;
  224.             vy += (dy - _y) * _spring;
  225.             vx *= _friction;
  226.             vy *= _friction;
  227.             _x += vx;
  228.             _y += vy;
  229.         }
  230.     }
  231.     import flash.events.Event;
  232.     import flash.events.EventDispatcher;
  233.     import flash.geom.Point;
  234.     /**
  235.      * Model クラス
  236.      * @author YOSHIDA, Akio (Aquioux)
  237.      */
  238.     class Model extends EventDispatcher {
  239.         // 外部へ出力するデータ
  240.         // 全頂点のXY座標値
  241.         public function get vertices():Vector.<Number> { return _vertices; }
  242.         private var _vertices:Vector.<Number>;
  243.         // 外部から入力するデータ
  244.         // 全頂点データ
  245.         public function set coordinates(value:Vector.<Coordinate>):void {
  246.             _coordinates = value;
  247.             _vertices = new Vector.<Number>(_coordinates.length * 2true);
  248.         }
  249.         private var _coordinates:Vector.<Coordinate>;
  250.         
  251.         
  252.         public function Model() {}
  253.         public function update(mousePoint:Point):void {
  254.             var n:uint = _coordinates.length;
  255.             for (var i:int = 0; i < n; i++) {
  256.                 var c:Coordinate = _coordinates[i];
  257.                 c.update(mousePoint);
  258.                 _vertices[i * 2]     = c.x;
  259.                 _vertices[i * 2 + 1] = c.y;
  260.             }
  261.             dispatchEvent(new Event(Event.CHANGE));
  262.         }
  263.     }
  264.     import flash.display.Bitmap;
  265.     import flash.display.BitmapData;
  266.     import flash.display.GraphicsBitmapFill;
  267.     import flash.display.GraphicsEndFill;
  268.     import flash.display.GraphicsTrianglePath;
  269.     import flash.display.IGraphicsData;
  270.     import flash.display.Sprite;
  271.     import flash.events.Event;
  272.     import flash.events.MouseEvent;
  273.     import flash.geom.ColorTransform;
  274.     import flash.geom.Point;
  275.     /**
  276.      * ...
  277.      * @author YOSHIDA, Akio (Aquioux)
  278.      */
  279.     class View extends Sprite {
  280.         // 外部から入力するデータ
  281.         // 四隅の頂点の、全頂点中の順番
  282.         public function setBounds(idx:uint, value:Vector.<int>):void {
  283.             idx *= 4;
  284.             _bounds = value.slice(idx, idx + 4);
  285.         }
  286.         private var _bounds:Vector.<int>;
  287.         
  288.         // スキンとなる BitmapData
  289.         public function set bitmapData(value:BitmapData):void { _bitmapData = value; }
  290.         private var _bitmapData:BitmapData;
  291.         private var model:Model;
  292.         
  293.         // 内部だけで使用するデータ
  294.         // for drawTriangles
  295.         private var graphicsData:Vector.<IGraphicsData>;
  296.         private var path:GraphicsTrianglePath;
  297.         private var vertices:Vector.<Number>;
  298.         private var indices:Vector.<int>;
  299.         private var uvtData:Vector.<Number>;
  300.         
  301.         static private var topIndex:uint = 0;
  302.         
  303.         private var isVisited:Boolean = false;
  304.         private const visitedColor:ColorTransform = new ColorTransform(0001, Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), 0);
  305.         private const noVisitedColor:ColorTransform = new ColorTransform(11110000);
  306.         
  307.         public function View(model:Model) {
  308.             // 対 Model
  309.             this.model = model;
  310.             model.addEventListener(Event.CHANGE, update);
  311.             addEventListener(MouseEvent.ROLL_OVER, overHandler);
  312.             addEventListener(MouseEvent.ROLL_OUT, outHandler);
  313.             addEventListener(MouseEvent.CLICK, clickHandler);
  314.         }
  315.         
  316.         private function overHandler(event:MouseEvent):void {
  317.             if (topIndex == 0)
  318.                 topIndex = parent.numChildren - 1;
  319.             parent.setChildIndex(this, topIndex);
  320.             
  321.             if (isVisited)
  322.                 transform.colorTransform = noVisitedColor;
  323.         }
  324.         private function outHandler(event:MouseEvent):void {
  325.             if (isVisited)
  326.                 transform.colorTransform = visitedColor;
  327.         }
  328.         
  329.         
  330.         private function clickHandler(event:MouseEvent):void {
  331.             isVisited = true;
  332.             transform.colorTransform = visitedColor;
  333.         }
  334.         
  335.         
  336.         public function setup():void {
  337.             // drawTriangles で使う Vector の設定
  338.             vertices = new Vector.<Number>(8true);
  339.             indices = Vector.<int>([
  340.                 013,
  341.                 123
  342.             ]);
  343.             indices.fixed = true;
  344.             uvtData = Vector.<Number>([
  345.                 00,
  346.                 10,
  347.                 11,
  348.                 01
  349.             ]);
  350.             uvtData.fixed = true;
  351.             
  352.             // graphicsData の設定
  353.             graphicsData = new Vector.<IGraphicsData>();
  354.             graphicsData.push(new GraphicsBitmapFill(_bitmapData));
  355.             graphicsData.push(path = new GraphicsTrianglePath(vertices, indices, uvtData));
  356.             graphicsData.push(new GraphicsEndFill());
  357.         }
  358.         
  359.         private function update(event:Event):void {
  360.             var allVertices:Vector.<Number> = model.vertices;
  361.             for (var i:int = 0; i < 4; i++) {
  362.                 var idx:int = _bounds[i];
  363.                 vertices[i * 2]     = allVertices[idx * 2];
  364.                 vertices[i * 2 + 1] = allVertices[idx * 2 + 1];
  365.             }
  366.             path.vertices = vertices;
  367.             draw();
  368.         }
  369.         private function draw():void {
  370.             graphics.clear();
  371.             graphics.drawGraphicsData(graphicsData);
  372.         }
  373.     }
  374.     import flash.display.Sprite;
  375.     import flash.events.Event;
  376.     import flash.geom.Point;
  377.     /**
  378.      * Controller
  379.      * マウス座標を Model に渡すだけ
  380.      * マウス座標を取得するので Sprite を継承する
  381.      * @author YOSHIDA, Akio (Aquioux)
  382.      */
  383.     class Controller extends Sprite {
  384.         private var model:Model;
  385.         
  386.         public function Controller(model:Model) {
  387.             this.model = model;
  388.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  389.         }
  390.         
  391.         private function enterFrameHandler(event:Event):void {
  392.             var mousePoint:Point = new Point(mouseX, mouseY);
  393.             model.update(mousePoint);
  394.         }
  395.     }
noswf
  1. package {
  2.     import flash.display.Bitmap;
  3.     import flash.display.BitmapData;
  4.     import flash.display.Loader;
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.net.URLRequest;
  8.     import flash.system.LoaderContext;
  9.     [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#000000")]
  10.     /**
  11.      * Wonderwall のパチモン
  12.      * @author YOSHIDA, Akio (Aquioux)
  13.      */
  14.     public class Main extends Sprite {
  15.         private const DISTANCE_OF_REACTION:int = 125;    // マウスに反応する距離
  16.         private const SPRING:Number   = 0.01;            // バネ係数
  17.         private const FRICTION:Number = 0.9;            // 摩擦係数
  18.         private const NUM_OF_ROW:int = 3;    // 横方向の数
  19.         private const NUM_OF_COL:int = 3;    // 縦方向の数
  20.         private const IMAGE_WIDTH:int  = 100;
  21.         private const IMAGE_HEIGHT:int = 100;
  22.         
  23.         private var imageUrls:Vector.<String> = Vector.<String>([
  24.             "http://assets.wonderfl.net/images/related_images/5/59/59ce/59ce4dc0471ca9405ef2d0ed2dc1cea5e70c91c1",
  25.             "http://assets.wonderfl.net/images/related_images/7/71/716a/716a033cf4afbec77f72d124e1903b1ff05cbbda",
  26.             "http://assets.wonderfl.net/images/related_images/8/87/871a/871a53382ac812425d86de3888971e86fb3b956b",
  27.             "http://assets.wonderfl.net/images/related_images/7/74/74f2/74f2c57ef48e0d2b88be0bb3999ad15c110bcaf2",
  28.             "http://assets.wonderfl.net/images/related_images/c/c5/c5ef/c5efc60973f19249b625ca1b0672b2b359d63eee",
  29.             "http://assets.wonderfl.net/images/related_images/b/b4/b43b/b43bf737d1fe33d1c083c8e9e04f97467751df96",
  30.             "http://assets.wonderfl.net/images/related_images/3/3d/3dcd/3dcd1527c2f968c5b4e39471303b690d28e28687",
  31.             "http://assets.wonderfl.net/images/related_images/7/76/765f/765f0fef8e179cb6d384b85e972879be8e003f69",
  32.             "http://assets.wonderfl.net/images/related_images/e/ee/ee94/ee94f456247434eee366c2c6a6362b8a7db03f8e"
  33.         ]);
  34.         private var images:Vector.<BitmapData> = new Vector.<BitmapData>(9true);
  35.         private var cnt:uint = 0;
  36.         private const NUM_OF_IMAGE:uint = NUM_OF_ROW * NUM_OF_COL;
  37.         
  38.         public function Main() {
  39.             imageLoad();
  40.         }
  41.         
  42.         private function imageLoad():void {
  43.             var loader:Loader = new Loader();
  44.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  45.             var url:String = imageUrls[cnt];
  46.             loader.load(new URLRequest(url), new LoaderContext(true));
  47.         }
  48.         private function completeHandler(event:Event):void {
  49.             var loader:Loader = event.target.loader;
  50.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
  51.             images[cnt] = Bitmap(loader.content).bitmapData;
  52.             cnt++;
  53.             cnt >= NUM_OF_IMAGE ? next() : imageLoad();
  54.         }
  55.             
  56.         private function next():void {
  57.             // Coordinate 初期化
  58.             Coordinate.spring   = SPRING;
  59.             Coordinate.friction = FRICTION;
  60.             Coordinate.distanceOfReaction = DISTANCE_OF_REACTION;
  61.         
  62.             // データ生成
  63.             var dataFactory:DataFactory = new DataFactory();
  64.             dataFactory.numOfCol = NUM_OF_COL;
  65.             dataFactory.numOfRow = NUM_OF_ROW;
  66.             dataFactory.cellWidth  = IMAGE_WIDTH;
  67.             dataFactory.cellHeight = IMAGE_HEIGHT;
  68.             dataFactory.stageWidth  = stage.stageWidth;
  69.             dataFactory.stageHeight = stage.stageHeight;
  70.             dataFactory.start();
  71.             
  72.             // Model 生成
  73.             var model:Model = new Model();
  74.             model.coordinates = dataFactory.coordinates;
  75.             // View 生成
  76.             var viewLayer:Sprite = new Sprite();
  77.             addChild(viewLayer);
  78.             var n:uint = NUM_OF_ROW * NUM_OF_COL;
  79.             for (var i:int = 0; i < n; i++) {
  80.                 var view:View = new View(model);
  81.                 view.bitmapData = images[i];
  82.                 view.setBounds(i, dataFactory.bounds);
  83.                 view.setup();
  84.                 viewLayer.addChild(view);
  85.             }
  86.             // Controller 生成
  87.             var controller:Controller = new Controller(model);
  88.         }
  89.     }
  90. }
  91.     import flash.geom.Point;
  92.     /**
  93.      * データ生成クラス
  94.      * @author YOSHIDA, Akio (Aquioux)
  95.      */
  96.     class DataFactory {
  97.         // 外部へ出力するデータ
  98.         public function get coordinates():Vector.<Coordinate> { return _coordinates; }
  99.         private var _coordinates:Vector.<Coordinate>;    // すべての頂点(for Model)
  100.         
  101.         public function get bounds():Vector.<int> { return _bounds; }
  102.         private var _bounds:Vector.<int>;                // セルの四隅の頂点の組み合わせ(for View)
  103.         
  104.         // 外部から入力するデータ
  105.         public function set numOfRow(value:int):void { _numOfRow = value; }
  106.         private var _numOfRow:uint;        // 横方向のセル数
  107.         public function set numOfCol(value:int):void { _numOfCol = value; }
  108.         private var _numOfCol:uint;        // 縦方向のセル数
  109.         public function set cellWidth(value:Number):void { _cellWidth = value; }
  110.         private var _cellWidth:Number;    // セル幅
  111.         public function set cellHeight(value:Number):void { _cellHeight = value; }
  112.         private var _cellHeight:Number;    // セル高
  113.         
  114.         public function set stageWidth(value:Number):void { _stageWidth = value; }
  115.         private var _stageWidth:Number;        // ステージ幅
  116.         public function set stageHeight(value:Number):void { _stageHeight = value; }
  117.         private var _stageHeight:Number;    // ステージ高
  118.         
  119.         
  120.         // 内部だけで使用するデータ
  121.         private var offsetX:Number;    // X座標オフセット値
  122.         private var offsetY:Number;    // Y座標オフセット値
  123.         
  124.         public function DataFactory() {}
  125.         
  126.         // 出力データ作成開始
  127.         // このメソッドは入力データをすべて設定してから実行すること
  128.         public function start():void {
  129.             var totalWidth:int  = _numOfRow * _cellWidth;
  130.             var totalHeight:int = _numOfCol * _cellHeight;
  131.             var offsetX:Number = (_stageWidth - totalWidth) / 2;
  132.             var offsetY:Number = (_stageHeight - totalHeight) / 2;
  133.             
  134.             // 全頂点
  135.             // 左上端からZ順に各頂点を Coordinete クラスで設定し、その Coordinate インスタンスを Vector に格納
  136.             _coordinates = new Vector.<Coordinate>();
  137.             for (var i:int = 0; i < _numOfCol + 1; i++) {
  138.                 for (var j:int = 0; j < _numOfRow + 1; j++) {
  139.                     var posX:Number = j * _cellWidth  + offsetX + Math.random() * 30 - 15;
  140.                     var posY:Number = i * _cellHeight + offsetY + Math.random() * 30 - 15;
  141.                     var coordinate:Coordinate = new Coordinate(posX, posY);
  142.                     _coordinates.push(coordinate);
  143.                 }
  144.             }
  145.             _coordinates.fixed = true;
  146.             
  147.             // セルの四隅の頂点の組み合わせ
  148.             // 各セルの四隅(左上から時計回り)が全頂点のうちそれぞれ何番目になるのかを、Vector に格納(dtawTriangle の indices と似ている)
  149.             _bounds = new Vector.<int>();
  150.             var cnt:uint = 0;
  151.             for (i = 0; i < _numOfCol; i++) {
  152.                 for (j = 0; j < _numOfRow; j++) {
  153.                     var leftTop:uint     =  i      * (_numOfRow + 1) + j;
  154.                     var rightTop:uint    =  i      * (_numOfRow + 1) + j + 1;
  155.                     var leftBottom:uint  = (i + 1) * (_numOfRow + 1) + j;
  156.                     var rightBottom:uint = (i + 1) * (_numOfRow + 1) + j + 1;
  157.                     _bounds[cnt++] = leftTop;
  158.                     _bounds[cnt++] = rightTop;
  159.                     _bounds[cnt++] = rightBottom;
  160.                     _bounds[cnt++] = leftBottom;
  161.                 }
  162.             }
  163.             _bounds.fixed = true;
  164.         }
  165.     }
  166.     import flash.geom.Point;
  167.     /**
  168.      * 頂点生成クラス
  169.      * @author YOSHIDA, Akio (Aquioux)
  170.      */
  171.     class Coordinate {
  172.         // 外部から入力するデータ
  173.         // 静的変数
  174.         // バネ係数
  175.         static public function set spring(value:Number):void { _spring = value; }
  176.         static private var _spring:Number = 0.01;
  177.         // 抵抗
  178.         static public function set friction(value:Number):void { _friction = value; }
  179.         static private var _friction:Number = 0.9;
  180.         // 反応距離
  181.         static public function set distanceOfReaction(value:Number):void { _distanceOfReaction = value; }
  182.         static private var _distanceOfReaction:Number = 100;
  183.         // 外部へ出力するデータ
  184.         // 現在座標
  185.         public function get x():Number { return _x; }
  186.         private var _x:Number;
  187.         public function get y():Number { return _y; }
  188.         private var _y:Number;
  189.         
  190.         // 内部だけで使用するデータ
  191.         // 既定座標
  192.         private var localX:Number;
  193.         private var localY:Number;
  194.         // 速度
  195.         private var vx:Number = 0.0;
  196.         private var vy:Number = 0.0;
  197.     
  198.         public function Coordinate(valueX:Number, valueY:Number) {
  199.             _x = localX = valueX;
  200.             _y = localY = valueY;
  201.         }
  202.     
  203.         public function update(mousePoint:Point):void {
  204.             // マウスの位置と自分との距離を求める
  205.             var distance:Number = Point.distance(mousePoint, new Point(localX, localY));
  206.         
  207.             // 到達値
  208.             var dx:Number;
  209.             var dy:Number;
  210.             // 到達値の計算
  211.             if (distance < _distanceOfReaction) {
  212.                 var diff:Number     = -distance * (_distanceOfReaction - distance) / _distanceOfReaction;
  213.                 var radian:Number   = Math.atan2(mousePoint.y - localY, mousePoint.x - localX);
  214.                 var diffPoint:Point = Point.polar(diff*2, radian);
  215.                 dx = localX + diffPoint.x;
  216.                 dy = localY + diffPoint.y;
  217.             } else{    // 位置を元に戻す
  218.                 dx = localX;
  219.                 dy = localY;
  220.             }
  221.         
  222.             vx += (dx - _x) * _spring;
  223.             vy += (dy - _y) * _spring;
  224.             vx *= _friction;
  225.             vy *= _friction;
  226.             _x += vx;
  227.             _y += vy;
  228.         }
  229.     }
  230.     import flash.events.Event;
  231.     import flash.events.EventDispatcher;
  232.     import flash.geom.Point;
  233.     /**
  234.      * Model クラス
  235.      * @author YOSHIDA, Akio (Aquioux)
  236.      */
  237.     class Model extends EventDispatcher {
  238.         // 外部へ出力するデータ
  239.         // 全頂点のXY座標値
  240.         public function get vertices():Vector.<Number> { return _vertices; }
  241.         private var _vertices:Vector.<Number>;
  242.         // 外部から入力するデータ
  243.         // 全頂点データ
  244.         public function set coordinates(value:Vector.<Coordinate>):void {
  245.             _coordinates = value;
  246.             _vertices = new Vector.<Number>(_coordinates.length * 2true);
  247.         }
  248.         private var _coordinates:Vector.<Coordinate>;
  249.         
  250.         
  251.         public function Model() {}
  252.         public function update(mousePoint:Point):void {
  253.             var n:uint = _coordinates.length;
  254.             for (var i:int = 0; i < n; i++) {
  255.                 var c:Coordinate = _coordinates[i];
  256.                 c.update(mousePoint);
  257.                 _vertices[i * 2]     = c.x;
  258.                 _vertices[i * 2 + 1] = c.y;
  259.             }
  260.             dispatchEvent(new Event(Event.CHANGE));
  261.         }
  262.     }
  263.     import flash.display.Bitmap;
  264.     import flash.display.BitmapData;
  265.     import flash.display.GraphicsBitmapFill;
  266.     import flash.display.GraphicsEndFill;
  267.     import flash.display.GraphicsTrianglePath;
  268.     import flash.display.IGraphicsData;
  269.     import flash.display.Sprite;
  270.     import flash.events.Event;
  271.     import flash.events.MouseEvent;
  272.     import flash.geom.ColorTransform;
  273.     import flash.geom.Point;
  274.     /**
  275.      * ...
  276.      * @author YOSHIDA, Akio (Aquioux)
  277.      */
  278.     class View extends Sprite {
  279.         // 外部から入力するデータ
  280.         // 四隅の頂点の、全頂点中の順番
  281.         public function setBounds(idx:uint, value:Vector.<int>):void {
  282.             idx *= 4;
  283.             _bounds = value.slice(idx, idx + 4);
  284.         }
  285.         private var _bounds:Vector.<int>;
  286.         
  287.         // スキンとなる BitmapData
  288.         public function set bitmapData(value:BitmapData):void { _bitmapData = value; }
  289.         private var _bitmapData:BitmapData;
  290.         private var model:Model;
  291.         
  292.         // 内部だけで使用するデータ
  293.         // for drawTriangles
  294.         private var graphicsData:Vector.<IGraphicsData>;
  295.         private var path:GraphicsTrianglePath;
  296.         private var vertices:Vector.<Number>;
  297.         private var indices:Vector.<int>;
  298.         private var uvtData:Vector.<Number>;
  299.         
  300.         static private var topIndex:uint = 0;
  301.         
  302.         private var isVisited:Boolean = false;
  303.         private const visitedColor:ColorTransform = new ColorTransform(0001, Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), 0);
  304.         private const noVisitedColor:ColorTransform = new ColorTransform(11110000);
  305.         
  306.         public function View(model:Model) {
  307.             // 対 Model
  308.             this.model = model;
  309.             model.addEventListener(Event.CHANGE, update);
  310.             addEventListener(MouseEvent.ROLL_OVER, overHandler);
  311.             addEventListener(MouseEvent.ROLL_OUT, outHandler);
  312.             addEventListener(MouseEvent.CLICK, clickHandler);
  313.         }
  314.         
  315.         private function overHandler(event:MouseEvent):void {
  316.             if (topIndex == 0)
  317.                 topIndex = parent.numChildren - 1;
  318.             parent.setChildIndex(this, topIndex);
  319.             
  320.             if (isVisited)
  321.                 transform.colorTransform = noVisitedColor;
  322.         }
  323.         private function outHandler(event:MouseEvent):void {
  324.             if (isVisited)
  325.                 transform.colorTransform = visitedColor;
  326.         }
  327.         
  328.         
  329.         private function clickHandler(event:MouseEvent):void {
  330.             isVisited = true;
  331.             transform.colorTransform = visitedColor;
  332.         }
  333.         
  334.         
  335.         public function setup():void {
  336.             // drawTriangles で使う Vector の設定
  337.             vertices = new Vector.<Number>(8true);
  338.             indices = Vector.<int>([
  339.                 013,
  340.                 123
  341.             ]);
  342.             indices.fixed = true;
  343.             uvtData = Vector.<Number>([
  344.                 001.0,
  345.                 101.0,
  346.                 111.0,
  347.                 011.0
  348.             ]);
  349.             uvtData.fixed = true;
  350.             
  351.             // graphicsData の設定
  352.             graphicsData = new Vector.<IGraphicsData>();
  353.             graphicsData.push(new GraphicsBitmapFill(_bitmapData));
  354.             graphicsData.push(path = new GraphicsTrianglePath(vertices, indices, uvtData));
  355.             graphicsData.push(new GraphicsEndFill());
  356.         }
  357.         
  358.         private function update(event:Event):void {
  359.             var allVertices:Vector.<Number> = model.vertices;
  360.             for (var i:int = 0; i < 4; i++) {
  361.                 var idx:int = _bounds[i];
  362.                 vertices[i * 2]     = allVertices[idx * 2];
  363.                 vertices[i * 2 + 1] = allVertices[idx * 2 + 1];
  364.             }
  365.             path.vertices = vertices;
  366.             // set Ts in uvtData: code by Zeh Fernando
  367.             // http://zehfernando.com/2010/the-best-drawplane-distortimage-method-ever/
  368.             p1.x = vertices [0]; p1.y = vertices [1];
  369.             p2.x = vertices [2]; p2.y = vertices [3];
  370.             p3.x = vertices [6]; p3.y = vertices [7];
  371.             p4.x = vertices [4]; p4.y = vertices [5];
  372.             var pc:Point = getIntersection(p1, p4, p2, p3);
  373.             if (pc != null) {
  374.                 // Lenghts of first diagonal
  375.                 var ll1:Number = Point.distance(p1, pc);
  376.                 var ll2:Number = Point.distance(pc, p4);
  377.                 // Lengths of second diagonal
  378.                 var lr1:Number = Point.distance(p2, pc);
  379.                 var lr2:Number = Point.distance(pc, p3);
  380.                 // Ratio between diagonals
  381.                 var f:Number = (ll1 + ll2) / (lr1 + lr2);
  382.                 // Magic
  383.                 uvtData [2] = (1 / ll2) * f;
  384.                 uvtData [5] = (1 / lr2);
  385.                 uvtData [8] = (1 / ll1) * f;
  386.                 uvtData [11] = (1 / lr1);
  387.                 path.uvtData = uvtData;
  388.             }
  389.             draw();
  390.         }
  391.         private function draw():void {
  392.             graphics.clear();
  393.             graphics.drawGraphicsData(graphicsData);
  394.         }
  395.         private var p1:Point = new Point,
  396.             p2:Point = new Point, p3:Point = new Point, p4:Point = new Point;
  397.         private function getIntersection(p1:Point, p2:Point, p3:Point, p4:Point):Point {
  398.             // Returns a point containing the intersection between two lines
  399.             // http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/
  400.             // http://www.gamedev.pastebin.com/f49a054c1
  401.             var a1:Number = p2.y - p1.y;
  402.             var b1:Number = p1.x - p2.x;
  403.             var a2:Number = p4.y - p3.y;
  404.             var b2:Number = p3.x - p4.x;
  405.             var denom:Number = a1 * b2 - a2 * b1;
  406.             if (denom == 0return null;
  407.             var c1:Number = p2.x * p1.y - p1.x * p2.y;
  408.             var c2:Number = p4.x * p3.y - p3.x * p4.y;
  409.             var p:Point = new Point((b1 * c2 - b2 * c1)/denom, (a2 * c1 - a1 * c2)/denom);
  410.             if (Point.distance(p, p2) > Point.distance(p1, p2)) return null;
  411.             if (Point.distance(p, p1) > Point.distance(p1, p2)) return null;
  412.             if (Point.distance(p, p4) > Point.distance(p3, p4)) return null;
  413.             if (Point.distance(p, p3) > Point.distance(p3, p4)) return null;
  414.             return p;
  415.         }
  416.     }
  417.     import flash.display.Sprite;
  418.     import flash.events.Event;
  419.     import flash.geom.Point;
  420.     /**
  421.      * Controller
  422.      * マウス座標を Model に渡すだけ
  423.      * マウス座標を取得するので Sprite を継承する
  424.      * @author YOSHIDA, Akio (Aquioux)
  425.      */
  426.     class Controller extends Sprite {
  427.         private var model:Model;
  428.         
  429.         public function Controller(model:Model) {
  430.             this.model = model;
  431.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  432.         }
  433.         
  434.         private function enterFrameHandler(event:Event):void {
  435.             var mousePoint:Point = new Point(mouseX, mouseY);
  436.             model.update(mousePoint);
  437.         }
  438.     }
noswf
  1. // forked from Aquioux's Wonderwall のパチモン
  2. package {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.Loader;
  6.     import flash.display.Sprite;
  7.     import flash.events.Event;
  8.     import flash.net.URLRequest;
  9.     import flash.system.LoaderContext;
  10.     [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#000000")]
  11.     /**
  12.      * Wonderwall のパチモン
  13.      * @author YOSHIDA, Akio (Aquioux)
  14.      */
  15.     public class Main extends Sprite {
  16.         private const DISTANCE_OF_REACTION:int = 125;    // マウスに反応する距離
  17.         private const SPRING:Number   = 0.09;            // バネ係数
  18.         private const FRICTION:Number = 0.9;            // 摩擦係数
  19.         private const NUM_OF_ROW:int = 3;    // 横方向の数
  20.         private const NUM_OF_COL:int = 3;    // 縦方向の数
  21.         private const IMAGE_WIDTH:int  = 100;
  22.         private const IMAGE_HEIGHT:int = 100;
  23.         
  24.         private var imageUrls:Vector.<String> = Vector.<String>([
  25.             "http://assets.wonderfl.net/images/related_images/5/59/59ce/59ce4dc0471ca9405ef2d0ed2dc1cea5e70c91c1",
  26.             "http://assets.wonderfl.net/images/related_images/7/71/716a/716a033cf4afbec77f72d124e1903b1ff05cbbda",
  27.             "http://assets.wonderfl.net/images/related_images/8/87/871a/871a53382ac812425d86de3888971e86fb3b956b",
  28.             "http://assets.wonderfl.net/images/related_images/7/74/74f2/74f2c57ef48e0d2b88be0bb3999ad15c110bcaf2",
  29.             "http://assets.wonderfl.net/images/related_images/c/c5/c5ef/c5efc60973f19249b625ca1b0672b2b359d63eee",
  30.             "http://assets.wonderfl.net/images/related_images/b/b4/b43b/b43bf737d1fe33d1c083c8e9e04f97467751df96",
  31.             "http://assets.wonderfl.net/images/related_images/3/3d/3dcd/3dcd1527c2f968c5b4e39471303b690d28e28687",
  32.             "http://assets.wonderfl.net/images/related_images/7/76/765f/765f0fef8e179cb6d384b85e972879be8e003f69",
  33.             "http://assets.wonderfl.net/images/related_images/e/ee/ee94/ee94f456247434eee366c2c6a6362b8a7db03f8e"
  34.         ]);
  35.         private var images:Vector.<BitmapData> = new Vector.<BitmapData>(9true);
  36.         private var cnt:uint = 0;
  37.         private const NUM_OF_IMAGE:uint = NUM_OF_ROW * NUM_OF_COL;
  38.         
  39.         public function Main() {
  40.             imageLoad();
  41.         }
  42.         
  43.         private function imageLoad():void {
  44.             var loader:Loader = new Loader();
  45.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  46.             var url:String = imageUrls[cnt];
  47.             loader.load(new URLRequest(url), new LoaderContext(true));
  48.         }
  49.         private function completeHandler(event:Event):void {
  50.             var loader:Loader = event.target.loader;
  51.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
  52.             images[cnt] = Bitmap(loader.content).bitmapData;
  53.             cnt++;
  54.             cnt >= NUM_OF_IMAGE ? next() : imageLoad();
  55.         }
  56.             
  57.         private function next():void {
  58.             // Coordinate 初期化
  59.             Coordinate.spring   = SPRING;
  60.             Coordinate.friction = FRICTION;
  61.             Coordinate.distanceOfReaction = DISTANCE_OF_REACTION;
  62.         
  63.             // データ生成
  64.             var dataFactory:DataFactory = new DataFactory();
  65.             dataFactory.numOfCol = NUM_OF_COL;
  66.             dataFactory.numOfRow = NUM_OF_ROW;
  67.             dataFactory.cellWidth  = IMAGE_WIDTH;
  68.             dataFactory.cellHeight = IMAGE_HEIGHT;
  69.             dataFactory.stageWidth  = stage.stageWidth;
  70.             dataFactory.stageHeight = stage.stageHeight;
  71.             dataFactory.start();
  72.             
  73.             // Model 生成
  74.             var model:Model = new Model();
  75.             model.coordinates = dataFactory.coordinates;
  76.             // View 生成
  77.             var viewLayer:Sprite = new Sprite();
  78.             addChild(viewLayer);
  79.             var n:uint = NUM_OF_ROW * NUM_OF_COL;
  80.             for (var i:int = 0; i < n; i++) {
  81.                 var view:View = new View(model);
  82.                 view.bitmapData = images[i];
  83.                 view.setBounds(i, dataFactory.bounds);
  84.                 view.setup();
  85.                 viewLayer.addChild(view);
  86.             }
  87.             // Controller 生成
  88.             var controller:Controller = new Controller(model);
  89.         }
  90.     }
  91. }
  92.     import flash.geom.Point;
  93.     /**
  94.      * データ生成クラス
  95.      * @author YOSHIDA, Akio (Aquioux)
  96.      */
  97.     class DataFactory {
  98.         // 外部へ出力するデータ
  99.         public function get coordinates():Vector.<Coordinate> { return _coordinates; }
  100.         private var _coordinates:Vector.<Coordinate>;    // すべての頂点(for Model)
  101.         
  102.         public function get bounds():Vector.<int> { return _bounds; }
  103.         private var _bounds:Vector.<int>;                // セルの四隅の頂点の組み合わせ(for View)
  104.         
  105.         // 外部から入力するデータ
  106.         public function set numOfRow(value:int):void { _numOfRow = value; }
  107.         private var _numOfRow:uint;        // 横方向のセル数
  108.         public function set numOfCol(value:int):void { _numOfCol = value; }
  109.         private var _numOfCol:uint;        // 縦方向のセル数
  110.         public function set cellWidth(value:Number):void { _cellWidth = value; }
  111.         private var _cellWidth:Number;    // セル幅
  112.         public function set cellHeight(value:Number):void { _cellHeight = value; }
  113.         private var _cellHeight:Number;    // セル高
  114.         
  115.         public function set stageWidth(value:Number):void { _stageWidth = value; }
  116.         private var _stageWidth:Number;        // ステージ幅
  117.         public function set stageHeight(value:Number):void { _stageHeight = value; }
  118.         private var _stageHeight:Number;    // ステージ高
  119.         
  120.         
  121.         // 内部だけで使用するデータ
  122.         private var offsetX:Number;    // X座標オフセット値
  123.         private var offsetY:Number;    // Y座標オフセット値
  124.         
  125.         public function DataFactory() {}
  126.         
  127.         // 出力データ作成開始
  128.         // このメソッドは入力データをすべて設定してから実行すること
  129.         public function start():void {
  130.             var totalWidth:int  = _numOfRow * _cellWidth;
  131.             var totalHeight:int = _numOfCol * _cellHeight;
  132.             var offsetX:Number = (_stageWidth - totalWidth) / 2;
  133.             var offsetY:Number = (_stageHeight - totalHeight) / 2;
  134.             
  135.             // 全頂点
  136.             // 左上端からZ順に各頂点を Coordinete クラスで設定し、その Coordinate インスタンスを Vector に格納
  137.             _coordinates = new Vector.<Coordinate>();
  138.             for (var i:int = 0; i < _numOfCol + 1; i++) {
  139.                 for (var j:int = 0; j < _numOfRow + 1; j++) {
  140.                     var posX:Number = j * _cellWidth  + offsetX + Math.random() * 30 - 15;
  141.                     var posY:Number = i * _cellHeight + offsetY + Math.random() * 30 - 15;
  142.                     var coordinate:Coordinate = new Coordinate(posX, posY);
  143.                     _coordinates.push(coordinate);
  144.                 }
  145.             }
  146.             _coordinates.fixed = true;
  147.             
  148.             // セルの四隅の頂点の組み合わせ
  149.             // 各セルの四隅(左上から時計回り)が全頂点のうちそれぞれ何番目になるのかを、Vector に格納(dtawTriangle の indices と似ている)
  150.             _bounds = new Vector.<int>();
  151.             var cnt:uint = 0;
  152.             for (i = 0; i < _numOfCol; i++) {
  153.                 for (j = 0; j < _numOfRow; j++) {
  154.                     var leftTop:uint     =  i      * (_numOfRow + 1) + j;
  155.                     var rightTop:uint    =  i      * (_numOfRow + 1) + j + 1;
  156.                     var leftBottom:uint  = (i + 1) * (_numOfRow + 1) + j;
  157.                     var rightBottom:uint = (i + 1) * (_numOfRow + 1) + j + 1;
  158.                     _bounds[cnt++] = leftTop;
  159.                     _bounds[cnt++] = rightTop;
  160.                     _bounds[cnt++] = rightBottom;
  161.                     _bounds[cnt++] = leftBottom;
  162.                 }
  163.             }
  164.             _bounds.fixed = true;
  165.         }
  166.     }
  167.     import flash.geom.Point;
  168.     /**
  169.      * 頂点生成クラス
  170.      * @author YOSHIDA, Akio (Aquioux)
  171.      */
  172.     class Coordinate {
  173.         // 外部から入力するデータ
  174.         // 静的変数
  175.         // バネ係数
  176.         static public function set spring(value:Number):void { _spring = value; }
  177.         static private var _spring:Number = 0.01;
  178.         // 抵抗
  179.         static public function set friction(value:Number):void { _friction = value; }
  180.         static private var _friction:Number = 0.9;
  181.         // 反応距離
  182.         static public function set distanceOfReaction(value:Number):void { _distanceOfReaction = value; }
  183.         static private var _distanceOfReaction:Number = 100;
  184.         // 外部へ出力するデータ
  185.         // 現在座標
  186.         public function get x():Number { return _x; }
  187.         private var _x:Number;
  188.         public function get y():Number { return _y; }
  189.         private var _y:Number;
  190.         
  191.         // 内部だけで使用するデータ
  192.         // 既定座標
  193.         private var localX:Number;
  194.         private var localY:Number;
  195.         // 速度
  196.         private var vx:Number = 0.0;
  197.         private var vy:Number = 0.0;
  198.     
  199.         public function Coordinate(valueX:Number, valueY:Number) {
  200.             _x = localX = valueX;
  201.             _y = localY = valueY;
  202.         }
  203.     
  204.         public function update(mousePoint:Point):void {
  205.             // マウスの位置と自分との距離を求める
  206.             var distance:Number = Point.distance(mousePoint, new Point(localX, localY));
  207.         
  208.             // 到達値
  209.             var dx:Number;
  210.             var dy:Number;
  211.             // 到達値の計算
  212.             if (distance < _distanceOfReaction) {
  213.                 var diff:Number     = -distance * (_distanceOfReaction - distance) / _distanceOfReaction;
  214.                 var radian:Number   = Math.atan2(mousePoint.y - localY, mousePoint.x - localX);
  215.                 var diffPoint:Point = Point.polar(diff*2, radian);
  216.                 dx = localX + diffPoint.x;
  217.                 dy = localY + diffPoint.y;
  218.             } else{    // 位置を元に戻す
  219.                 dx = localX;
  220.                 dy = localY;
  221.             }
  222.         
  223.             vx += (dx - _x) * _spring;
  224.             vy += (dy - _y) * _spring;
  225.             vx *= _friction;
  226.             vy *= _friction;
  227.             _x += vx;
  228.             _y += vy;
  229.         }
  230.     }
  231.     import flash.events.Event;
  232.     import flash.events.EventDispatcher;
  233.     import flash.geom.Point;
  234.     /**
  235.      * Model クラス
  236.      * @author YOSHIDA, Akio (Aquioux)
  237.      */
  238.     class Model extends EventDispatcher {
  239.         // 外部へ出力するデータ
  240.         // 全頂点のXY座標値
  241.         public function get vertices():Vector.<Number> { return _vertices; }
  242.         private var _vertices:Vector.<Number>;
  243.         // 外部から入力するデータ
  244.         // 全頂点データ
  245.         public function set coordinates(value:Vector.<Coordinate>):void {
  246.             _coordinates = value;
  247.             _vertices = new Vector.<Number>(_coordinates.length * 2true);
  248.         }
  249.         private var _coordinates:Vector.<Coordinate>;
  250.         
  251.         
  252.         public function Model() {}
  253.         public function update(mousePoint:Point):void {
  254.             var n:uint = _coordinates.length;
  255.             for (var i:int = 0; i < n; i++) {
  256.                 var c:Coordinate = _coordinates[i];
  257.                 c.update(mousePoint);
  258.                 _vertices[i * 2]     = c.x;
  259.                 _vertices[i * 2 + 1] = c.y;
  260.             }
  261.             dispatchEvent(new Event(Event.CHANGE));
  262.         }
  263.     }
  264.     import flash.display.Bitmap;
  265.     import flash.display.BitmapData;
  266.     import flash.display.GraphicsBitmapFill;
  267.     import flash.display.GraphicsEndFill;
  268.     import flash.display.GraphicsTrianglePath;
  269.     import flash.display.IGraphicsData;
  270.     import flash.display.Sprite;
  271.     import flash.events.Event;
  272.     import flash.events.MouseEvent;
  273.     import flash.geom.ColorTransform;
  274.     import flash.geom.Point;
  275.     /**
  276.      * ...
  277.      * @author YOSHIDA, Akio (Aquioux)
  278.      */
  279.     class View extends Sprite {
  280.         // 外部から入力するデータ
  281.         // 四隅の頂点の、全頂点中の順番
  282.         public function setBounds(idx:uint, value:Vector.<int>):void {
  283.             idx *= 4;
  284.             _bounds = value.slice(idx, idx + 4);
  285.         }
  286.         private var _bounds:Vector.<int>;
  287.         
  288.         // スキンとなる BitmapData
  289.         public function set bitmapData(value:BitmapData):void { _bitmapData = value; }
  290.         private var _bitmapData:BitmapData;
  291.         private var model:Model;
  292.         
  293.         // 内部だけで使用するデータ
  294.         // for drawTriangles
  295.         private var graphicsData:Vector.<IGraphicsData>;
  296.         private var path:GraphicsTrianglePath;
  297.         private var vertices:Vector.<Number>;
  298.         private var indices:Vector.<int>;
  299.         private var uvtData:Vector.<Number>;
  300.         
  301.         static private var topIndex:uint = 0;
  302.         
  303.         private var isVisited:Boolean = false;
  304.         private const visitedColor:ColorTransform = new ColorTransform(0001, Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), 0);
  305.         private const noVisitedColor:ColorTransform = new ColorTransform(11110000);
  306.         
  307.         public function View(model:Model) {
  308.             // 対 Model
  309.             this.model = model;
  310.             model.addEventListener(Event.CHANGE, update);
  311.             addEventListener(MouseEvent.ROLL_OVER, overHandler);
  312.             addEventListener(MouseEvent.ROLL_OUT, outHandler);
  313.             addEventListener(MouseEvent.CLICK, clickHandler);
  314.         }
  315.         
  316.         private function overHandler(event:MouseEvent):void {
  317.             if (topIndex == 0)
  318.                 topIndex = parent.numChildren - 1;
  319.             parent.setChildIndex(this, topIndex);
  320.             
  321.             if (isVisited)
  322.                 transform.colorTransform = noVisitedColor;
  323.         }
  324.         private function outHandler(event:MouseEvent):void {
  325.             if (isVisited)
  326.                 transform.colorTransform = visitedColor;
  327.         }
  328.         
  329.         
  330.         private function clickHandler(event:MouseEvent):void {
  331.             isVisited = true;
  332.             transform.colorTransform = visitedColor;
  333.         }
  334.         
  335.         
  336.         public function setup():void {
  337.             // drawTriangles で使う Vector の設定
  338.             vertices = new Vector.<Number>(8true);
  339.             indices = Vector.<int>([
  340.                 013,
  341.                 123
  342.             ]);
  343.             indices.fixed = true;
  344.             uvtData = Vector.<Number>([
  345.                 00,
  346.                 10,
  347.                 11,
  348.                 01
  349.             ]);
  350.             uvtData.fixed = true;
  351.             
  352.             // graphicsData の設定
  353.             graphicsData = new Vector.<IGraphicsData>();
  354.             graphicsData.push(new GraphicsBitmapFill(_bitmapData));
  355.             graphicsData.push(path = new GraphicsTrianglePath(vertices, indices, uvtData));
  356.             graphicsData.push(new GraphicsEndFill());
  357.         }
  358.         
  359.         private function update(event:Event):void {
  360.             var allVertices:Vector.<Number> = model.vertices;
  361.             for (var i:int = 0; i < 4; i++) {
  362.                 var idx:int = _bounds[i];
  363.                 vertices[i * 2]     = allVertices[idx * 2];
  364.                 vertices[i * 2 + 1] = allVertices[idx * 2 + 1];
  365.             }
  366.             path.vertices = vertices;
  367.             draw();
  368.         }
  369.         private function draw():void {
  370.             graphics.clear();
  371.             graphics.drawGraphicsData(graphicsData);
  372.         }
  373.     }
  374.     import flash.display.Sprite;
  375.     import flash.events.Event;
  376.     import flash.geom.Point;
  377.     /**
  378.      * Controller
  379.      * マウス座標を Model に渡すだけ
  380.      * マウス座標を取得するので Sprite を継承する
  381.      * @author YOSHIDA, Akio (Aquioux)
  382.      */
  383.     class Controller extends Sprite {
  384.         private var model:Model;
  385.         
  386.         public function Controller(model:Model) {
  387.             this.model = model;
  388.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  389.         }
  390.         
  391.         private function enterFrameHandler(event:Event):void {
  392.             var mousePoint:Point = new Point(mouseX, mouseY);
  393.             model.update(mousePoint);
  394.         }
  395.     }
noswf
  1. // forked from Aquioux's Wonderwall のパチモン
  2. package {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.Loader;
  6.     import flash.display.Sprite;
  7.     import flash.events.Event;
  8.     import flash.net.URLRequest;
  9.     import flash.system.LoaderContext;
  10.     [SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#000000")]
  11.     /**
  12.      * Wonderwall のパチモン
  13.      * @author YOSHIDA, Akio (Aquioux)
  14.      */
  15.     public class Main extends Sprite {
  16.         private const DISTANCE_OF_REACTION:int = 125;    // マウスに反応する距離
  17.         private const SPRING:Number   = 0.01;            // バネ係数
  18.         private const FRICTION:Number = 0.9;            // 摩擦係数
  19.         private const NUM_OF_ROW:int = 3;    // 横方向の数
  20.         private const NUM_OF_COL:int = 3;    // 縦方向の数
  21.         private const IMAGE_WIDTH:int  = 100;
  22.         private const IMAGE_HEIGHT:int = 100;
  23.         
  24.         private var imageUrls:Vector.<String> = Vector.<String>([
  25.             "http://assets.wonderfl.net/images/related_images/5/59/59ce/59ce4dc0471ca9405ef2d0ed2dc1cea5e70c91c1",
  26.             "http://assets.wonderfl.net/images/related_images/7/71/716a/716a033cf4afbec77f72d124e1903b1ff05cbbda",
  27.             "http://assets.wonderfl.net/images/related_images/8/87/871a/871a53382ac812425d86de3888971e86fb3b956b",
  28.             "http://assets.wonderfl.net/images/related_images/7/74/74f2/74f2c57ef48e0d2b88be0bb3999ad15c110bcaf2",
  29.             "http://assets.wonderfl.net/images/related_images/c/c5/c5ef/c5efc60973f19249b625ca1b0672b2b359d63eee",
  30.             "http://assets.wonderfl.net/images/related_images/b/b4/b43b/b43bf737d1fe33d1c083c8e9e04f97467751df96",
  31.             "http://assets.wonderfl.net/images/related_images/3/3d/3dcd/3dcd1527c2f968c5b4e39471303b690d28e28687",
  32.             "http://assets.wonderfl.net/images/related_images/7/76/765f/765f0fef8e179cb6d384b85e972879be8e003f69",
  33.             "http://assets.wonderfl.net/images/related_images/e/ee/ee94/ee94f456247434eee366c2c6a6362b8a7db03f8e"
  34.         ]);
  35.         private var images:Vector.<BitmapData> = new Vector.<BitmapData>(9true);
  36.         private var cnt:uint = 0;
  37.         private const NUM_OF_IMAGE:uint = NUM_OF_ROW * NUM_OF_COL;
  38.         
  39.         public function Main() {
  40.             imageLoad();
  41.         }
  42.         
  43.         private function imageLoad():void {
  44.             var loader:Loader = new Loader();
  45.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
  46.             var url:String = imageUrls[cnt];
  47.             loader.load(new URLRequest(url), new LoaderContext(true));
  48.         }
  49.         private function completeHandler(event:Event):void {
  50.             var loader:Loader = event.target.loader;
  51.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, completeHandler);
  52.             images[cnt] = Bitmap(loader.content).bitmapData;
  53.             cnt++;
  54.             cnt >= NUM_OF_IMAGE ? next() : imageLoad();
  55.         }
  56.             
  57.         private function next():void {
  58.             // Coordinate 初期化
  59.             Coordinate.spring   = SPRING;
  60.             Coordinate.friction = FRICTION;
  61.             Coordinate.distanceOfReaction = DISTANCE_OF_REACTION;
  62.         
  63.             // データ生成
  64.             var dataFactory:DataFactory = new DataFactory();
  65.             dataFactory.numOfCol = NUM_OF_COL;
  66.             dataFactory.numOfRow = NUM_OF_ROW;
  67.             dataFactory.cellWidth  = IMAGE_WIDTH;
  68.             dataFactory.cellHeight = IMAGE_HEIGHT;
  69.             dataFactory.stageWidth  = stage.stageWidth;
  70.             dataFactory.stageHeight = stage.stageHeight;
  71.             dataFactory.start();
  72.             
  73.             // Model 生成
  74.             var model:Model = new Model();
  75.             model.coordinates = dataFactory.coordinates;
  76.             // View 生成
  77.             var viewLayer:Sprite = new Sprite();
  78.             addChild(viewLayer);
  79.             var n:uint = NUM_OF_ROW * NUM_OF_COL;
  80.             for (var i:int = 0; i < n; i++) {
  81.                 var view:View = new View(model);
  82.                 view.bitmapData = images[i];
  83.                 view.setBounds(i, dataFactory.bounds);
  84.                 view.setup();
  85.                 viewLayer.addChild(view);
  86.             }
  87.             // Controller 生成
  88.             var controller:Controller = new Controller(model);
  89.         }
  90.     }
  91. }
  92.     import flash.geom.Point;
  93.     /**
  94.      * データ生成クラス
  95.      * @author YOSHIDA, Akio (Aquioux)
  96.      */
  97.     class DataFactory {
  98.         // 外部へ出力するデータ
  99.         public function get coordinates():Vector.<Coordinate> { return _coordinates; }
  100.         private var _coordinates:Vector.<Coordinate>;    // すべての頂点(for Model)
  101.         
  102.         public function get bounds():Vector.<int> { return _bounds; }
  103.         private var _bounds:Vector.<int>;                // セルの四隅の頂点の組み合わせ(for View)
  104.         
  105.         // 外部から入力するデータ
  106.         public function set numOfRow(value:int):void { _numOfRow = value; }
  107.         private var _numOfRow:uint;        // 横方向のセル数
  108.         public function set numOfCol(value:int):void { _numOfCol = value; }
  109.         private var _numOfCol:uint;        // 縦方向のセル数
  110.         public function set cellWidth(value:Number):void { _cellWidth = value; }
  111.         private var _cellWidth:Number;    // セル幅
  112.         public function set cellHeight(value:Number):void { _cellHeight = value; }
  113.         private var _cellHeight:Number;    // セル高
  114.         
  115.         public function set stageWidth(value:Number):void { _stageWidth = value; }
  116.         private var _stageWidth:Number;        // ステージ幅
  117.         public function set stageHeight(value:Number):void { _stageHeight = value; }
  118.         private var _stageHeight:Number;    // ステージ高
  119.         
  120.         
  121.         // 内部だけで使用するデータ
  122.         private var offsetX:Number;    // X座標オフセット値
  123.         private var offsetY:Number;    // Y座標オフセット値
  124.         
  125.         public function DataFactory() {}
  126.         
  127.         // 出力データ作成開始
  128.         // このメソッドは入力データをすべて設定してから実行すること
  129.         public function start():void {
  130.             var totalWidth:int  = _numOfRow * _cellWidth;
  131.             var totalHeight:int = _numOfCol * _cellHeight;
  132.             var offsetX:Number = (_stageWidth - totalWidth) / 2;
  133.             var offsetY:Number = (_stageHeight - totalHeight) / 2;
  134.             
  135.             // 全頂点
  136.             // 左上端からZ順に各頂点を Coordinete クラスで設定し、その Coordinate インスタンスを Vector に格納
  137.             _coordinates = new Vector.<Coordinate>();
  138.             for (var i:int = 0; i < _numOfCol + 1; i++) {
  139.                 for (var j:int = 0; j < _numOfRow + 1; j++) {
  140.                     var posX:Number = j * _cellWidth  + offsetX + Math.random() * 30 - 15;
  141.                     var posY:Number = i * _cellHeight + offsetY + Math.random() * 30 - 15;
  142.                     var coordinate:Coordinate = new Coordinate(posX, posY);
  143.                     _coordinates.push(coordinate);
  144.                 }
  145.             }
  146.             _coordinates.fixed = true;
  147.             
  148.             // セルの四隅の頂点の組み合わせ
  149.             // 各セルの四隅(左上から時計回り)が全頂点のうちそれぞれ何番目になるのかを、Vector に格納(dtawTriangle の indices と似ている)
  150.             _bounds = new Vector.<int>();
  151.             var cnt:uint = 0;
  152.             for (i = 0; i < _numOfCol; i++) {
  153.                 for (j = 0; j < _numOfRow; j++) {
  154.                     var leftTop:uint     =  i      * (_numOfRow + 1) + j;
  155.                     var rightTop:uint    =  i      * (_numOfRow + 1) + j + 1;
  156.                     var leftBottom:uint  = (i + 1) * (_numOfRow + 1) + j;
  157.                     var rightBottom:uint = (i + 1) * (_numOfRow + 1) + j + 1;
  158.                     _bounds[cnt++] = leftTop;
  159.                     _bounds[cnt++] = rightTop;
  160.                     _bounds[cnt++] = rightBottom;
  161.                     _bounds[cnt++] = leftBottom;
  162.                 }
  163.             }
  164.             _bounds.fixed = true;
  165.         }
  166.     }
  167.     import flash.geom.Point;
  168.     /**
  169.      * 頂点生成クラス
  170.      * @author YOSHIDA, Akio (Aquioux)
  171.      */
  172.     class Coordinate {
  173.         // 外部から入力するデータ
  174.         // 静的変数
  175.         // バネ係数
  176.         static public function set spring(value:Number):void { _spring = value; }
  177.         static private var _spring:Number = 0.01;
  178.         // 抵抗
  179.         static public function set friction(value:Number):void { _friction = value; }
  180.         static private var _friction:Number = 0.9;
  181.         // 反応距離
  182.         static public function set distanceOfReaction(value:Number):void { _distanceOfReaction = value; }
  183.         static private var _distanceOfReaction:Number = 100;
  184.         // 外部へ出力するデータ
  185.         // 現在座標
  186.         public function get x():Number { return _x; }
  187.         private var _x:Number;
  188.         public function get y():Number { return _y; }
  189.         private var _y:Number;
  190.         
  191.         // 内部だけで使用するデータ
  192.         // 既定座標
  193.         private var localX:Number;
  194.         private var localY:Number;
  195.         // 速度
  196.         private var vx:Number = 0.0;
  197.         private var vy:Number = 0.0;
  198.     
  199.         public function Coordinate(valueX:Number, valueY:Number) {
  200.             _x = localX = valueX;
  201.             _y = localY = valueY;
  202.         }
  203.     
  204.         public function update(mousePoint:Point):void {
  205.             // マウスの位置と自分との距離を求める
  206.             var distance:Number = Point.distance(mousePoint, new Point(localX, localY));
  207.         
  208.             // 到達値
  209.             var dx:Number;
  210.             var dy:Number;
  211.             // 到達値の計算
  212.             if (distance < _distanceOfReaction) {
  213.                 var diff:Number     = -distance * (_distanceOfReaction - distance) / _distanceOfReaction;
  214.                 var radian:Number   = Math.atan2(mousePoint.y - localY, mousePoint.x - localX);
  215.                 var diffPoint:Point = Point.polar(diff*2, radian);
  216.                 dx = localX + diffPoint.x;
  217.                 dy = localY + diffPoint.y;
  218.             } else{    // 位置を元に戻す
  219.                 dx = localX;
  220.                 dy = localY;
  221.             }
  222.         
  223.             vx += (dx - _x) * _spring;
  224.             vy += (dy - _y) * _spring;
  225.             vx *= _friction;
  226.             vy *= _friction;
  227.             _x += vx;
  228.             _y += vy;
  229.         }
  230.     }
  231.     import flash.events.Event;
  232.     import flash.events.EventDispatcher;
  233.     import flash.geom.Point;
  234.     /**
  235.      * Model クラス
  236.      * @author YOSHIDA, Akio (Aquioux)
  237.      */
  238.     class Model extends EventDispatcher {
  239.         // 外部へ出力するデータ
  240.         // 全頂点のXY座標値
  241.         public function get vertices():Vector.<Number> { return _vertices; }
  242.         private var _vertices:Vector.<Number>;
  243.         // 外部から入力するデータ
  244.         // 全頂点データ
  245.         public function set coordinates(value:Vector.<Coordinate>):void {
  246.             _coordinates = value;
  247.             _vertices = new Vector.<Number>(_coordinates.length * 2true);
  248.         }
  249.         private var _coordinates:Vector.<Coordinate>;
  250.         
  251.         
  252.         public function Model() {}
  253.         public function update(mousePoint:Point):void {
  254.             var n:uint = _coordinates.length;
  255.             for (var i:int = 0; i < n; i++) {
  256.                 var c:Coordinate = _coordinates[i];
  257.                 c.update(mousePoint);
  258.                 _vertices[i * 2]     = c.x;
  259.                 _vertices[i * 2 + 1] = c.y;
  260.             }
  261.             dispatchEvent(new Event(Event.CHANGE));
  262.         }
  263.     }
  264.     import flash.display.Bitmap;
  265.     import flash.display.BitmapData;
  266.     import flash.display.GraphicsBitmapFill;
  267.     import flash.display.GraphicsEndFill;
  268.     import flash.display.GraphicsTrianglePath;
  269.     import flash.display.IGraphicsData;
  270.     import flash.display.Sprite;
  271.     import flash.events.Event;
  272.     import flash.events.MouseEvent;
  273.     import flash.geom.ColorTransform;
  274.     import flash.geom.Point;
  275.     /**
  276.      * ...
  277.      * @author YOSHIDA, Akio (Aquioux)
  278.      */
  279.     class View extends Sprite {
  280.         // 外部から入力するデータ
  281.         // 四隅の頂点の、全頂点中の順番
  282.         public function setBounds(idx:uint, value:Vector.<int>):void {
  283.             idx *= 4;
  284.             _bounds = value.slice(idx, idx + 4);
  285.         }
  286.         private var _bounds:Vector.<int>;
  287.         
  288.         // スキンとなる BitmapData
  289.         public function set bitmapData(value:BitmapData):void { _bitmapData = value; }
  290.         private var _bitmapData:BitmapData;
  291.         private var model:Model;
  292.         
  293.         // 内部だけで使用するデータ
  294.         // for drawTriangles
  295.         private var graphicsData:Vector.<IGraphicsData>;
  296.         private var path:GraphicsTrianglePath;
  297.         private var vertices:Vector.<Number>;
  298.         private var indices:Vector.<int>;
  299.         private var uvtData:Vector.<Number>;
  300.         
  301.         static private var topIndex:uint = 0;
  302.         
  303.         private var isVisited:Boolean = false;
  304.         private const visitedColor:ColorTransform = new ColorTransform(0001, Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), Math.floor(Math.random()*0xFF), 0);
  305.         private const noVisitedColor:ColorTransform = new ColorTransform(11110000);
  306.         
  307.         public function View(model:Model) {
  308.             // 対 Model
  309.             this.model = model;
  310.             model.addEventListener(Event.CHANGE, update);
  311.             addEventListener(MouseEvent.ROLL_OVER, overHandler);
  312.             addEventListener(MouseEvent.ROLL_OUT, outHandler);
  313.             addEventListener(MouseEvent.CLICK, clickHandler);
  314.         }
  315.         
  316.         private function overHandler(event:MouseEvent):void {
  317.             if (topIndex == 0)
  318.                 topIndex = parent.numChildren - 1;
  319.             parent.setChildIndex(this, topIndex);
  320.             
  321.             if (isVisited)
  322.                 transform.colorTransform = noVisitedColor;
  323.         }
  324.         private function outHandler(event:MouseEvent):void {
  325.             if (isVisited)
  326.                 transform.colorTransform = visitedColor;
  327.         }
  328.         
  329.         
  330.         private function clickHandler(event:MouseEvent):void {
  331.             isVisited = true;
  332.             transform.colorTransform = visitedColor;
  333.         }
  334.         
  335.         
  336.         public function setup():void {
  337.             // drawTriangles で使う Vector の設定
  338.             vertices = new Vector.<Number>(8true);
  339.             indices = Vector.<int>([
  340.                 013,
  341.                 123
  342.             ]);
  343.             indices.fixed = true;
  344.             uvtData = Vector.<Number>([
  345.                 00,
  346.                 10,
  347.                 11,
  348.                 01
  349.             ]);
  350.             uvtData.fixed = true;
  351.             
  352.             // graphicsData の設定
  353.             graphicsData = new Vector.<IGraphicsData>();
  354.             graphicsData.push(new GraphicsBitmapFill(_bitmapData));
  355.             graphicsData.push(path = new GraphicsTrianglePath(vertices, indices, uvtData));
  356.             graphicsData.push(new GraphicsEndFill());
  357.         }
  358.         
  359.         private function update(event:Event):void {
  360.             var allVertices:Vector.<Number> = model.vertices;
  361.             for (var i:int = 0; i < 4; i++) {
  362.                 var idx:int = _bounds[i];
  363.                 vertices[i * 2]     = allVertices[idx * 2];
  364.                 vertices[i * 2 + 1] = allVertices[idx * 2 + 1];
  365.             }
  366.             path.vertices = vertices;
  367.             draw();
  368.         }
  369.         private function draw():void {
  370.             graphics.clear();
  371.             graphics.drawGraphicsData(graphicsData);
  372.         }
  373.     }
  374.     import flash.display.Sprite;
  375.     import flash.events.Event;
  376.     import flash.geom.Point;
  377.     /**
  378.      * Controller
  379.      * マウス座標を Model に渡すだけ
  380.      * マウス座標を取得するので Sprite を継承する
  381.      * @author YOSHIDA, Akio (Aquioux)
  382.      */
  383.     class Controller extends Sprite {
  384.         private var model:Model;
  385.         
  386.         public function Controller(model:Model) {
  387.             this.model = model;
  388.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  389.         }
  390.         
  391.         private function enterFrameHandler(event:Event):void {
  392.             var mousePoint:Point = new Point(mouseX, mouseY);
  393.             model.update(mousePoint);
  394.         }
  395.     }
noswf
Get Adobe Flash Player