<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Patrick Gunderson &#187; Counting</title>
	<atom:link href="http://pat.theorigin.net/tag/counting/feed/" rel="self" type="application/rss+xml" />
	<link>http://pat.theorigin.net</link>
	<description>Art, Design, Code</description>
	<lastBuildDate>Mon, 03 May 2010 19:02:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial: Two Methods to Make a Countdown Timer</title>
		<link>http://pat.theorigin.net/2008/10/28/tutorial-two-methods-to-make-a-countdown-timer/</link>
		<comments>http://pat.theorigin.net/2008/10/28/tutorial-two-methods-to-make-a-countdown-timer/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 08:44:44 +0000</pubDate>
		<dc:creator>Patrick</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Adobe Flash]]></category>
		<category><![CDATA[AS2]]></category>
		<category><![CDATA[Counting]]></category>
		<category><![CDATA[Timer]]></category>

		<guid isPermaLink="false">http://pat.theorigin.net/?p=70</guid>
		<description><![CDATA[Setting up a countdown timer can be helpful in a number of situations, particularly in situations that have a time limit or deadline. Here are two methods for creating and stopping countdown timers. To set the scene, place a dynamic textfield on stage and have it listen for the variable _root.timeLeft . Next draw a [...]]]></description>
			<content:encoded><![CDATA[<p>Setting up a countdown timer can be helpful in a number of situations, particularly in situations that have a time limit or deadline. Here are two methods for creating and stopping countdown timers. To set the scene, place a dynamic textfield on stage and have it listen for the variable _root.timeLeft . Next draw a simple box stage, select it and convert it to a MovieClip by pressing the f8 key. Give your movieclip the name stopButton. Remember to also give your instance the name stopButton by clicking back to the Stage, selecting your box and naming it in the properties palette.</p>
<h3>Counting Frames Method</h3>
<p>First let&#8217;s go over the more basic, but ultimately more complex method. Since  movieclips can execute commands every frame using an onEnterFrame event handler (a specially named function) if we know the framerate, we can count the frames and divide to find the number of seconds past.</p>
<p>Declare a new variable timeLeft and give it a value of 10 (our counter will count down from 10). Create a new Movieclip called tenSecondTimer and assign it properties for frameRate, and counter. We&#8217;ll use the counter to count how many frames have passed.</p>
<pre class="code">var timeLeft:Number = 10;
createEmptyMovieClip("tenSecondTimer", getNextHighestDepth());
tenSecondTimer.frameRate = 24; // this movie runs at 24 fps
tensecondTimer.counter = 0;</pre>
<p>Now create an onEnterFrame event handler, which will be executed with every frame, that increases the counter, compares it to the framerate, and if they match (a second has passed) reduce the timeLeft by one. for this function we&#8217;ll use the modulo operator (%), the increment operator(++) and the decrement operator (&#8211;). The modulo operator (%divides two numbers and returns the remainder. if the remainder is 0 then the first number is evenly divisible by the second. The increment operator (++) increases the value by one. x++ is the same as x = x + 1. The decrement operator (&#8211;) reduces the value by 1. x&#8211; is the same as x = x &#8211; 1.</p>
<pre class="code">tenSecondTimer.onEnterFrame = funtion(){
    this.counter++;
    if (this.counter % this.frameRate == 0){
        _root.timeLeft--;
    }
}</pre>
<p>In addition to decreasing the timeLeft after every second, we want it to do something when the time is up. Then remove the timer.</p>
<pre class="code">tenSecondTimer.onEnterFrame = funtion(){
    this.counter++;
    if (this.counter % this.frameRate == 0){
        _root.timeLeft--;
    }
    if (timeLeft &lt;= 0){
        trace("TIME'S UP");
        this.removeMovieClip();
    }
}</pre>
<p>We also have the button which we want to stop the clock, so we can define an onRelease event handler for it.</p>
<pre class="code">stopButton.onRelease = funtion(){
    trace("TIMER STOPPED");
    _root.tenSecondTimer.removeMovieClip();
}</pre>
<h3>setInterval Method</h3>
<p>The second method for creating a countdown timer uses the setInterval command. setInterval creates a timer that will execute a function after a certain number of milliseconds. Otherwise the method is similar. This simplifies things a bit because now we don&#8217;t have to count frames to determine if a second has passed, flash will do that for us.</p>
<p>Setup the stage the same way as above, with the textfield and the stopButton.</p>
<p>Declare timeLeft, but this time we don&#8217;t need a counter or framerate, so all se need is to make a setInterval.</p>
<pre class="code">var timeLeft:Number = 10;

// execute the function countdown every 1000 miliseconds (1 second);
tenSecondTimer = setInterval(countDown, 1000);</pre>
<p>now we need to define a function CountDown to decrease the limeLeft every second, and to do something once timeleft is 0.</p>
<pre class="code">function countDown(){
    timeLeft--;
    if (timeLeft &lt;= 0){
        trace("TIME'S UP");
        clearInterval(tenSecondTimer);
    }
}</pre>
<p>Be sure to clear the interval when time is up and when the stop button is clicked or else you will get &#8220;TIME&#8217;s UP&#8221; messages every second for the rest of your movie!</p>
<pre class="code">stopButton.onRelease = funtion(){
    trace("TIMER STOPPED");
    clearInterval(_root.tenSecondTimer);
}</pre>
<p>There you have it. Two different methods to display a countdown timer and to stop the countdown with a stop button.</p>
<p>About the author: Patrick Gunderson is a web developer and designer at TWBA\Tequila in Los Angeles.</p>
]]></content:encoded>
			<wfw:commentRss>http://pat.theorigin.net/2008/10/28/tutorial-two-methods-to-make-a-countdown-timer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
