Posts Tagged ‘show the time in flash’

Last week I was working on automating the house’s coffee machine.  And a real neat feature I saw around was the ability to display updated content in flash.
In bare bones, simple copy and paste, below is the the code I used to update the clock each second.

Basically open a scene of one frame and insert the code below on it.

Here is a sample of my coffee machine with the time below.   The images were done in Photo Impact and the using Flash CS3 and ActionScript 2.0

In your flash script insert in to frame 0 the following code by right clicking on the frame and selecting ‘action’

In my example above just create a dynamic text with the var setting as ‘txtTime’
Please remove the ‘Comments’ before copy and pasting.’ they are for showing what is being done and will give you errors if not removed 🙂

'-------------------- Setup Variables
var timeint;
'---------------- Set the timer to run the function called 'timer' each second or 1000 milliseconds

'------------------------ the function itself
function timer() {

'--------------------- grab the date, and break down by hours, minutes and AM or PM
  oDate = new Date();
  nHours= oDate.getHours();
  nMinutes = oDate.getMinutes();
'------------------------------------ If minutes is less the 10 then add a 0 in front so it looks like 01,02,03 etc.
  if (nMinutes<10) nMinutes="0"+nMinutes;
  nSeconds = oDate.getSeconds();

'------------------------------------ If seconds is less the 10 then add a 0 in front so it looks like 01,02,03 etc.
  if (nSeconds<10) nSeconds="0"+nSeconds;

'---------------------------------------- If hours is over 24 then its PM.
  sAmPm = (nHours < 12) ? " AM" : " PM";
'--------------------------------- if hours >12 then subtract -12

  nHours = (nHours%12 == 0) ? 12 : nHours%12;

'--------------- Update Dynamic variabel to .txtTime on the _root layer. 
  _root.txtTime = nHours + ":" + nMinutes + ":" + nSeconds + sAmPm

stop();   '---------- Remember to stop the movie if not stopped somewhere else.