package { import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; /** * ... * @author okoi */ [SWF(width = "465", height = "465", backgroundColor = "0x000000", frameRate = "30")] public class Main extends Sprite { private var workLine:Line = null; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); stage.addEventListener(MouseEvent.MOUSE_DOWN, DrawStart); } /** * お絵かきを開始 * @param e */ private function DrawStart(e:MouseEvent):void { var line:Line = new Line(); workLine = line; line.x = 0; line.y = 0; addChild(line); stage.addEventListener(MouseEvent.MOUSE_MOVE, DrawPath); stage.addEventListener(MouseEvent.MOUSE_UP, DrawEnd); } private function DrawPath(e:MouseEvent):void { if ( workLine != null ) { workLine.AddPath( stage.mouseX, stage.mouseY ); } } private function DrawEnd(e:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_UP, DrawEnd); stage.removeEventListener(MouseEvent.MOUSE_MOVE, DrawPath); workLine.RunStart(); workLine = null; } } } //////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////// import flash.display.MovieClip; import flash.events.Event; // ラインクラス class Line extends MovieClip { private static const MODE_DRAW:int = 0; private static const MODE_RUN:int = 1; private var mode:int = 0; private var linePath:Array = new Array(); private var movePower:int = 0; private var speedRate:int = 0; private var targetX:int = 0; private var targetY:int = 0; private var targetZ:int = 0; public function Line() { super(); mode = MODE_DRAW; movePower = 20; addEventListener( Event.ENTER_FRAME, EnterFrame ); addEventListener( Event.REMOVED_FROM_STAGE, RemoveFromStage ); } public function RemoveFromStage(e:Event):void { removeEventListener( Event.REMOVED_FROM_STAGE, RemoveFromStage ); removeEventListener( Event.ENTER_FRAME, EnterFrame ); } public function EnterFrame(e:Event) : void { if ( !stage ) return; var i:int; graphics.clear(); if ( mode == MODE_DRAW ) { graphics.lineStyle( 4, 0xFF0000, 1 ); for ( i = 0; i < linePath.length; i++ ) { if ( i == 0 ) { graphics.moveTo( linePath[i][0], linePath[i][1] ); }else { graphics.lineTo( linePath[i][0], linePath[i][1] ); } } }else { graphics.lineStyle( 4, 0xFF0000, 1 ); for ( i = 0; i < linePath.length; i++ ) { if ( i == 0 ) { linePath[i][1] += (targetY - linePath[i][1]) / speedRate; linePath[i][0] += (targetX - linePath[i][0]) / speedRate; if( Math.sqrt( (linePath[0][1] - targetY) * (linePath[0][1] - targetY) + (linePath[0][0] - targetX) * (linePath[0][0] - targetX) ) < 1 ) SetTargetPos(linePath[0][0],linePath[0][1]); graphics.moveTo( linePath[i][0], linePath[i][1] ); }else { { linePath[i][1] += (linePath[i-1][1] - linePath[i][1]) / speedRate; linePath[i][0] += (linePath[i-1][0] - linePath[i][0]) / speedRate; } graphics.lineTo( linePath[i][0], linePath[i][1] ); } } } } /** * ラインのパスを追加していく * @param _x * @param _y */ public function AddPath(_x:int, _y:int):void { linePath.push( new Array( _x, _y ) ); } /** * 描画モード終了、追尾モードへ * もしパスが不十分だと消してもらう */ public function RunStart() : void { mode = MODE_RUN; speedRate = int(Math.random() * 10) + 2; movePower = int(Math.random() * 30) + 20; if ( linePath.length <= 1 ) { parent.removeChild(this); }else { linePath.reverse(); // 反転させて最後についたパスを先頭にする SetTargetPos( linePath[0][0], linePath[0][1] ); } } /** * 次のターゲットポイントを決める * @param nowX 現在の位置 * @param nowY 現在の位置 */ public function SetTargetPos(nowX:int,nowY:int):void { var sX:int; var sY:int; if ( !stage ) { sX = sY = 0; }else { sX = stage.mouseX; sY = stage.mouseY; } // マウスまでのベクトルを正規化 var len:Number = Math.sqrt((sX - nowX) * (sX - nowX) + (sY - nowY) * (sY - nowY)); var nX:Number = (sX - nowX) / len; var nY:Number = (sY - nowY) / len; // サブ目標地点設定 // 移動力よりマウスまでの方が近かったらそっち優先で var tempX:Number; var tempY:Number; if ( len < movePower ) { tempX = sX; tempY = sY; }else { tempX = nowX + (nX * movePower); tempY = nowY + (nY * movePower); } // マウスから若干はずした地点にいくようにぶれさす var randR:Number = int(Math.random() * 360) * Math.PI/180; var randL:Number = int(Math.random() * (movePower)); targetX = tempX + Math.cos( randR ) * randL; targetY = tempY + Math.sin( randR ) * randL; } } 描いたラインが芋虫のように追いかけてきます