通过在ActionScript 3中保持其一端固定来旋转直线

时间:2013-03-06 10:11:30

标签: actionscript-3 flex flex3

我想将一个星形线(例如代码中的秒针)旋转到特定的角度,保持一端固定,我能够旋转它,但它的位置正在改变。如何在ActionScript 3中执行此操作?

    public function AnalogClockFace(face:Number)
    {
        this.size = face;
    }

    public  function draw():void
    {
        currentTime = new Date();
        showTime(currentTime);
    }

    public function init():void
    {
        this.graphics.clear();
        this.graphics.lineStyle(3.0,0x000000,3.0);
        this.graphics.beginFill(0xABC123,1.0);
        this.graphics.drawCircle(100,500,size);
        this.graphics.endFill();

        secondHand=new Shape();
        secondHand.graphics.lineStyle(2.0,0x000000,1.0);
        secondHand.graphics.moveTo(100,((500-size)+5));
        secondHand.graphics.lineTo(100,500);
        this.addChild(secondHand);
    }
    public function showTime(time:Date):void
    {
        var seconds:uint=time.getSeconds();
        var minutes:uint=time.getMinutes();
        var hours:uint=time.getHours();         

        this.secondHand.rotation = 180 + (seconds * 6);         
        this.minuteHand.rotation = 180 + (minutes * 6);
        this.hourHand.rotation = 180 + (hours * 30) + (minutes * 0.5);
    }

}

1 个答案:

答案 0 :(得分:0)

这是因为焦点是左上角。因此,这将围绕旋转旋转。必须是你作为左中点的中间位置。为此,请创建父容器名称secondContainer以添加secondHand。在secondHand点移动到相对父级的中心位置之后。然后更改父容器的旋转。

应用以下代码的概念。复制并粘贴。

import flash.events.Event;
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Point;

const CLOCK_CENTER_POINT = new Point(stage.stageWidth/2, stage.stageHeight/2);

var secondContainer:Sprite = new Sprite();
addChild(secondContainer);
secondContainer.x = CLOCK_CENTER_POINT.x;
secondContainer.y = CLOCK_CENTER_POINT.y;

var secondHand:Shape =new Shape();
const SECOND_LINE_WIDTH:Number = 2.0
const SECOND_LINE_HEIGHT:Number = 100;
secondHand.x = SECOND_LINE_WIDTH/2;
secondHand.y = -SECOND_LINE_HEIGHT;
secondHand.graphics.lineStyle(SECOND_LINE_WIDTH,0x000000,1.0);
secondHand.graphics.moveTo(0,SECOND_LINE_HEIGHT);
secondHand.graphics.lineTo(0,0);
secondContainer.addChild(secondHand);

var minContainer:Sprite = new Sprite();
addChild(minContainer);
minContainer.x = CLOCK_CENTER_POINT.x;
minContainer.y = CLOCK_CENTER_POINT.y;

var minHand:Shape =new Shape();
const MIN_LINE_WIDTH:Number = 2.0
const MIN_LINE_HEIGHT:Number = 80;
minHand.x = MIN_LINE_WIDTH/2;
minHand.y = -MIN_LINE_HEIGHT;
minHand.graphics.lineStyle(MIN_LINE_WIDTH,0x00ff00,1.0);
minHand.graphics.moveTo(0,MIN_LINE_HEIGHT);
minHand.graphics.lineTo(0,0);
minContainer.addChild(minHand);

var hourContainer:Sprite = new Sprite();
addChild(hourContainer);
hourContainer.x = CLOCK_CENTER_POINT.x;
hourContainer.y = CLOCK_CENTER_POINT.y;

var hourHand:Shape =new Shape();
const HOUR_LINE_WIDTH:Number = 2.0
const HOUR_LINE_HEIGHT:Number = 50;
hourHand.x = HOUR_LINE_WIDTH/2;
hourHand.y = -HOUR_LINE_HEIGHT;
hourHand.graphics.lineStyle(HOUR_LINE_WIDTH,0xff0000,1.0);
hourHand.graphics.moveTo(0,HOUR_LINE_HEIGHT);
hourHand.graphics.lineTo(0,0);
hourContainer.addChild(hourHand);

stage.addEventListener(Event.ENTER_FRAME, onEnter);

function onEnter(e:Event):void
{
    var time:Date = new Date();

    secondContainer.rotation = time.seconds*6+(time.milliseconds/(1000/6));
    minContainer.rotation = time.minutes*6+(time.seconds/10);
    hourContainer.rotation = time.hours*30+(time.minutes/2);
}