OOPs: Object Oriented Programming

Get Adobe Flash player

The definition for a class is in a separate file. The name of the file is the same as the name of the class with the extension as. The Spinner.as file is shown below:

package {
	import flash.display.MovieClip;
	import flash.events.*;
	public class Spinner extends MovieClip {
		public var speed:int=3; //initial value
		public function Spinner() {
			this.addEventListener(Event.ENTER_FRAME,spin);
		} //constructor
        private function spin(e:Event):void {
	       this.rotation=this.rotation+speed; //rotate itself
		} //spin
	} //class
} //package
  • Notice the following: The keyword package has an opening { at the start. The matching } is on the last line.
  • Anything that the class needs is imported: flash.display.MovieClip and all of the events in flash.events.
  • The Spinner class has an opening { after extends MovieClip and the closing } just before the closing } for the package.
  • Spinner extends MovieClip. That means that a Spinner is a movie clip with some extra features.
  • Each spinner has a public variable speed. That means that we can change it in our movie if we want.
  • There is always a function with just the name of the class. This function is called a constructor. It is executed once when the movie starts.
  • In this movie when add an eventListener for the ENTER_FRAME event.
  • The frames event is completely enclosed inside the Spinner Class which is completely enclosed inside the package.

Get Adobe Flash player

Open the link to Spinner.as and save it to your computer in the same directory where you will create the spinning stars movie. Or create it yourself by selecting File, New, ActionScript 3.0 class. You will be prompted for a name. Name it Spinner. When you save it you will be prompted wit the name Spinner.as.

NEXT: The Spinner Movie: Using a Class