円と線と動く円
灰色の円はマウスで動かせます。
灰色の円と円の間は線が表示されます。
動く円はとりあえず線の上を移動します。
/**
* Copyright yuugurenote ( http://wonderfl.net/user/yuugurenote )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jhjC
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(width=465,height=465,backgroundColor=0xFFFFFF,frameRate=60)]
public class FlashTest extends Sprite {
var sw:Number=stage.stageWidth;
var sh:Number=stage.stageHeight;
var myArray:Array = new Array();
var max:Number=10;
var myLine:Sprite =new Sprite();
public function FlashTest() {
for (var n:Number=0; n<max; n++) {
var myCircle:Sprite = new Sprite();
with (myCircle.graphics) {
beginFill(0x999999,1);
drawCircle(0,0,5);
endFill();
}
myCircle.x=Math.random()*sw;
myCircle.y=Math.random()*sh;
addChild(myCircle);
myArray.push(myCircle);
myArray[n].addEventListener(MouseEvent.MOUSE_DOWN,xDown);
}
stage.addEventListener(Event.ENTER_FRAME,xEnter);
function xEnter(e:Event):void {
myLine.graphics.clear();
for (var nn:Number =0; nn<max; nn++) {
if (nn==max-1) {
with (myLine.graphics) {
lineStyle(1,0xCCCCCC,1);
moveTo(myArray[nn].x,myArray[nn].y);
lineTo(myArray[0].x,myArray[0].y);
}
} else {
with (myLine.graphics) {
lineStyle(1,0xCCCCCC,1);
moveTo(myArray[nn].x,myArray[nn].y);
lineTo(myArray[nn + 1].x,myArray[nn + 1].y);
}
}
addChild(myLine);
}
}
//Rectとなっているのは最初は四角形にしていたのですが
//Circleのほうが動きが良いので、その部分だけ変えたからです。
var myRect:Sprite = new Sprite();
with (myRect.graphics) {
beginFill(0x0099CC,1);
drawCircle(0,0,7);
endFill();
}
myRect.x = myArray[0].x;
myRect.y = myArray[0].y;
addChild(myRect);
var num:Number =0;
myRect.addEventListener(Event.ENTER_FRAME,xRect);
function xRect(e:Event):void {
var speed:Number = 3;
var dx=myArray[num + 1].x-myRect.x;
var dy=myArray[num + 1].y-myRect.y;
var r:Number = Math.atan2(myArray[num +1].y -myRect.y,myArray[num +1].x - myRect.x)*180/Math.PI;
myRect.x += speed * Math.cos(r/180*Math.PI);
myRect.y += speed * Math.sin(r/180*Math.PI);
if ((Math.abs(dx) < 2) &&(Math.abs(dy) < 2)) {
num++;
if (num == 9) num=-1;
}
}
function xDown(e:MouseEvent):void {
e.target.startDrag();
e.target.addEventListener(MouseEvent.MOUSE_MOVE, xMove);
stage.addEventListener(MouseEvent.MOUSE_UP, xUp);
}
function xMove(e:MouseEvent):void {
e.updateAfterEvent();
}
function xUp(e:MouseEvent):void {
e.target.stopDrag();
e.target.removeEventListener(MouseEvent.MOUSE_MOVE, xMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, xUp);
}
}
}
}