As3 Main
As3: Digital Clocks
I haven't found one tutorial on making a clock in as3 so far, hopefully this will clear things up.
I'll start with showing the full code, with comments for each important part.
// Create new TextField variable
var textBox:TextField = new TextField();
// Add the TextField to the stage
addChild(textBox);
// Create function that performs enterframe tasks
function refreshTimes(Event)
{
/*
Please note that these tasks
are done constantly in order
to have an up to date time.
In As2, the date updated itself,
whereas in As3, you must recreate
the Date var consistantly
*/
// Make new Date variable
var date:Date = new Date();
// Variables that add a 0 to seconds and minutes under 10
var hourZero:String = new String();
var minuteZero:String = new String();
var secondZero:String = new String();
// Retreave dates for hours seconds and minutes
// Turn dates into Number variables
var h:Number = date.getHours();
var m:Number = date.getMinutes();
var s:Number = date.getSeconds();
// Checks seconds/minutes and adds 0 if under 10
if (s < 10)
{
secondZero = "0";
}
else
{
secondZero = "";
}
if (m < 10)
{
minuteZero = "0";
}
else
{
minuteZero = "";
}
if (h < 10)
{
hourZero = "0";
}
else
{
hourZero = "";
}
// Create variable to store hours minutes and seconds combined
var displayTime:String = (hourZero + h + ":" + minuteZero + m + ":" + secondZero + s);
// Display the time String on the stage
textBox.text = displayTime;
}
//Performs the above function every frame
stage.addEventListener(Event.ENTER_FRAME, refreshTimes);
As briefly stated in the code, unlike as2, you must update the time variable constantly, in my method anyway.
I've also added 0's before each number under 10, as it is not ready formatted this way.
This clock is fully made in code, so I'm not too bothered about it's dull placement and appearance, but feel free to play around with your versions!
Enjoy!
~ Crushy