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

線分AB上のアリア

-----------------------------
任意線分上に束縛されたオブジェクトのドラッグ
先日自分に出した宿題
(cf: http://wonderfl.net/code/d785cbaa767059a9e3bb18ee4d0ac1f3625fa63a)
の答え.  今度は両端点と中間点のいずれもドラッグ可能
-----------------------------
Get Adobe Flash player
by tenasaku 27 Mar 2010
/**
 * Copyright tenasaku ( http://wonderfl.net/user/tenasaku )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ca6d
 */

/*	-----------------------------
	任意線分上に束縛されたオブジェクトのドラッグ
	先日自分に出した宿題
	(cf: http://wonderfl.net/code/d785cbaa767059a9e3bb18ee4d0ac1f3625fa63a)
	の答え.  今度は両端点と中間点のいずれもドラッグ可能
	-----------------------------
*/

package {

	import flash.display.*;
	import flash.events.*;

	public class Main extends Sprite {

		private var mumu:KumaIcon; // 茶色クマの むむ
		private var shirayuki:ShiroKumaIcon; // シロクマの しらゆき
		private var kimochi:HeartIcon; // ハート形
		private var t:Number; // 線分の分割比

		private function draw():void {
			this.graphics.clear();
			this.graphics.beginFill(0xffdddd);
			this.graphics.drawRect(0,0,465,465);
			this.graphics.endFill();
			this.graphics.lineStyle(3,0xff0000);
			this.graphics.moveTo(mumu.x,mumu.y);
			this.graphics.lineTo(shirayuki.x,shirayuki.y);
			kimochi.x = (1-t)*mumu.x + t*shirayuki.x;
			kimochi.y = (1-t)*mumu.y + t*shirayuki.y;
		}

		private function onMouseDown(e:MouseEvent):void {
			var spr:Sprite;
			var dx:Number,dy:Number;
			// どのオブジェクトが押されたか判定する
			for each ( spr in [ kimochi, mumu, shirayuki ] ) {
				dx = e.stageX - spr.x;
				dy = e.stageY - spr.y;
				if ( Math.sqrt(dx*dx+dy*dy) < 20.0 ) { // ヒット!!
					if ( spr == kimochi ) {
						// ハートが押された場合の動作を指定
						if (( mumu.x == shirayuki.x )&&( mumu.y == shirayuki.y )) {
							// 両端点が一致した場合, (ゼロでの割り算を避けるため)
							// 一方の端点のドラッグを開始
							mumu.startDrag();
							stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
						} else {
							stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove_sono2);
						}
					} else {
						// むむ か しらゆきが押されたのであれば普通のドラッグ処理
						spr.startDrag();
						stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);		
					}
					stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
					break;
				}
			}
		}

		// 中間点のドラッグは線分上に束縛される
		private function onMouseMove_sono2(e:MouseEvent):void {
			var ax:Number = shirayuki.x - mumu.x;
			var ay:Number = shirayuki.y - mumu.y;
			var bx:Number = e.stageX - mumu.x;
			var by:Number = e.stageY - mumu.y;
			var rsquare:Number = ax*ax+ay*ay;
			var innerproduct:Number = ax*bx+ay*by;
			if (rsquare > 0) {
				this.t = innerproduct/rsquare;
				if ( t < 0 ) { t = 0; }
				if ( t > 1 ) { t = 1; }
				e.updateAfterEvent();
				this.draw();
			}
		}

		// 両端点のドラッグには束縛がない
		private function onMouseMove(e:MouseEvent):void {
			e.updateAfterEvent();
			this.draw();
		}

		private function onMouseUp(e:MouseEvent):void {
			mumu.stopDrag();
			shirayuki.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove_sono2);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			this.draw();
		}

		private function initialize(e:Event):void {
			this.removeEventListener(Event.ADDED_TO_STAGE, initialize);
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			mumu = new KumaIcon();
			shirayuki = new ShiroKumaIcon();
			kimochi = new HeartIcon();
			mumu.x = 100;
			mumu.y = 100;
			mumu.scaleX = mumu.scaleY = 1.0;
			shirayuki.x = 300;
			shirayuki.y = 300;
			shirayuki.scaleX = shirayuki.scaleY = 1.0;
			t = 0.5;
			draw();
			this.addChild(mumu);
			this.addChild(shirayuki);
			this.addChild(kimochi);
			mumu.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			shirayuki.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			kimochi.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		// The Main constructor simply calles initialize() function.

		public function Main():void {
			if ( stage != null ) {
				initialize(null);
			} else {
				this.addEventListener(Event.ADDED_TO_STAGE, initialize);
			}
		}

	} // end of class Main
} // end of package

// クマ顔アイコン 2010年3月26日バージョン
import flash.display.*;
class KumaIcon extends Sprite {
	private static const _borderColor:uint = 0x000000;
	private static const _fillColor:uint = 0x800000;
	public function KumaIcon():void {
		this.graphics.clear();
		this.graphics.lineStyle(NaN);
		this.graphics.beginFill(_fillColor);
		this.graphics.drawCircle(0,0,20);
		this.graphics.endFill();
		this.graphics.beginFill(_fillColor);
		this.graphics.drawCircle(14,-14,8);
		this.graphics.endFill();
		this.graphics.beginFill(_fillColor);
		this.graphics.drawCircle(-14,-14,8);
		this.graphics.endFill();
		this.graphics.beginFill(0xffffff);
		this.graphics.drawEllipse(-8,0,16,15);
		this.graphics.endFill();
		this.graphics.beginFill(_borderColor);
		this.graphics.drawEllipse(-2,2,4,3);
		this.graphics.endFill();
		this.graphics.beginFill(_borderColor);
		this.graphics.drawEllipse(-12,-5,3,4);
		this.graphics.endFill();
		this.graphics.beginFill(_borderColor);
		this.graphics.drawEllipse( 12,-5,-3,4);
		this.graphics.endFill();
		this.graphics.lineStyle(1,_borderColor);
		this.graphics.moveTo(-4, 12);
		this.graphics.curveTo( 0, 10, 0, 6);
		this.graphics.curveTo( 0, 10, 4, 12);
	}
}

// シロクマ顔アイコン
class ShiroKumaIcon extends Sprite {
	private static const _borderColor:uint = 0x000000;
	private static const _ovalColor:uint = 0x999999;
	private static const _fillColor:uint = 0xffffff;
	public function ShiroKumaIcon():void {
		this.graphics.clear();
		this.graphics.lineStyle(NaN);
		this.graphics.beginFill(_fillColor);
		this.graphics.drawCircle(14,-14,8);
		this.graphics.endFill();
		this.graphics.beginFill(_fillColor);
		this.graphics.drawCircle(-14,-14,8);
		this.graphics.endFill();
		this.graphics.beginFill(_fillColor);
		this.graphics.drawCircle(0,0,20);
		this.graphics.endFill();
		this.graphics.lineStyle(0,_ovalColor);
		this.graphics.beginFill(0xffffff);
		this.graphics.drawEllipse(-8,0,16,15);
		this.graphics.endFill();
		this.graphics.lineStyle(NaN);
		this.graphics.beginFill(_borderColor);
		this.graphics.drawEllipse(-2,2,4,3);
		this.graphics.endFill();
		this.graphics.beginFill(_borderColor);
		this.graphics.drawEllipse(-12,-5,3,4);
		this.graphics.endFill();
		this.graphics.beginFill(_borderColor);
		this.graphics.drawEllipse( 12,-5,-3,4);
		this.graphics.endFill();
		this.graphics.lineStyle(1,_borderColor);
		this.graphics.moveTo(-4, 12);
		this.graphics.curveTo( 0, 10, 0, 6);
		this.graphics.curveTo( 0, 10, 4, 12);
	}
}

// ハート形アイコン
class HeartIcon extends Sprite {
	private static const _borderColor:uint = 0xffccccc;
	private static const _pongColor:uint = 0x990000;
	private static const _fillColor:uint = 0xff0000;
	private static const cX:Number = 30;
	private static const cY:Number = 20;
	public function HeartIcon():void {
		this.graphics.clear();
		this.graphics.lineStyle(1,_borderColor);
		this.graphics.beginFill(_fillColor);
		this.graphics.moveTo(0,cY);
		this.graphics.curveTo(cX,0,cX/3,-cY/3*2);
		this.graphics.curveTo(cX/6,-cY/6*5,0,-cY/3);
		this.graphics.curveTo(-cX/6,-cY/6*5,-cX/3,-cY/3*2);
		this.graphics.curveTo(-cX,0,0,cY);
		this.graphics.endFill();
	}
}