Actionscript 3 Designer Toolkit

More design, less programming

Actionscript 3 Designer Toolkit header image 2

Displaying time: the Clock class

March 16th, 2010 · 1 Comment · AS3 Custom Classes

The Clock class displays a digital clock with optional 24 hour display mode and optional display of seconds.  Text properties are controlled by an array which define the text’s placement within the background and the display’s font, size and color.

The class relies on the Actionscript 3 Date class used in conjunction with the toLocalTimeString( ) method which returns time data as a string.   The substr( ) function is then used to extract the hours, minutes, seconds and am/pm elements.

import com.dtk.Clock;
 
// init basic setting
var bgWidth:uint  = 200;
var bgHeight:uint =  60;
var bgColor:uint  = 0x014da3;
 
// array of time display text properties
var textProps:Array = new Array( );
textProps.push( { displayXOffset : 24, displayYOffset : 12, displayFont : 'Arial', displaySize : 30, displayColor : 0xffffff } );
 
/**
 *	CLOCK class: 	set first Boolean param to true to show 24 hour time format
 *			set second Boolean param to true to display seconds
 *			use displayXOffset, displayYOffset to position text on background
 */
var c:Clock = new Clock( bgWidth, bgHeight, bgColor, textProps, false, true );
c.x = 10;
c.y = 10;
addChild( c );

Download files: Clock.zip (50)

Post to Twitter

Tags: ····

One Comment so far ↓

  • Andrew

    Good stuff and thanks for sharing. In my clock class I went about the other way – I made calls individually, translated a bit, and pieced them together as a string. I also had to have multiple date formats that had to be combined with time, though.

    private var _hours:String;
    private var _currentDate:Date;

    _currentDate = new Date();

    // Time Method Calls
    _currentHours = currentDate.getUTCHours();

    // Add zeros if less than 10
    _hours = (_currentHours<10) ? "0"+String(_currentHours)+":" : String(_currentHours)+":";

    rinse, repeat for the other values

Leave a Comment

Spam Protection by WP-SpamFree