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

interactive circle

インタラクションのサンプル
* つかんで動かす
* @author Default
* @version 0.1
Get Adobe Flash player
by uranodai 17 Dec 2008
/**
* インタラクションのサンプル
* つかんで動かす
* @author Default
* @version 0.1
*/

package  {
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	[SWF(width="400", height="400", frameRate="30", backgroundColor="#FFFFFF")]
	
	public class Study1 extends Sprite{
		
		private var FRICTION:Number = 0.98; //スピードの減衰率
		private var GRAVITY:Number = 1; //重力
		private var RADIUS:Number = 40; //半径
		private var posx:Number; //位置
		private var posy:Number;
		private var vx:Number; //速度
		private var vy:Number;
		private var pmouseX:Number = 0; //前フレームのマウスポインタの座標
		private var pmouseY:Number = 0;
		private var onMouse:Boolean = false; //ボールとマウスポインタの接触フラグ
		private var onDrag:Boolean = false; //ボールのドラッグ中フラグ
		
		public function Study1() 
		{
			//位置とスピードの初期化
			posx = stage.stageWidth/2;
			posy = stage.stageHeight/2;
			vx = 0;
			vy = 0;
				
			//イベントリスナの追加
			addEventListener(Event.ENTER_FRAME, draw);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void
			{
				if(onMouse){
					pmouseX = mouseX;
					pmouseY = mouseY;
					onDrag = true;
				}
			});
			stage.addEventListener(MouseEvent.MOUSE_UP, function(e:MouseEvent):void
			{
				onDrag = false;
			});
		}
		
		private function draw(e:Event):void
		{
			graphics.clear();
			
			//位置の計算
			if(onDrag){
				posx = mouseX;
				posy = mouseY;
				
				vx = mouseX - pmouseX;
				vy = mouseY - pmouseY;
				
				pmouseX = mouseX;
				pmouseY = mouseY;
			}else{
				vx = vx*FRICTION;
				vy = vy*FRICTION+GRAVITY;
				
				posx = posx+vx;
				posy = posy+vy;
				
				bounce(); //跳ね返り判定
			}
			
			checkOnMouse(); //ボールとマウスが触れているか判定
			
			//描画
			graphics.lineStyle(0,0xFF0066);
			graphics.drawCircle(posx, posy, RADIUS);
			graphics.endFill();
			
		}
		
		//ボールとマウスが触れているか判定
		private function checkOnMouse():void
		{
			if(Math.sqrt(Math.pow(mouseX-posx,2)+Math.pow(mouseY-posy,2))<RADIUS){
				onMouse = true;
				graphics.beginFill(0xFF0066,0.3);
			}else{
				onMouse = false;
				graphics.beginFill(0xFFFFFF,0.3);
			}
		}
		
		//跳ね返り判定
		private function bounce():void{
			var bounceMinX:Number = RADIUS;
			var bounceMaxX:Number = stage.stageWidth-RADIUS;
			var bounceMinY:Number = RADIUS;
			var bounceMaxY:Number = stage.stageHeight-RADIUS;
			
			if(posx < bounceMinX || posx>bounceMaxX){
				vx = -vx*FRICTION;
				
				if(Math.abs(vx)<1)vx = 0;
				
				if(posx<bounceMinX){
					posx = bounceMinX -(posx-bounceMinX);
				}
				if(posx > bounceMaxX){
					posx = bounceMaxX -(posx-bounceMaxX);
				}
			}
			
			if(posy < bounceMinY || posy>bounceMaxY){
				vy = -vy*FRICTION;
				
				if(Math.abs(vy)<1)vy = 0;
				
				if(posy<bounceMinY){
					posy = bounceMinY -(posy-bounceMinY);
				}
				if(posy > bounceMaxY){
					if(bounceMaxY-posy<1){
						posy = bounceMaxY;
					}else{
						posy = bounceMaxY-(posy-bounceMaxY);
					}
				}
			}
			
		}
	}
}