Class Variables in ActionScript3

In addition to the primitive data types such as int and Number ActionScript has hundreds of special types called classes that are built-in. They can do everything from playing a sound to opening a browser to a particular page.

These special types are called classes and always start with an uppercase letter. When we declare a variable of this type we must use the word new.

One important class that is very useful is the Timer. Get Adobe Flash player

In this movie we have a spinning star. Instead of using the frame event, this movie uses a timer.

  1. Start a new Flash movie.
  2. Draw a star and make it a movie clip.
  3. Add a layer at the top and name it actionscript:
  4. Write the code as shown below:
var spinTimer:Timer=new Timer(50); //how often does the timer go off?
spinTimer.addEventListener(TimerEvent.TIMER,spin); //listen for the timer to go off
spinTimer.start(); //timer is not automatically "set" to go off
function spin(e:TimerEvent): void { 
  star.rotation=star.rotation+1; 
}

The first line declares the timer as type Timer. The next part says that spinTimer is a new instance of the Timer class. Classes can take parameters, the timer has a parameter telling how often we want the timer to go off. We need to listen for the timer to go off, and we also have to start the timer.

We can change the interval either in the variable declaration or through the delay property:

spinTimer.delay=20;

To Do: Change the interval of the timer in the first line to make the star spin faster of slower. Add buttons to change the delay property and make the star spin faster or slower.

NEXT: Sound Class