Newsletter: Learn More ||| Multimedia Presentation Software |||
Flash Actionscript 2.0 - Class Inheritance

Contents

Introduction - creating a class

Every class has attributes and behaviours. Attributes can describe the state or qualities of an object:
Say we have a Car class. It might have the attributes of:

Color: red, green, blue.
Style - sedan, station wagon, urban assault vehicle.
Make - Toyota, Holden, Ford , Daihatsu
Attributes can also have information about its state.
has4WheelDrive - true , false;
currentGear - 1,2,3,4,R;

Let's define a Car class(save it as Car.as) with what we have so far:

class Car{
// declare attributes
private var carColor:String;
private var make:String;
private var year:Number;
private var style:String;
}

Methods

Methods define the behaviours of our classes. What do we want our car to do? How about move.. and stop. Let's add them:

class Car{
// declare attributes
private var xpos:Number;
private var ypos:NUmber;
private var speed:Number;
private var carColor:String;
private var make:String;
private var year:Number;
private var style:String;
// Methods
public function move()
{
xpos +=speed;
}
public function stop()
{
speed = 0;
}

}

Inheritance

Inheritance means that subclasses of a higher class inherit all the methods and data(variables) from their superclass, or higher class.

The classic superclass example is Shape. What sort of class would be a subclass of Shape? How about Square , Circle. Let's write our superclass Shape:

class Shape{
// declare variables
private var area:Number;
private var posx:Number;
private var posy:Number;
private var speed:Number;
private var direction:String;
// Constructor
public function Shape(xpos:Number, ypos:Number)
{
posx = xpos;
posy = ypos;
}
// declare methods
public function setArea(parea:Number):Void
{
area = parea;
}
public function getArea():Number
{
trace("superclass getarea method");
return area;
}
public function move(speed, dir)
{
if(dir=="up") posy -= speed;
if(dir=="down") posy += speed;
if(dir=="left") posx -= speed;
if(dir=="right") posx += speed;
trace("x = " + posx + ", y = "+posy);
}
}

Now we want a Square class that is a subclass of Shape. Shape is the "base" class and Square will be the derived class.

Superclass = base class
SubClass = Derived Class

The word we use for inheritance is "extends". So in our class definition we write that :
class Square extends Shape{

Then Square will inherit all the attributes and methods of Shape. And we can create extra attributes of its own such as numberOfSides, width, height, angle, whatever you need. When you use Inheritance you save time, because the base attributes and methods already exists. You also reduce errors, because the base class, Shape, has already been used and tested.

You already know how the base class works. Inheritance is useful because it makes classes re-usable and extendable. So let's write it:

class Square extends Shape{
// declare variables
private var width:Number;

// constructor
public function Square(pwidth:Number)
{
width = pwidth;
}
}

But we know that the area of our square can come from width*width . We could "override" the inherited method getArea(). We could write our own implementation that would do a better job in this class. We will override our getArea method then:

class Square extends Shape{
// declare variables
private var width:Number;

// constructor
public function Square(pwidth:Number)
{
width = pwidth;

}
// overRidden getArea method
public function getArea():Number
{
trace("Square getArea's overridden method");
area = width * width;
return area;
}
}

Now that Square is a subclass of Shape we can use Shape's methods. We can write:

var s1:Square = new Square(100);
s1.move();
s1.getArea();

Polymorphism
Which getArea() method did s1 use? Did it use the Shape or Square getArea method? Answer: the Square class's getArea method. If a superclass's method is declared and implemented in the derived(sub) class, then we say that it has been overwritten. Our circle class that we will write will have a getArea that returns PI times radius squared. Using the same method name to implement different code in subclasses is called PolyMorphism. There are many implementations associated with a method depending upon the object associated with the method name:

Circle.getArea(){PI * radius*radius ;)
Square.getArea(){
width*width ;}   
Triangle.getArea{
   .5*base *height;}   

Write your Circle class which will inherit from the Shape class as well:

class Circle extends Shape{ 
// declare variables
private var radius:Number;

// constructor
public function Circle(pradius:Number)
{ 
radius = pradius;

} 
// overRidden getArea method
public function getArea():Number
{ 
trace("inside circle's overridden getarea method");
area = Math.PI* radius* radius;
return area;
} 
} 

Now test if our square and circle classes implement their own overridden getArea method. Open a new flash file and put this in the actions window, frame 1:

var s1:Square = new Square(100);
var c1:Circle = new Circle(50);
trace(s1.getArea());
trace(c1.getArea());


Output:
Square getArea's overridden method
10000
inside circle's overridden getarea method
7853.98163397448

So polymorphism does work. Try out all the other methods.

SuperClass constructors that require arguments

In our above superclass, Shape, we had a constructor:

public function Shape(posx:Number, posy:Number){ ...

When we write our own superclass constructor, we need to include a constuctor for every subclass we create. We have already done that, but we must call the parent's constructor. We do this by using the keyword super. Since our Shape constructor takes 2 arguments, our x and y coordinates our super must call these like so:

super(xpos , ypos);

I had better do it properly then:

class Square extends Shape{
// declare variables
private var width:Number;

// constructor
public function Square(px:Number, py:Number, pwidth:Number)
{
super(px,py);
width = pwidth;

}
// overRidden getArea method
public function getArea():Number
{
trace("Square getArea's overridden method");
area = width * width;
return area;
}
}

And we had better fix up the Circle class, too:

class Circle extends Shape{
// declare variables
private var radius:Number;

// constructor
public function Circle(px:Number, py:Number,pradius:Number)
{
super(px,py);
radius = pradius;

}
// overRidden getArea method
public function getArea():Number
{
trace("inside circle's overridden getarea method");
area = Math.PI* radius* radius;
return area;
}
}

And the code in Flash will have to be changed as well.

var s1:Square = new Square(20, 50, 100);
var c1:Circle = new Circle(100, 100, 50);
trace(s1.getArea());
trace(c1.getArea());
// And test to see if Move method works
_root.onEnterFrame = function()
{
s1.move( 2, "down");
};

What happened to the x coordinate? Try moving up and left and right. Try adding methods. Try adding some graphics to the classes. Try writing a Square class using the flash drawing API with lineTo. Here is a clas my good friend Lithium wrote as an example.

// polygon class by lithium. http://www.shife.com/lithium/
// Usage: var Poly:Polygon = new Polygon();
// Poly.createPoly(radius, sides);

//Polygon.as

dynamic class Polygon
{
public function createPoly(radius, points)
{
nextDepth = _root.getNextHighestDepth();
name = "Polygon" + nextDepth;
level = nextDepth;
var poop = _root.createEmptyMovieClip(name, level);

poop.lineStyle(2, 0x000000, 100);

var startx = Math.sin(0) * radius;
var starty = Math.cos(0) * radius;
poop.moveTo(startx, starty);
var trans = Math.PI / 180;

for(var i = 0; i < points+1; i++)
{
var angle = i * (360/points);
var x = (Math.sin(angle * trans) * radius);
var y = (Math.cos(angle * trans) * radius);
trace("x : " + x);
trace("y : " + y);
trace("angle : " + angle);
poop.lineTo(x, y);
poop.moveTo(x, y);
}
poop.lineTo(startx, starty);
}
}

And here is how you implement it in your Flash MX2004 file -

// Polygon.fla

var poly:Polygon = new Polygon();
for(i = 0; i < 10; i++)
{
poly.createPoly(150, 3+i);
}

This tutorial provided by www.video-animation.com.
 
©2010 Wildform, Inc | Policies | Contact Us | Newsletter Options
 
Wildform provides a 100% satisfaction guarantee on all our Flash software. If you are not completely satisfied with our Flash multimedia software for any reason you may request a refund within 15 days of purchase.