In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

慣性の動き

Get Adobe Flash player
by hiphi 20 Feb 2009
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 = 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;
		}
	}
}