Skip to main content
Dave_Hollings
Inspiring
April 30, 2009
Question

AS3 Timer Function Help

  • April 30, 2009
  • 1 reply
  • 2513 views

Hi,

I am trying to create a desktop countdown timer (personal project) however I am getting some issues with the script.  The countdown timer works fine until I try and get user inputs.  I have created some variables  to store the end date and have a series of input fields for the user to provide their desired date.

I have provided the complete actionscript below in the hope this will help idenitfy the issue.  Basically when the user inputs their desired date and click OK the application takes these values converts them to a number and assigns them to the date variables.  Then it is supposed to update the countdowntimer start counting down from this new date value.

I have used trace to ensure the initial values are correct (which they are) and the ensure the user inputs are getting through and they are, I just can't seem to get the timer to refresh with the updated end date.

CODE

// import flash common system classes
import flash.desktop.*;
import flash.net.*;
import flash.display.*;
import flash.events.*;
import flash.system.Capabilities;
import fl.transitions.*;
import fl.transitions.easing.*;
import flash.text.*;
import flash.display.Sprite;
 
// Create Variables and data type them to store application end Date
var myYear:Number;
var myMonth:Number;
var myDay:Number;
 
//Create user variables to store user inputs
var userYear:String;
var userMonth:String;
var userDay:String;
 
// Give application varibales initial values
myYear = 2009;
myMonth = 6;
myDay = 2;
 
// As Jan is referenced in AS as 0 -1 from the actual month number;
myMonth = myMonth -1;
 
//Assign a new Date variable with the target date initially set.
var endDate:Date = new Date(myYear,myMonth,myDay);
trace (endDate);
 
//create time with a delay of 1 second
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
 
//Update timer function fired every second
function updateTime(e:TimerEvent):void
{
     var now:Date = new Date();
     var timeLeft:Number = endDate.getTime() - now.getTime();
     var seconds:Number = Math.floor(timeLeft / 1000);
     var minutes:Number = Math.floor(seconds / 60);
     var hours:Number = Math.floor(minutes / 60);
     var days:Number = Math.floor(hours / 24);
     
     seconds %= 60;
     minutes %= 60;
     hours %= 24;
     
     var sec:String = seconds.toString();
     var min:String = minutes.toString();
     var hrs:String = hours.toString();
     var d:String = days.toString();
     
     if(sec.length < 2) {
          sec = "0" + sec;
     }
     if(min.length < 2) {
          min = "0" + min;
     }
     if(hrs.length < 2) {
          hrs = "0" + hrs;
     }
     if(d.length == 2) {
          d = "0" + d;
     }
     if(d.length == 1) {
          d = "00" + d;
     }
     var time:String = d + ":" + hrs + ":" + min + ":" + sec;
     
     countDown_mc.time_txt.text = time;
}
 
//create event listener
background_mc.addEventListener(MouseEvent.MOUSE_DOWN, back_CLICK);
close_btn.addEventListener(MouseEvent.CLICK, closeButton_CLICK);
settings_btn.addEventListener(MouseEvent.CLICK, settingsbtn_CLICK);
settings_mc.ok_btn.addEventListener(MouseEvent.CLICK, okbtn_CLICK);
settings_mc.cancel_btn.addEventListener(MouseEvent.CLICK, cancelbtn_CLICK);
 
//***** ASSIGN FUNCTIONS ******
//function to action when back MC listener is activated
function back_CLICK(e:MouseEvent):void
{
     stage.nativeWindow.startMove();
}
 
//function to action when the close button listener is activated
function closeButton_CLICK(e:MouseEvent):void
{
     NativeApplication.nativeApplication.exit();
}
 
//Set function for click event on settings button
function settingsbtn_CLICK(e:MouseEvent):void
{
     //Setup an alpha tween on the timer display
     var clockAlphaTween1:Tween = new Tween(countDown_mc, "alpha", Strong.easeOut, 1,0,2,true);
     //Listen for when the tween has finished and then run function
     clockAlphaTween1.addEventListener(TweenEvent.MOTION_FINISH, clockAlphaTween1_FINISH);
}
 
//Function for when alpha tween above is finished
function clockAlphaTween1_FINISH(e:TweenEvent):void
{
     //Setup an alpha tween for the settings mc
     var settingsAlphaTween1:Tween = new Tween(settings_mc, "alpha",Strong.easeOut, 0,1,2,true);
     //Setup a motion tween on the x axis for settings mc
     var settingsXTween1:Tween = new Tween(settings_mc, "x",Strong.easeOut, 416, 96, 2,true);
}
 
//Set function for click event for the cancel button
function cancelbtn_CLICK(e:MouseEvent):void
{
     //Setup an alpha tween on the settings mc
     var settingsAlphaTween2:Tween = new Tween(settings_mc, "alpha",Strong.easeOut, 1,0,2,true);
     //Setup a motion tween on the x axis for setting mc
     var settingsXTween2:Tween = new Tween(settings_mc, "x",Strong.easeOut, 96, 416, 2,true);
     //Listen for when the tween above has finished then run function
     settingsXTween2.addEventListener(TweenEvent.MOTION_FINISH, settingsXTween2_FINISH);
}
 
//Function for when motion tween has finished
function settingsXTween2_FINISH(e:TweenEvent):void
{
     //Setup alpha tween for timer display
     var clockAlphaTween2:Tween = new Tween(countDown_mc, "alpha", Strong.easeOut, 0,1,2,true);
}
 
//Set function for click event on ok button
function okbtn_CLICK(e:MouseEvent):void
{
     //Assign user inputed values to variables
     userYear = settings_mc.yearInput_txt.text;
     userMonth = settings_mc.monthInput_txt.text;
     userDay = settings_mc.dayInput_txt.text;
     
     //Convert string values to number data type and replace app variables with these values
     myYear = Number(userYear);
     myMonth = Number(userMonth)-1;
     myDay = Number(userDay);
     
     //Close the settings mc with alpha and motion tweens
     var settingsAlphaTween3:Tween = new Tween(settings_mc, "alpha",Strong.easeOut, 1,0,2,true);
     var settingsXTween3:Tween = new Tween(settings_mc, "x",Strong.easeOut, 96, 416, 2,true);
     //Listen for when motion tween has finished and run function
     settingsXTween3.addEventListener(TweenEvent.MOTION_FINISH, settingsXTween3_FINISH);
}
 
//Function for when motion tween has finished
function settingsXTween3_FINISH(e:TweenEvent):void
{
     //Run functions
     clearSettings();
     applyNewTimer();     
     //Setup motion tween on x axis for timer display
     var clockAlphaTween3:Tween = new Tween(countDown_mc, "alpha", Strong.easeOut, 0,1,2,true);
}
 
//Function to clear the setting input fields
function clearSettings()
{
     settings_mc.yearInput_txt.text = "";
     settings_mc.monthInput_txt.text = "";
     settings_mc.dayInput_txt.text = "";
}
 
//Function to assign new endDate with new user values.
function applyNewTimer()
{
     var endDate:Date = new Date(myYear,myMonth,myDay);
     trace (endDate);
}
This topic has been closed for replies.

1 reply

Inspiring
May 1, 2009

if its a timer issue; I think your missing the repeat count, which is why it stops after its first run, modify to match with the follwing:

var countdownTimer:Timer = new Timer(1000, 999);// infinite amount of repeats??

Timer(delay:Number, repeatCount:int = 0)
Constructs a new Timer object with the specified delay and repeatCount states.
If that doesn't solve your problem, try posting the errors you get, or better yet post your fla online so I have a better idea of whats going on
Ned Murphy
Legend
May 1, 2009

The default repeat count for a Timer is 0.  A repeat count value of 0 repeats infinitely.

Inspiring
May 1, 2009

yep, you are absolutely right Ned, I just wasn't sure if 0 meant no repeat or infinity...

so your saying that if you don't define a delayTimer then it defaults to loop indefinately? or is it a must to define the second parameter on the Timer?