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

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

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


forked from : miniapp's [diff(40)]

FAVORITE BY
:
graphicsphysical布がゆれる drawLine関数の中が違います
FORKED
  1. // forked from miniapp's GraphicsPathCommand使ったバージョン  forked from: 布
  2. /**
  3.  * GraphicsPathCommandでラインを描画するバージョン。
  4.  * drawLine関数の中が違います。
  5.  *
  6.  * 本格的な布のシミュレーションではありません。
  7.  * 
  8.  * ドラッグでマウスに一番近いポイントを移動させる。
  9.  * ctrlキー押しながらドラッグで固定。
  10.  * ダブルクリックで固定を解除。
  11.  */
  12. package {
  13.     import flash.display.GraphicsPathCommand;
  14.     import flash.display.Sprite;
  15.     import flash.display.StageQuality;
  16.     import flash.display.StageScaleMode;
  17.     import flash.events.Event;
  18.     import flash.events.KeyboardEvent;
  19.     import flash.events.MouseEvent;
  20.     [SWF(backgroundColor="0xFFFFFF", width="465", height="465", frameRate="60")]
  21.     public class Cloth_DrawPath extends Sprite {
  22.         
  23.         public static const STAGE_WIDTH:uint = 465;
  24.         public static const STAGE_HEIGHT:uint = 465;
  25.         
  26.         public function Cloth_DrawPath() {
  27.             if (stage) init();
  28.             else addEventListener(Event.ADDED_TO_STAGE, init);
  29.         }
  30.         
  31.         private var _lineColor:uint = 0x000000;
  32.         private var _cols:uint = 16;//横の数
  33.         private var _rows:uint = 16;//縦の数
  34.         private var _diffX:uint = 10;
  35.         private var _diffY:uint = 10;
  36.         private var _isCtrlPress:Boolean = false;
  37.         private var _isMouseDown:Boolean = false;
  38.         private var _draggedPoint:Point;
  39.         private var _isFirst:Boolean = false;
  40.         private var _numJoints:uint;
  41.         private var _joints:Vector.<Joint> = new Vector.<Joint>();
  42.         private var _points:Vector.<Point> = new Vector.<Point>();
  43.         private var _pointsXs:Vector.<Vector.<Point>> = new Vector.<Vector.<Point>>(_rows, true);
  44.         private var _pointsYs:Vector.<Vector.<Point>> = new Vector.<Vector.<Point>>(_cols, true);
  45.         
  46.         private var _vertices:Vector.<Number> = new Vector.<Number>();
  47.         private var _commands:Vector.<int> = new Vector.<int>();
  48.         
  49.         private function init(e:Event = null):void {
  50.             removeEventListener(Event.ADDED_TO_STAGE, init);
  51.             stage.scaleMode = StageScaleMode.NO_SCALE;
  52.             //stage.quality = StageQuality.MEDIUM;
  53.             
  54.             stage.doubleClickEnabled = true;
  55.             stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  56.             stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDwonHandler);
  57.             stage.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
  58.             stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDonwHandler);
  59.             stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
  60.             
  61.             putPoint();
  62.             joint();
  63.             
  64.             //上端2つを固定します。
  65.             //左端
  66.             var point1:Point = _pointsXs[0][0];
  67.             point1.x = 100;
  68.             point1.y = 10;
  69.             point1.isPinned = true;
  70.             
  71.             //右端
  72.             var point2:Point = _pointsXs[0][_rows - 1];
  73.             point2.x = STAGE_WIDTH - 100;
  74.             point2.y = 10;
  75.             point2.isPinned = true;
  76.             
  77.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  78.         }
  79.         
  80.         /**
  81.          * ポイントを配置します。
  82.          */
  83.         private function putPoint():void {
  84.             //縦方向に並べたポイントを入れる配列を先に用意する
  85.             for (var i:uint = 0; i < _cols; i++) { 
  86.                 var pointsY:Vector.<Point> = new Vector.<Point>(_rows, true);
  87.                 _pointsYs[i] = pointsY;
  88.             }
  89.             
  90.             var clothWidth:Number = (_cols - 1) * _diffX;
  91.             var clothHeight:Number = (_rows - 1) * _diffY;
  92.             var startX:Number = (STAGE_WIDTH - clothWidth) / 2;
  93.             var startY:Number = (STAGE_HEIGHT - clothHeight) / 2;
  94.             
  95.             //ポイントを格子状に配置
  96.             for (i = 0; i < _rows; i++) {
  97.                 var pointsX:Vector.<Point> = new Vector.<Point>(_cols, true);
  98.                 _pointsXs[i] = pointsX;
  99.                 for (var j:uint = 0; j < _cols; j++) {
  100.                     var point:Point = new Point();
  101.                     _points.push(point);
  102.                     point.name = String(i) + "-" + String(j);//デバッグ用
  103.                     point.x = startX + _diffX * j;
  104.                     point.y = startY + _diffY * i;
  105.                     
  106.                     //横に並ぶポイントを入れる
  107.                     pointsX[j] = point;
  108.                     
  109.                     //縦方向に並ぶポイントを入れる
  110.                     pointsY = _pointsYs[j];
  111.                     pointsY[i] = point;
  112.                 }
  113.             }
  114.             _points.fixed = true;
  115.         }
  116.         
  117.         private function joint():void {
  118.             
  119.             //00------01-------02
  120.             //|        |       |
  121.             //|        |       |
  122.             //|        |       |
  123.             //10------11-------12
  124.             //|        |       |
  125.             //|        |       |
  126.             //|        |       |
  127.             //20------21-------22
  128.             
  129.             for (var i:uint = 0; i < _rows; i++) {
  130.                 var up:Boolean = (i - 1) >= 0;//上の行があるか。
  131.                 var down:Boolean = (i + 1) < _rows;//下の行があるか。
  132.                 if (up)
  133.                     var pointsX0:Vector.<Point> = _pointsXs[i - 1];//一つ上の段
  134.                     
  135.                 var pointsX1:Vector.<Point> = _pointsXs[i];
  136.                 
  137.                 if (down)
  138.                     var pointsX2:Vector.<Point> = _pointsXs[i + 1];//一つ下の段
  139.                 
  140.                 for (var j:uint = 0; j < _cols; j++) {
  141.                     var left:Boolean = (j - 1) >= 0;//左の列があるか。
  142.                     var right:Boolean = (j + 1) < _cols;//右の列があるか。
  143.                     
  144.                     var point11:Point = pointsX1[j];//中央のポイント
  145.                     
  146.                     if (up) {
  147.                         var point01:Point = pointsX0[j];//上
  148.                         //trace(point01.name);
  149.                         _joints.push(new Joint(point11, point01));
  150.                     }
  151.                     
  152.                     if (left) { 
  153.                         var point10:Point = pointsX1[j -1];//左
  154.                         //trace(point10.name);
  155.                         _joints.push(new Joint(point11, point10));
  156.                         
  157.                         if (up) {
  158.                             var point00:Point = pointsX0[j - 1]; //左上
  159.                             //trace(point00.name);
  160.                             _joints.push(new Joint(point11, point00));
  161.                         }
  162.                         
  163.                         if (down) {
  164.                             var point20:Point = pointsX2[j -1];//左下
  165.                             //trace(point20.name);
  166.                             _joints.push(new Joint(point11, point20));
  167.                         }
  168.                     }
  169.                     
  170.                     if (right) { 
  171.                         var point12:Point = pointsX1[j + 1];//右
  172.                         //trace(point12.name);
  173.                         _joints.push(new Joint(point11, point12));
  174.                         
  175.                         if (up) {
  176.                             var point02:Point = pointsX0[j + 1];//右上
  177.                             //trace(point02.name);
  178.                             _joints.push(new Joint(point11, point02));
  179.                         }
  180.                         
  181.                         if (down) {
  182.                             var point22:Point = pointsX2[j + 1];//右下
  183.                             //trace(point22.name);
  184.                             _joints.push(new Joint(point11, point22));
  185.                         }
  186.                     }
  187.                     
  188.                     if (down) {
  189.                         var point21:Point = pointsX2[j];//下
  190.                         //trace(point21.name);
  191.                         _joints.push(new Joint(point11, point21));
  192.                     }
  193.                 }
  194.             }
  195.             _numJoints = _joints.length;
  196.             _joints.fixed = true;
  197.         }
  198.         
  199.         /**
  200.          * 一番カーソルに近いポイントを捜す。
  201.          */
  202.         private function searchPoint():Point {
  203.             var lastMinDist:Number = Infinity;
  204.             var target:Point;
  205.             for each(var point:Point in _points) {
  206.                 var dx:Number = point.x - mouseX;
  207.                 var dy:Number = point.y - mouseY;
  208.                 var dist:Number = Math.sqrt(dx * dx + dy * dy);
  209.                 if (dist < lastMinDist) {
  210.                     lastMinDist = dist;
  211.                     target = point;
  212.                 }
  213.             }
  214.             return target;
  215.         }
  216.         
  217.         /**
  218.          * ポイント同士を繋ぐ線を書く
  219.          */
  220.         private function drawLine():void {
  221.             graphics.clear();
  222.             graphics.lineStyle(1, _lineColor);
  223.             
  224.             var dataIndex:uint = 0;
  225.             var commandIndex:uint = 0;
  226.             for each(var pointsX:Vector.<Point> in _pointsXs){
  227.                 //横列のポイントが入った配列にアクセス。
  228.                 var firstBall:Point = pointsX[0];
  229.                 _vertices[dataIndex++] = firstBall.x;
  230.                 _vertices[dataIndex++] = firstBall.y;
  231.                 _commands[commandIndex++] = GraphicsPathCommand.MOVE_TO;
  232.                 
  233.                 for (var i:uint = 1; i < _cols; i++) {
  234.                     var ball:Point = pointsX[i];
  235.                     _vertices[dataIndex++] = ball.x;
  236.                     _vertices[dataIndex++] = ball.y;
  237.                     _commands[commandIndex++] = GraphicsPathCommand.LINE_TO;
  238.                 }
  239.             }
  240.             
  241.             for each(var ballsY:Vector.<Point> in _pointsYs) {
  242.                 firstBall = ballsY[0];
  243.                 _vertices[dataIndex++] = firstBall.x;
  244.                 _vertices[dataIndex++] = firstBall.y;
  245.                 _commands[commandIndex++] = GraphicsPathCommand.MOVE_TO;
  246.                 
  247.                 for (i = 1; i < _rows; i++) {
  248.                     ball = ballsY[i];
  249.                     _vertices[dataIndex++] = ball.x;
  250.                     _vertices[dataIndex++] = ball.y;
  251.                     _commands[commandIndex++] = GraphicsPathCommand.LINE_TO;
  252.                 }
  253.             }
  254.             if(_isFirst){
  255.                 _commands.fixed = _vertices.fixed = true;
  256.                 _isFirst = false;
  257.             }
  258.             graphics.drawPath(_commands, _vertices);
  259.         }
  260.         
  261.         private function enterFrameHandler(e:Event):void {
  262.             if (_isMouseDown) {
  263.                 _draggedPoint.x = mouseX;
  264.                 _draggedPoint.y = mouseY;
  265.             }
  266.             
  267.             for each(var joint:Joint in _joints) {
  268.                 joint.update();
  269.             }
  270.             
  271.             drawLine();
  272.         }
  273.         
  274.         private function keyDonwHandler(e:KeyboardEvent = null):void{
  275.             if(e.ctrlKey) _isCtrlPress = true;
  276.         }
  277.         
  278.         private function keyUpHandler(e:KeyboardEvent = null):void{
  279.             _isCtrlPress = false;
  280.         }
  281.         
  282.         private function doubleClickHandler(e:MouseEvent = null):void{
  283.             searchPoint().isPinned = false;
  284.         }
  285.         
  286.         private function mouseDwonHandler(e:MouseEvent):void {
  287.             _isMouseDown = true;
  288.             _draggedPoint = searchPoint();
  289.             _draggedPoint.isDragging = true;
  290.         }
  291.         
  292.         private function mouseUpHandler(e:MouseEvent):void    {
  293.             _isMouseDown = false;
  294.             if (_isCtrlPress)
  295.                 _draggedPoint.isPinned = true;
  296.             _draggedPoint.isDragging = false;
  297.             _draggedPoint = undefined;
  298.         }
  299.     }
  300. }
  301. class Joint {
  302.     
  303.     public static var SPRING:Number = 0.03;
  304.     public static var FRICTION:Number = 0.97;
  305.     public static var GRAVITY:Number = 0.005;
  306.     
  307.     public function Joint(point:Point, target:Point) { 
  308.         var initDx:Number = target.x - point.x;
  309.         var initDy:Number = target.y - point.y;
  310.         var length:Number = Math.sqrt(initDx * initDx + initDy * initDy);
  311.         var angle:Number = Math.atan2(initDy, initDx);
  312.         
  313.         var tx:Number = length * Math.cos(angle);
  314.         var ty:Number = length * Math.sin(angle);
  315.         
  316.         //バネ運動させる関数
  317.         update = function():void {
  318.             if (point.isDragging || point.isPinned)
  319.                 return;
  320.             
  321.             var dx:Number = target.x - tx - point.x;
  322.             var dy:Number = target.y - ty - point.y;
  323.             point.vx += dx * Joint.SPRING;
  324.             point.vy += dy * Joint.SPRING;
  325.             point.vy += GRAVITY;
  326.             point.x += (point.vx *= FRICTION);
  327.             point.y += (point.vy *= FRICTION);
  328.         }
  329.     }
  330.     
  331.     public var update:Function;
  332. }
  333. class Point {
  334.     public var name:String;
  335.     public var x:Number;
  336.     public var y:Number;
  337.     public var vx:Number = 0;
  338.     public var vy:Number = 0;
  339.     public var isPinned:Boolean = false;
  340.     public var isDragging:Boolean = false;
  341. }
noswf
Get Adobe Flash Player