forked from: 慣性の動き
アンカーポイントの設定と、そこからの相対座標に目的地を変更
// forked from hiphi's 慣性の動き
pack
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
{
var obj:Object = new Object();
obj.x = 200;
obj.y = 200;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 200;
obj.y = 400;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 400;
obj.y = 400;
obj.z = 100;
obj.ac = 1/8;
this.ary.push(obj);
obj = new Object();
obj.x = 400;
obj.y = 200;
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.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;
}
}
}