+ Reply to Thread
Results 1 to 4 of 4

Thread: Flash: Timers Part I

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    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. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97

    Re: Flash: Timers Part I

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

  3. #3
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    Re: Flash: Timers Part I

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

  4. #4
    Guru MathX has a spectacular aura about MathX has a spectacular aura about MathX's Avatar
    Join Date
    Oct 2008
    Location
    Kosovo
    Age
    19
    Posts
    4,007

    Re: Flash: Timers Part I

    Superb!

+ 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: 286
    Last Post: 05-07-2010, 08:03 PM
  2. Hacking Flash Games (PART 2)
    By TcM in forum Security Tutorials
    Replies: 114
    Last Post: 03-28-2010, 06:28 PM
  3. Replies: 6
    Last Post: 08-28-2009, 06:27 PM
  4. Java: Random Name Generator
    By Sinipull in forum Classes and Code Snippets
    Replies: 0
    Last Post: 08-05-2009, 04:43 AM
  5. Adobe Releases Flash Player 9 for Linux
    By Onur in forum Linux/Unix General
    Replies: 1
    Last Post: 08-28-2007, 04:03 PM