A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: I need Help with a AS 2

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    32

    I need Help with a AS 2

    If somebody can help me, I need an AS 2 for a normal clock: has 3 hands,2 for hours, and one for seconds.
    That is to say make hands move according to the time,
    thank you for your help.

  2. #2
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Don't know why you want 2 hands for hours, but oh well, here you go:

    I thought of this way by myself, and I think it's pretty clever, as the code is really short, compared to professional written codes specifically for analog clocks. So, I thought of catching the computer's time, and multiplying it as many times, as that it's equivalent to the degrees for that hand on the analog clock

    So, make 3 hands, facing UP, straight UP, or else the degrees won't work correctly, and make sure the registration point (cross, plus sign in middle), is at the bottom of your hand, like in this image:



    Make 2 copies of that movieclip, so that you have 3 hands, 2 for the hour, and 1 for the seconds. Give the hour hands, the instance name of hour1 and hour2, and the seconds movieclip, second1, and then write this on your frame:

    Actionscript Code:
    onEnterFrame = function(){
        date = new Date();
        hours = date.getHours();
        minutes = date.getMinutes();
        seconds = date.getSeconds();
        hour1._rotation = (hours+minutes/60)*30;
        hour2._rotation = (hours+minutes/60)*30;
        second1._rotation = seconds*6;
    }

    Explanation:

    Actionscript Code:
    // we assign an onEnterFrame loop to keep updating the time
    onEnterFrame = function(){
        // we make a new Date object, to catch the computer's time
        date = new Date();
        // variable which contains the hours
        hours = date.getHours();
        // variable which contains the minutes, this is essential
        // to find the correct degrees for the hour hand
        minutes = date.getMinutes();
        // variable for the seconds
        seconds = date.getSeconds();
       
        // set the rotation for hour1, hours+minutes/60, which
        // gives us the accurate degrees for the hour hand, and that
        // times 30, to convert it to degrees
        hour1._rotation = (hours+minutes/60)*30;
        hour2._rotation = (hours+minutes/60)*30;
        // set the rotation for second1, seconds times 6, which
        // converts it to seconds
        second1._rotation = seconds*6;
    }

    So, I thought that a circle has 360 degrees, and if 360 degrees divided by 60 seconds, equals 1 seconds = 6 degrees, and thus, we just multiply the seconds with 6 to find the correct degrees

    For the hours, 360 degrees divided by 12 hours, equals 1 hour = 30 degrees, but that will only give the hour, so if the times is 11.30, the hours catched from the computer will be 11, and that times 30, will give us only one position, and when it changes to 12, it will snap to the degrees corresponding at hour 12. To prevent that, we catch the minutes, divide them with 60, to convert them to hours, and then add that to the hours, and multiply the whole thing with 30 to find the accurate degree

    I hope you understood that, and I discovered this way myself, which is why I feel proud
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    32
    Quote Originally Posted by Nig 13 View Post
    Don't know why you want 2 hands for hours, but oh well, here you go:

    I thought of this way by myself, and I think it's pretty clever, as the code is really short, compared to professional written codes specifically for analog clocks. So, I thought of catching the computer's time, and multiplying it as many times, as that it's equivalent to the degrees for that hand on the
    I hope you understood that, and I discovered this way myself, which is why I feel proud
    Of course you are, thank you for your time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center