我的代码有问题

时间:2013-04-08 06:43:10

标签: actionscript-3 optimization timer

我有一个fla文件和一个名为theAlarmClock的外部类。 时钟上的指针应该移动,但在Ctrl+Enter之后 - 它们是空闲的。

package 
{
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;

public class theAlarmClock extends MovieClip
{
    var myDate:Date;
    var mySec:int;
    var myMin:int;
    var myHou:int;
    var myTimer:Timer = new Timer(1000);

    public function theAlarmClock()
    {

        myTimer.addEventListener(TimerEvent.TIMER, updateClock);
        myTimer.start();


        function updateClock(t:TimerEvent):void
        {
            myDate = new Date();
            mySec = myDate.seconds;
            myMin = myDate.minutes;
            myHou = myDate.hours;
            clockHandSeconds_mc.rotation = mySec * 6;
            clockHandMinutes_mc.rotation = myMin * 6;
            clockHandHours_mc.rotation = myHou * 30 + myMin * 0.5;
            trace("tick");


        }


    }
}

}

2 个答案:

答案 0 :(得分:0)

您应该尝试Date - 类的get函数。

请在updateClock函数中尝试使用此代码:

myDate = new Date();
mySec = myDate.getSeconds();
myMin = myDate.getMinutes();
myHou = myDate.getHours();
clockHandSeconds_mc.rotation = mySec * 6;
clockHandMinutes_mc.rotation = myMin * 6;
clockHandHours_mc.rotation = myHou * 30 + myMin * 0.5;
trace("tick");

答案 1 :(得分:0)

package {


    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.sensors.Accelerometer;
    import flash.utils.Timer;

    public class ToolClass extends Sprite
    {
        private var myDate:Date;
        private var mySec:uint;
        private var myMin:uint;
        private var myHou:uint;
        private var myClock:Sprite;
        private var myHour:Shape;
        private var myMins:Shape;
        private var mySeco:Shape;

        private var myTimer:Timer=new Timer(1000);

    public function ToolClass()
    {
        drawClock();

        myTimer.addEventListener(TimerEvent.TIMER,updateClock);
        myTimer.start();
    }

    private function drawClock():void
    {
        myClock=new Sprite();
        myClock.graphics.lineStyle(0,0x000000,1);
        myClock.graphics.drawCircle(0,0,100);
        addChild(myClock);
        myClock.x=150;
        myClock.y=150;

        myHour=new Shape();
        myHour.graphics.lineStyle(5,0x00000,1);
        myHour.graphics.lineTo(0,-50);
        myClock.addChild(myHour);

        myMins=new Shape();
        myMins.graphics.lineStyle(2,0x000000,1);
        myMins.graphics.lineTo(0,-80);
        myClock.addChild(myMins);


        mySeco=new Shape();
        mySeco.graphics.lineStyle(1,0x000000,1);
        mySeco.graphics.lineTo(0,-80);
        myClock.addChild(mySeco);


    }
    public function getTime():void
    {
        myDate=new Date();

        myHou=myDate.hours;
        myMin=myDate.minutes;
        mySec=myDate.seconds;

    }
    private function updateClock(e:TimerEvent):void
    {
        getTime();
        myHour.rotation=myHou*30+30*myMin/60;
        myMins.rotation=myMin*6+6*mySec/60;
        mySeco.rotation=mySec*6;
        trace("tick");
    }
    }
}