根据Math.cos和Math.sin来实现不同的方位判断,想着借助该Demo可以做成时钟计时的小事例、类似于弹弹堂中的调整炮弹发射方向的小Demo。主要根据下面函数来计算:
var angleRadians:Number = angle * Math.PI / 180; //换算成弧度
var p:Point = new Point(Math.cos(angleRadians) * RADIUS, Math.sin(angleRadians) * RADIUS); //计算偏移量
AS3中的示例代码:
package mytest
{
import flash.display.Sprite;
import flash.geom.Point;
public class MyTest extends Sprite
{
private const CENTRE:Point = new Point(100, 150); //圆心
private const RADIUS:Number = 100; //半径
private const START_ANGLE:Number = 15; //起初角度
private const Tile_TOTAL:Number = 9; //组件个数
private const SINGLE_ANGLE:Number = 360 / Tile_TOTAL; //平均角度
public function MyTest()
{
for(var i:uint=0; i var pb:Tile = new Tile(); //新建组件 var angle:uint = i * SINGLE_ANGLE + START_ANGLE; //角度 var p:Point = getPoint(angle); pb.x = p.x; pb.y = p.y; //移动组件 pb.rotation = angle - 90; //旋转组件 this.addChild(pb);//加到舞台 } } private function getPoint(angle:uint):Point{ var angleRadians:Number = angle * Math.PI / 180; //换算成弧度 var p:Point = new Point(Math.cos(angleRadians) * RADIUS, Math.sin(angleRadians) * RADIUS); //计算偏移量 p.x += CENTRE.x; //跟据圆心对偏移量进行修正 p.y += CENTRE.y; //跟据圆心对偏移量进行修正 return p; } } } package mytest { import flash.display.Sprite; public class Tile extends Sprite { public function Tile() { super(); graphics.lineStyle(1, 0x0000ff); graphics.beginFill(0xcccccc,0.5); graphics.endFill(); graphics.drawRect(0, 0, 50, 20); } } } AS3效果图: JAVA中的示例代码: /**一个时钟Demo的雏形*/ package mychTest; import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; public class EclipsePaint extends Applet{ private Graphics drawOffScreen; private Image offScreen; private int appWidth, appHeight; private Clock clock; //钟表实体类 public void init(){ appWidth = getSize().width; appHeight = getSize().height; offScreen = createImage(appWidth, appHeight); drawOffScreen = offScreen.getGraphics(); clock = new Clock(appWidth, appHeight); new Thread(new PaintThread()).start(); } public void paint(Graphics g){ clock.draw(drawOffScreen); g.drawImage(offScreen, 0, 0, this); } public void update(Graphics g){ paint(g); } class PaintThread implements Runnable{ @Override public void run() { while(true){ try { Thread.sleep(41); } catch (InterruptedException e) { e.printStackTrace(); } repaint(); } } } } class Clock{ private int x, y, width, height, appWidth, appHeight; //外圆框的基本信息 private int heartX, heartY, heartWidth, heartHeight; //圆心坐标和宽度、高度 private int minuteAimX, minuteAimY; //秒针的X,Y坐标 private double minuteAngle; //秒针的当前角度 public Clock(int appWidth, int appHeight){ this.appWidth = appWidth; this.appHeight = appHeight; width = 80; //外圆直径,记住是直径 height = 80; x = appWidth / 2 - width / 2; //绘制外圆的X坐标 y = appHeight / 2 - height / 2; //绘制外圆的Y坐标 heartWidth = 5; //圆心直径 heartHeight = 5; heartX = x + width / 2 - heartWidth/2; //绘制圆心的X坐标 heartY = y + height / 2 - heartHeight/2; //绘制圆心的Y坐标 minuteAngle = 60; //当前角度 } public void draw(Graphics g){ //绘制外圆框 g.setColor(Color.RED); g.drawOval(x, y, width, height); //绘制中心点 g.setColor(Color.BLACK); g.fillOval(heartX, heartY, heartWidth, heartHeight); //绘制秒线 g.setColor(Color.BLUE); minuteAimX = (int)(Math.cos(minuteAngle * Math.PI / 180) * width/2) + (x + width / 2); minuteAimY = (int)(Math.sin(minuteAngle * Math.PI / 180) * height/2) + (y + height / 2); g.drawLine((x + width / 2), (y + height / 2), minuteAimX, minuteAimY); //这里的(x + width / 2), (y + height / 2)相当于圆心的X坐标,Y坐标; 因为heartX, heartY减去了圆心的半径,所以只是适用于绘制的时候 } } JAVA效果图: