archive: September 2009

30
SEP
2009

One of the problems using the flash date function is that if you need to manually test a time period and ‘start’ from a certain time there really is no ‘built in’ function to force the date function not to use your system clock. I was getting frustrated that I could set the starting clock. That would still work for me to see how either the sky was being set or how many stars would appear at night time, but I needed to keep the internal clock synced as I had a lot of time checks for star removal towards the early morning period. So I cam up with a solution which I will show you how.

var updateTime:Timer=new Timer(1000);

This is the timer I have setup to run every second. It runs all my time related check movies.

updateTime.addEventListener(TimerEvent.TIMER, updateAllMovies);
updateTime.start();

Create the listener for the timer to run the function needed.

private function updateAllMovies(e:TimerEvent = null):void {
 updateFuncTime();
 updateMovies();
}

The updateFuncTime() is where I run all my date/time settings and is the bread and butter of the operation.

private function updateFuncTime() {

 if ((DEBUG) && (! setArea)) {

 var tmpdateCount:Date = new Date();

 year_tmp=year_tmp>-1?year_tmp:tmpdateCount.getFullYear();
 month_tmp=month_tmp>-1?month_tmp:tmpdateCount.getMonth();
 date_tmp=date_tmp>-1?date_tmp:tmpdateCount.getDate();
 hour_tmp=hour_tmp>-1?hour_tmp:tmpdateCount.getHours();
 mins_tmp=mins_tmp>-1?mins_tmp:tmpdateCount.getMinutes();
 secs_tmp=secs_tmp>-1?secs_tmp:tmpdateCount.getSeconds();
 msec_tmp=msec_tmp>-1?msec_tmp:tmpdateCount.getMilliseconds();

 dateCount=new Date(year_tmp,month_tmp,date_tmp,hour_tmp,mins_tmp,secs_tmp,msec_tmp);

 setArea=true;

 } else if ((DEBUG) && (setArea)) {

 dateCount=addSeconds(dateCount,1);

 } else {

 dateCount= new Date();

 }

 year=dateCount.getFullYear();
 month=dateCount.getMonth();
 date=dateCount.getDate();
 hour=dateCount.getHours();
 mins=dateCount.getMinutes();
 secs=dateCount.getSeconds();
 msec=dateCount.getMilliseconds();

}

private static function addSeconds(date:Date, secs:Number):Date {
 return new Date((secs*1000)+date.getTime());
}

This is the major part of this code. Granted above you see I have a lot of variables from lines 7 through 13. These are the temp values along with the DEBUG variables that set my intended time and/or date. No matter the new ‘dateCount’ class gets picked apart (Lines 29 through 35) and sets the needed variables that my flash header uses. As it runs this function every second and after the initial ‘set’ (Line 17) I go ahead the second time around and add only one (1) second to the current time ‘dateCount’ object (Line 21) and returns the ‘new object’ so when I run lines 29 through 35 again it sets the correct values.

I have been kicking myself for about a week trying to figure this out and it took another through reading into the date class documentation of Adobe’s LiveDoc page for ActionScript to get it right.

If you have any questions and/or comments, please be sure to leave them below and I will try to help you, but I think you got it now! Enjoy!

Comments 0

Commenting is closed

No Comments Yet