Main

January 03, 2007

Timer Class - ActionScript 3.0

Listen to this article Listen to this article :: Talkr

The Timer class is a new addition to AS 3.0. The Timer class is similar to the setInterval function in AS 2.0 but has some more features added to it. One of such feature is the ability to start and stop the timer when needed.

Timer in AS 3.0 behaves a little different than the setInterval call in AS 2.0. A timer fires a timer event at regular intervals (the interval which is defined when defining the timer). As with the case of setInterval one can specify the time interval in which the event has to be fired.

The Timer class is part of the flash.utils package and has to be imported before one can use it. The declaration goes like this:

import flash.utils.Timer;

As mentioned earlier a Timer has a event associcated with it which is called a TimerEvent. The TimerEvent class is present in the flash.events package and if thie TimerEvent is used, one has to import the event before using it.

import flash.events.TimerEvent;

Now for the syntax of declaring a Timer it goes like this:

var myTimer:Timer = new Timer(delay, iterations);

One primary difference between the setInterval call and using a Timer is that, the Timer has a extra parameter which says how many times the timer has to fire (iterations). If you want the timer to iterate forever then the iteration can be declared as 0. This would keep firing the Timer forever.

To listen to the timer you need to add a listener which listens to the TimerEvent.

myTimer.addEventListener(TimerEvent.TIMER, drawRectangle);

and then start the timer.

myTimer.start();

At this point its also important to note that the getTimer function in AS 2.0 still exists and is in the package flash.utils and can be used as:

flash.utils.getTimer();

Sample code:

package {
     import flash.utils.Timer;
     import flash.utils.getTimer;
     import flash.events.TimerEvent;
     import flash.display.Sprite;
    

     public class TimerExample extends Sprite
     {
          public function TimerExample()
          {
               var myTimer:Timer = new Timer(100, 10);
               myTimer.addEventListener(TimerEvent.TIMER, drawRectangle);
               myTimer.start();
           }
          private function drawRectangle(evt:TimerEvent):void
          {
                trace(flash.utils.getTimer()+ " : " + evt);
          }
      }
}

The other useful properties that are present in the Timer class are:

currentCount - Returns the total number of times the timer has fired since it started at zero.

repeatCount - Returns the total number of times the timer is set to run.

running - Returns true if the timer is running else false.

delay - Returns the delay in milliseconds and can also be set while the timer is running.

The other two methods of the Timer class which the example above doesn't cover are:

stop() - Stops the current running timer.

reset() - Resets the currently running timer and sets the current count to 0.

Another event which is associated with the TimerEvent is:

TimerEvent.TIMER_COMPLETE - This event is fired when the timer is completed and can used as:

myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, drawRectangle);

December 08, 2006

AS3 Primer : Packages

Listen to this article Listen to this article :: Talkr

If you are used to coding in classes, the first notable difference you will find in AS3 is the usage of packages. Every class definition starts with the keyword "package" and then is followed by the actual class definition(s). In AS3 all classes MUST be placed inside a package such as:

package {

    public class Example {

    }

What exactly is a package?

A package is a simple way of organizing classes (of related content) into groups. It's similar to the com.xxx.yyy notation and is synonymous with directory in the file system. So, say for example I declare a package like this:

package com {

   public class Example {

   }

}

This ideally means that the class exists in the directory named com which is relative to the place where my FLA resides. The same applies to the example below:

 

package com.lah {

}

In this case the class file would exist in a subfolder named lah inside the com folder.

What is the purpose of using packages?

The simple purpose would be to better organize your classes and to avoid conflicts of classes. Say for example you have two classes with the same name which are responsible for doing two different tasks. Lets take a simple example of a login system and our application has two types of users - teachers and students. I would want to package all my teacher management related operations in a package named com.lah.teachermgmt and com.lah.studentmgmt. Considering that the login mechanism for the student and teacher is different I would have two LoginManager classes one in the package com.lah.teachermgmt and yet another in com.lah.studentmgmt. In this case, I can have two classes with the same name but under different packages.

By convention, package names are always lowercase characters as against class names which starts with a capital letter.

Usage Example:

Filename : com\lah\Example.as

package com.lah {

   public class Example{

      public function Example()

      {

           trace("Example");

       }

   }

}

Filename : Example.fla

import com.lah.*;

// for importing all the classes in the package com.lah or one can use import

// com.lah.Example; to import only a specific class in a package.

var myExample:Example = new Example(); //traces "Example"