Math.atan2 Example
/**
* Copyright greentec ( http://wonderfl.net/user/greentec )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/uDnW
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
public class FlashTest extends Sprite {
public var _bitmap:Bitmap;
public var whiteCircle:Shape;
public var blackCircle1:Shape;
public var blackCircle2:Shape;
public var p1:Point = new Point(465 / 2 - 40, 465 / 2);
public var p2:Point = new Point(465 / 2 + 40, 465 / 2);
public function FlashTest() {
// write as3 code here..
_bitmap = new Bitmap(new BitmapData(465, 465, false, 0x292929));
addChild(_bitmap);
whiteCircle = new Shape();
whiteCircle.graphics.beginFill(0xffffff);
whiteCircle.graphics.drawCircle(p1.x, p1.y, 30);
whiteCircle.graphics.drawCircle(p2.x, p2.y, 30);
whiteCircle.graphics.endFill();
addChild(whiteCircle);
blackCircle1 = new Shape();
blackCircle1.graphics.beginFill(0x0);
blackCircle1.graphics.drawCircle(p1.x, p1.y, 10);
blackCircle1.graphics.endFill();
addChild(blackCircle1);
blackCircle2 = new Shape();
blackCircle2.graphics.beginFill(0x0);
blackCircle2.graphics.drawCircle(p2.x, p2.y, 10);
blackCircle2.graphics.endFill();
addChild(blackCircle2);
addEventListener(Event.ENTER_FRAME, onLoop);
}
private function onLoop(e:Event):void
{
var angle:Number = Math.atan2(mouseY - p1.y, mouseX - p1.x);
blackCircle1.x = Math.cos(angle) * 18;
blackCircle1.y = Math.sin(angle) * 18;
angle = Math.atan2(mouseY - p2.y, mouseX - p2.x);
blackCircle2.x = Math.cos(angle) * 18;
blackCircle2.y = Math.sin(angle) * 18;
}
}
}