I'm making an analog clock on Adobe Flash that I can manually set to display a certain time using ActionScript 2.0. What code could work for it?

71 views Asked by At

I'm setting up an analog clock with adjustable hour and minute hands that can be manipulated in a separate frame. I'm used NumericStepper components so that I can set the hour and minute of the clock. The goal is for the hour and minute hands to be set to the values listed in the NumericStepper components.

I already got the clock to display the current computer time:

time = new Date();
seconds = time.getSeconds();
minutes = time.getMinutes();
hours = time.getHours();

hours = hours + (minutes/60);

seconds = seconds*6;
minutes = minutes*6;
hours = hours*30;

sec._rotation = seconds;
min._rotation = minutes;
hour._rotation = hours;

And here's the form: Picture of the form on screen

Update: I was finally able to make the form and output the value of the NumericStepper to some text boxes, so I think I'm in the right direction:

var palletteArray = new Array(); 

palleteArray = ["0x7FAAD4", "0xFFDF7F"]; 

function setTheme() { 
  _global.style.setStyle("themeColor", palleteArray[0]); 
  _global.style.setStyle("backgroundColor", palleteArray[1]); 
  _global.style.setStyle("fontSize", 11); 
  _global.style.setStyle("fontWeight", "bold"); 
  _global.style.setStyle("color", palleteArray[0]); 
} 

setTheme(); 
 
var stepsListener = new Object(); 
stepsListener.change = function() { 
  populateTextBoxes(); 
}; 

stepperHour.addEventListener("change", stepsListener); 
stepperMin.addEventListener("change", stepsListener); 

function populateTextBoxes() { 
  TextBox1.text = stepperHour.value;
  TextBox2.text = stepperMin.value;
}
1

There are 1 answers

0
Organis On

So. The problem can be decomposed into these sub-problems:

  1. How to display analog clock — you solved this one.
  2. How to update the clock if there are any changes.
  3. How to input changes — you solved this one, presumably.
  4. How to sync the input and the display.

Let's start with the №4. In AS2 there is a _global object, it is visible from any context and can be used to share data between separate parts of code. It goes as following:

_global.sharedDate = new Date();

Well, in order to have it initialized at any place, but not overwrite it, you need to check if it is already in place first. Put this to the both places you are working with:

if (_global.sharedDate == undefined)
{
    _global.sharedDate = new Date();
}

Good. Now, how to have your analog clock always in sync with it? The onEnterFrame handler fires every frame and is intended to keep your display updated by code. I'll just copy and modify what you have there:

function onEnterFrame()
{
    // Get the reference to the shared data.
    var aTime = _global.sharedDate;
    
    // Extract time values from the shared data.
    seconds = aTime.getSeconds();
    minutes = aTime.getMinutes();
    hours = aTime.getHours();
    
    // Rotate the clock hands.
    sec._rotation = seconds * 6;
    min._rotation = minutes * 6;
    hour._rotation = (hours + (minutes /60)) * 30;
}

Then, the last part. You need to update the shared data upon user interaction. I assume that code of yours is correct, so I am going only to add the update the shared data bit into your existing function:

function populateTextBoxes()
{ 
    TextBox1.text = stepperHour.value;
    TextBox2.text = stepperMin.value;
    
    // Get the reference to the shared data.
    var aTime = _global.sharedDate;
    
    // Update the shared data with the new time values.
    aTime.setHours(stepperHour.value);
    aTime.setMinutes(stepperMinute.value);
}

It was 15 years since I used AS2 and I am kind of not fluent (well, who is?), but the general idea should be correct and I believe you'll figure it out from here on.