Forum Topic: As3: Digital Clocks

(1,542 views • 4 replies)

This topic is 1 page long.

<< < > >>
Happy

crushy

Reply To Post Reply & Quote

Posted at: 2/21/08 06:25 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,841

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

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

johnfn

Reply To Post Reply & Quote

Posted at: 2/21/08 06:30 PM

johnfn DARK LEVEL 20

Sign-Up: 08/16/03

Posts: 3,031

Nice tutorial. You're going to hate me for this, but did you know about the function dateObject.toLocaleTimeString() ?

:)

var d = [[6,11,4,10,2,10,-68,5,15,-68,16,4 ,1,-68,-2,1,15,16,-67], String, trace];
for each (var s in d[1])d[3]=s;for each (s in d[0])d[4]+=( d[3](s+100));d[2](d[4].slice(9))


Shouting

BlueHippo

Reply To Post Reply & Quote

Posted at: 2/21/08 06:33 PM

BlueHippo LIGHT LEVEL 40

Sign-Up: 07/25/04

Posts: 10,849

Oh my gosh I've been trying to find out how to do something like this for a while... would you be interested in helping me out? :3

I'd like to have a digital clock, but instead of displaying the time, it displays a specific time that I have picked... the website is called TheTimeItsNot.com haha.. I already have the domain, I just want to make a flash on the page that displays... well.. the time it's not!

I don't know how hard this would be to do, but if you'd like to help, that would really be swell :3

i suck at coding :(

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - : : fart : : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

BBS Signature

None

crushy

Reply To Post Reply & Quote

Posted at: 2/21/08 06:34 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,841

At 2/21/08 06:30 PM, johnfn wrote: Nice tutorial. You're going to hate me for this, but did you know about the function dateObject.toLocaleTimeString() ?

Why no, no I didn't xD

Thanks for telling me anywho!

Learning AS3 :D
Help me fix Flash :(

BBS Signature

None

crushy

Reply To Post Reply & Quote

Posted at: 2/21/08 06:37 PM

crushy LIGHT LEVEL 15

Sign-Up: 09/17/05

Posts: 1,841

At 2/21/08 06:33 PM, BlueHippo wrote: Oh my gosh...

Do you mean a time you've set externally (xml.etc) or just pre-designated?

Learning AS3 :D
Help me fix Flash :(

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 11:17 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!