Classes

In addition to the primitive data types such as int and Number ActionScript has hundreds of special types called classes that are builtin. 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. WWhen we declare a variable of this type ww must use the word new.

One important class that is very usefiul 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.


INDEX, Timer Class, Sound Class, URLRequest to play a sound, Start and Stop a Sound with a Channel, URLRequest to Open the Browser, Add New Child at Runtime, List to download classes
Next lesson: Document Class

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Timer Class