|
||||||||||||||
. . . . . |
Flash ActionScript 2.0 - Class Inheritance
Every Flash Actiosnscript class has attributes and behaviours. Attributes can describe the state or qualities of an object:
class Car{
// declare attributes
private var carColor:String;
private var make:String;
private var year:Number;
private var style:String;
}
MethodsMethods define the behaviours of our classes. What do we want our car to do? how about move.. and stop. Lets 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
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.
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(); Are you interested in pursuing a career in web programming? There are many online college degrees in Information Technology, so you can live your aspirations. Locate school grants as well as higher education jobs to assist you. 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: 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. var s1:Square = new Square(100); var c1:Circle = new Circle(50); trace(s1.getArea()); trace(c1.getArea());
Output: So polymorphism does work. .. Try out all the other methods . Try and bust it.. bend it.. shake it.. SuperClass Constructors that require Arguments
In our above superclass , Shape , we had a constructor :
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 ?
// 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);
}
|
|
||||||||||||
|
. | Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Games | free backgrounds | Resume | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | . . |
||||||||||||||