+ Reply to Thread
Results 1 to 4 of 4

Thread: Flash: Timers Part I

  1. #1
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    Flash: Timers Part I

    Timers

    You create a new Timer by creating a new Timer object. The constructor takes two parameters: the number of milliseconds to run and the number of events to cause.

    One use of a timer is to create a clock. This Timer would run forever and would generate an event every second.

    Creating a new timer

    Code:
    var clockTimer:Timer = new Timer(1000); // this timer generates an event every one second
    var timer2:Timer = new Timer(2000,2); // this timer generates an every every 2000 seconds. It only generates 2 events before stopping.
    The Date

    To get the current date, we create a date object and use methods like getHours(), getMinutes, getSeconds() and other methods.

    This code gets the time in the format h:mm:ss

    Code:
    var systemDate:Date = new Date();
    var minutes:int = systemDate.getMinutes();
    var hours:int = systemDate.getHours();
    var seconds:int = systemDate.getSeconds();
    var sSuffix:String;
    var sMinutes:String;
    var sSeconds:String;
    
    if (hours > 12) {
    	// time is in 24 hour time, so subtracting 12
    	// puts it in 12 hour time
    	hours -= 12;
    	sSuffix = "P.M."; 
    } else {
    	sSuffix = "A.M.";
    }
    
    if (minutes < 10) {
    	sMinutes = "0" + String(minutes);
    } else {
    	sMinutes = String(minutes);
    }
    
    if (seconds < 10) {
    	sSeconds = "0" + String(seconds);
    } else {
    	sSeconds = String(seconds);
    }
    
    trace("The current time is " + String(hours) + ":" + sMinutes + ":" + String(sSeconds) + " " + sSuffix);
    Example output:

    The current time is 1:06:59 P.M.

    Notice, how I subtract 12 from the hours? This is because time is given in 24 hour time. If we want to display it in 12 hour time, we subtract 12 hours and append a suffix to the end.

    Now instead of tracing the command to the output, we can display the time in a label. Then all we need to do is put that code in a callback function for a timer and we will have ourselves a live clock in action script.

    So, add a label to the stage and change it's name to lblTime. Feel free to change the formatting of the label to anything you want.

    Creating a new timer:

    Code:
    var clockTimer:Timer = new Timer(1000);
    The time changes every second. So we set the interval to every 1000 milli seconds. This timer generates events forever.

    Now we to add an event listener so we can call a function everytime the timer generates an event.

    Code:
    clockTimer.addEventListener(TimerEvent.TIMER,updateTime);
    The updateTime function is simply going to contain the code from above. However, instead of showing text in the output window we are going to update the label.

    Here is the function:

    Code:
    function updateTime(e:TimerEvent):void {
    	var systemDate:Date = new Date();
    	var minutes:int = systemDate.getMinutes();
    	var hours:int = systemDate.getHours();
    	var seconds:int = systemDate.getSeconds();
    	var sPrefix:String;
    	var sMinutes:String;
    	var sSeconds:String;
    	
    	if (hours > 12) {
    		// time is in 24 hour time, so subtracting 12
    		// puts it in 12 hour time
    		hours -= 12;
    		sPrefix = "P.M."; 
    	} else {
    		sPrefix = "A.M.";
    	}
    	
    	if (minutes < 10) {
    		sMinutes = "0" + String(minutes);
    	} else {
    		sMinutes = String(minutes);
    	}
    	
    	if (seconds < 10) {
    		sSeconds = "0" + String(seconds);
    	} else {
    		sSeconds = String(seconds);
    	}
    	
    	lblTime.text =String(hours) + ":" + sMinutes + ":" + sSeconds + " " + sPrefix;
    }
    Now, all we have to do is start the timer outside of the function.

    Code:
    clockTimer.start();
    You can view the result here.

    In the next tutorial, we will look at how to stop timers. We will also look at how to create timers that don't run forever.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Flash: Timers Part I

    What, no screenshots this time?
    Nicely done. +rep

  4. #3
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    Re: Flash: Timers Part I

    There wasn't a need for screen shots this time.

  5. #4
    Join Date
    Oct 2008
    Location
    Istog, Kosova
    Posts
    4,001
    Blog Entries
    1
    Rep Power
    40

    Re: Flash: Timers Part I

    Superb!
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Hacking Flash Games (PART 1)
    By TcM in forum Security Tutorials
    Replies: 298
    Last Post: 01-16-2012, 06:42 PM
  2. Hacking Flash Games (PART 2)
    By TcM in forum Security Tutorials
    Replies: 119
    Last Post: 09-04-2011, 06:10 PM
  3. Beginner C# - Working With Timers
    By CommittedC0der in forum CSharp Tutorials
    Replies: 0
    Last Post: 08-26-2011, 12:22 PM
  4. C++ Multithreads Timers
    By H2O Pure in forum C and C++
    Replies: 7
    Last Post: 12-10-2010, 07:23 AM
  5. Which Thread do Timers run on?
    By NeedHelp in forum C# Programming
    Replies: 3
    Last Post: 07-17-2006, 01:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts