flash on 2011-7-28
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Point;
public class Main extends Sprite
{
public function Main()
{
addChild(Circle.c1);
addChild(Circle.c2);
Circle.init();
addChild(Circle.tf);
addChild(Circle.line);
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFormat;
class Circle extends Sprite
{
public static var c1:Circle = new Circle(140, 140, 30);
public static var c2:Circle = new Circle(300, 300, 70);
public static var line:Sprite = new Sprite();
public static var tf:TextField;
public var pos:Point;
public var radius:Number;
public static function init():void
{
tf = new TextField();
tf.defaultTextFormat = new TextFormat("_typeWriter", 12, 0x0, true);
tf.width = 465;
tf.autoSize = "center";
drawDistLine();
updateMessage();
}
public function Circle(x:Number, y:Number, radius:Number)
{
pos = new Point(x, y);
this.x = x, this.y = y;
this.radius = radius;
draw();
buttonMode = true;
addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
public function draw(lineColor:int = 0x0):void
{
graphics.clear();
graphics.lineStyle(2.0, lineColor);
graphics.beginFill(0x0, 0);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
private function onMouseDown(event:MouseEvent):void
{
startDrag();
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onMouseUp(event:MouseEvent):void
{
stopDrag();
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void
{
if (isCollision(c1, c2))
{
c1.draw(0xED1A3D);
c2.draw(0xED1A3D);
}
else
{
c1.draw();
c2.draw();
}
drawDistLine();
updateMessage();
}
public static function drawDistLine():void
{
line.graphics.clear();
line.graphics.lineStyle(1.0, 0x0);
line.graphics.moveTo(c1.x, c1.y);
line.graphics.lineTo(c2.x, c2.y);
line.graphics.lineStyle(1.0, 0x7F7F7F);
line.graphics.beginFill(0xFFFFFF);
line.graphics.drawCircle(c1.x, c1.y, 3);
line.graphics.drawCircle(c2.x, c2.y, 3);
line.graphics.endFill();
}
public static function updateMessage():void
{
tf.text = "円1の半径 + 円2の半径 = " + (c1.radius + c2.radius) + "\n";
tf.appendText("円同士の二点間距離 = " + Math.sqrt(Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2)).toFixed(2));
}
public static function isCollision(c1:Circle, c2:Circle):Boolean
{
if (Math.pow(c1.x - c2.x, 2) + Math.pow(c1.y - c2.y, 2) <= Math.pow(c1.radius + c2.radius, 2)) return true;
else return false;
}
}