Quite a few people have asked about this…how to make your SWF change gradually over time, or how to trigger an event when a certain time occurs:
// start by attaching an onEnterFrame function to the stage
// there are alternatives such as using a timer or an interval
// but this technique is the simplest
clock.onEnterFrame = function() {
// the following lines update the clock that's visible in the scene
// the date is continually updated
var clockDate:Date = new Date();
// from which the minutes and hours are retrieved
var minutes:Number = clockDate.getMinutes();
var hours:Number = clockDate.getHours();
// the minutes and hours are now applied to the respective clock hands
// the minute hand is rotated by a factor of 6 (6 * 60 = 360)
this.minutesHand._rotation = minutes * 6;
// the hour hand is rotated by a factor of 30 (12 * 30 = 360). To make this hand
// progress smoothly between the hours, an additional factor is applied
// by dividing the minutes by two
this.hoursHand._rotation = (hours * 30) + (minutes / 2);
// now the fun stuff; getting Flash to react to the time
if (hours >= 16 && hours <= 24) {
// turn on the lights
light._visible = true;
// and increase the night's alpha over time
night._alpha = hours * 5 - 20;
// apply some random animations to the windows
for (var i:Number = 1; i < 38; i++) {
var mc:MovieClip = light["window" + i];
mc.ran = Math.random() * 10000;
if (mc.ran > 9985) {
mc._visible = !mc._visible;
}
}
} else if (hours >= 0 && hours <= 7) {
// same as before; lights on
light._visible = true;
// it's after midnight so the night's alpha is reduced over time
night._alpha = 100 - (hours * 5);
// some more random window lights. Again, it's after midnight so
// the frequency at which they're toggled is reduced
for (var i:Number = 1; i < 38; i++) {
var mc:MovieClip = light["window" + i];
mc.ran = Math.random() * 10000;
if (mc.ran > 9999) {
mc._visible = !mc._visible;
}
}
} else {
// it's daylight so the lights and night are turned off
light._visible = false;
night._alpha = 0;
}
// lastly, a demonstration of using a Switch/Case statement to react to time.
// whenever the hours change, the appropriate commands in each
// case statement are triggered, for example updating a textfield.
switch (hours) {
case 0 :
messageTxt.text = "The witching hour arrives";
break;
case 4 :
messageTxt.text = "The streets are swept clean";
break;
case 6 :
messageTxt.text = "The workers prepare for a new day";
break;
case 8 :
messageTxt.text = "The city welcomes the new day";
break;
case 12 :
messageTxt.text = "The workers head off for lunch";
break;
case 16 :
messageTxt.text = "The city begins to wind down the working day";
break;
case 20 :
messageTxt.text = "The residents get ready to party";
break;
default :
// this is the default condition if none of the case statements above
// are true.
messageTxt.text = "";
break;
}
};
Anyone who’s made a countdown timer, or a clock, should be familiar with the techniques used here but a clock has been thrown in anyway. The FLA and SWF are attached. To see it in action, run the SWF and then adjust your system clock.
Edit: The SWF can be viewed here