OOPs: Object Oriented Programming

Let's suppose we have a spinning star. The code would be

this.addEventListener(Event.ENTER_FRAME,frames);

function frames(e:Event):void {
	star.rotation=star.rotation+1;
} //frames

Now, if we want to have 3 spinning stars, we would name them, star1, star2 and star3. The code would be:

this.addEventListener(Event.ENTER_FRAME,frames);

function frames(e:Event):void {
	star1.rotation=star1.rotation+1;
	star2.rotation=star2.rotation+1;
	star3.rotation=star3.rotation+1;
} //frames
Get Adobe Flash player

Objects

A movie like the one above, or one with 100 stars would be a lot of work! Fortunately, there is a much easier way: create some code for a Spinner, and then just say that each of the stars is a Spinner. The code that describes what a Spinner is is called a class. Each star is an instance of the Spinner Class and will spin automatically.

Terminology:

  • Class: a definition of the object. We could describe a Car. It has an engine, 4 wheels, a vin number. It has properties like location and owner. It has methods like drive(), wash(), and sell().
  • Instance: one member of the class. We can not take our definition of a car and actually go anywhere with it. In order to do that, we need an instance of a car. (In Flash, a MovieClip class is the one in the library, an instance is on the stage.)
  • Extends: We can define a truck as an extension of a car. A truck is everything a car is but with storage area in the back. We do not have to say that it has an engine, etc. because we have said that it extends a car.
  • Override: A car has 4 wheels. If our truck has 6 wheels, then we need to override the default value of 4 for wheels.

Note: Every MovieClip we create extends the basic MovieClip. All movieclips have the properties x, y, width, height and more. They have the methods play() and stop(). They have functions like totalFrames and current frames.

NEXT: A Spinner Class