I’m trying to create an analog clock that changes color when its PM. The only thing is that the variable won’t update. I made the code a function and put it in a code block that runs every second. To make it clear: The function DOES work, it just doesn’t update the variable ampm which means when the clock passes 12, it won’t change color. The function only gets the variable once from what I can tell. I traced the variable ampm when the local time was 3 PM but even if I changed the time to 3 AM, the variable didn’t change. So it’ll change color correctly when the flash file is first run but if the time goes from AM to PM while it’s running it will not change color.
Here is my function code:
function color():void
{
/* AM PM, if hours is greater than 11, that is 12 and 12 is PM */
if (hours<12)
{
var ampm = "AM";
}
else
{
var ampm = "PM";
}
if (ampm != "AM")
{
colorTransform.color = 0x0000FF;
Dial.transform.colorTransform = colorTransform;
}
else
{
colorTransform.color = 0x000000;
Dial.transform.colorTransform = colorTransform;
}
}
Here is the code that runs this function every second:
var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, clockHandler);
timer.start();
function clockHandler(event:TimerEvent)
{
// set hour
var time = new Date();
var hourHand = time.getHours();
var minuteHand = time.getMinutes();
var secondHand = time.getSeconds();
Hourhand.rotation = 30 * hourHand + minuteHand / 2;
Minutehand.rotation = 6 * minuteHand;
Secondhand.rotation = 6 * secondHand;
color ();
}