forked from: 慣性の動き
// forked from hiphi's 慣性の動き
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.text.TextField;
import flash.display.BitmapData;
import flash.geom.Point;
public class main extends MovieClip
{
public var shapes:Array = new Array();
protected var ary:Array = new Array();
protected var dAry:Array = new Array();
protected var trackNum:Object;
public function main()
{
this.init();
}
protected function init():void
{
this.stage.align = StageAlign.TOP_LEFT;
this.stage.scaleMode = StageScaleMode.NO_SCALE;
//this.timer = new Timer(50);
//this.timer.addEventListener(TimerEvent.TIMER,this.addPoint);
var t:TextField = new TextField();
t.text = "drag and drop Rect!";
t.x = 10;t.y = 10;
this.addChild(t);
var obj:Object = new Object();
obj.x = 400;
obj.y = 200;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
var obj:Object = new Object();
obj.x = 350;
obj.y = 350;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 200;
obj.y = 350;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 320;
obj.y = 440;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 270;
obj.y = 590;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 400;
obj.y = 500;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 530;
obj.y = 590;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 480;
obj.y = 440;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 600;
obj.y = 350;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 450;
obj.y = 350;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
for(var prop:* in this.ary)
{
var obj_copy:Object = new Object();
for(var prop2:* in this.ary[prop])
{
obj_copy[prop2] = this.ary[prop][prop2];
}
this.dAry.push(obj_copy);
}
this.trackNum = {x:0,y:0,z:100};
this.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDownEventHandler);
this.addEventListener(Event.ENTER_FRAME,this.render);
}
protected function render(e:Event):void
{
var len:uint = this.ary.length;
var i:uint=0;
for(i;i<len;i++)
{
var k:Number = (this.dAry[i].ac/50);
if(k<1)k = 1;
this.ary[i].x += ((this.dAry[i].x+this.trackNum.x) - this.ary[i].x)/k;
this.ary[i].y += ((this.dAry[i].y+this.trackNum.y) - this.ary[i].y)/k;
if(Math.abs((this.dAry[i].x+this.trackNum.x) - this.ary[i].x) < 1)
{
this.ary[i].x = (this.dAry[i].x+this.trackNum.x);
}
if(Math.abs((this.dAry[i].y+this.trackNum.y) - this.ary[i].y) < 1)
{
this.ary[i].y = (this.dAry[i].y+this.trackNum.y);
}
}
this.graphics.clear()
this.graphics.beginFill(0xcccccc);
this.graphics.moveTo(this.ary[0].x,this.ary[0].y);
this.graphics.lineTo(this.ary[1].x,this.ary[1].y);
this.graphics.lineTo(this.ary[2].x,this.ary[2].y);
this.graphics.lineTo(this.ary[3].x,this.ary[3].y);
this.graphics.lineTo(this.ary[4].x,this.ary[4].y);
this.graphics.lineTo(this.ary[5].x,this.ary[5].y);
this.graphics.lineTo(this.ary[6].x,this.ary[6].y);
this.graphics.lineTo(this.ary[7].x,this.ary[7].y);
this.graphics.lineTo(this.ary[8].x,this.ary[8].y);
this.graphics.lineTo(this.ary[9].x,this.ary[9].y);
this.graphics.endFill();
}
protected function mouseDownEventHandler(e:MouseEvent):void
{
this.trackNum = {x:this.stage.mouseX,y:this.stage.mouseY,z:100};
var len:uint = this.ary.length;
var i:uint=0;
var px:Number,py:Number;
//アンカーポイントの設定と、そこからの相対座標に目的地を変更
for(i;i<len;i++)
{
this.dAry[i].x = this.ary[i].x - this.trackNum.x;
this.dAry[i].y = this.ary[i].y - this.trackNum.y;
this.dAry[i].ac = (Math.sqrt((this.dAry[i].x*this.dAry[i].x)+(this.dAry[i].y*this.dAry[i].y)));
}
this.stage.removeEventListener(MouseEvent.MOUSE_DOWN,this.mouseDownEventHandler);
this.stage.addEventListener(MouseEvent.MOUSE_UP,this.mouseUpEventHandler);
this.addEventListener(Event.ENTER_FRAME,this.tracking);
}
protected function mouseUpEventHandler(e:MouseEvent):void
{
this.removeEventListener(Event.ENTER_FRAME,this.tracking);
this.stage.removeEventListener(MouseEvent.MOUSE_UP,this.mouseUpEventHandler);
this.stage.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDownEventHandler);
}
protected function tracking(e:Event):void
{
this.trackNum.x = this.stage.mouseX;
this.trackNum.y = this.stage.mouseY;
}
}
}