Tuesday, July 17, 2012

Inheritance in AS 3.0

Inheritance:
is one of the most powerful technequies of Object Oriented Programming. It refers to the way a child inherits the properties, metthods and events of parent or super class ( also known as parent-child relationship). We can create a new class that behaves like a another class which helps in saving effort and time
Inheritance is used to extend the abiltiy of exisisting class instantiate. Suppose a class Area has methods length() and breath(), class Volume which extends class Area has default methods length() and breath() and in addition to this class Volume can have its own methods like height() etc..
Lets start creating parent class Animal, with a constructor that indicates when an instance of the class is instantiated using a trace statement.
Note : Any class that inherits the Animal class gets all of its methods and interfaces



Download Source :http://flashsource.in/AS3OOPs1.html
Animal.as
package
{
public class Animal
{
// Constructor.
public function Animal( ):void
{
trace("Animal Created");
}
public function walk( ):void
{
trace("Parent Animal : is walking on the ground ")
}
public function Type( ):void
{
trace("Type of Animal ")
}
}
}
Now create subclasses DemosticAnimal and WildAnimal which extend the Animal class.
DemosticAnimal.as
package
{
public class DemosticAnimal extends Animal
{
// Constructor.
public function DemosticAnimal( ):void
{

}
//to change the default Type to custom Type ,we will override the Type() method
public override function Type( ):void
{
trace("this is Demostic Animal ")
}
//we can add our own methods also in extended class
public function Trained(){
trace("Demostic aninmals are Trained to respond to the master's command");
}
}
}
WildAnimal.as
package
{
public class WildAnimal extends Animal
{
// Constructor.
public function WildAnimal( ):void
{

}
//to change the default Type to custom Type ,we will override the Type() method
public override function Type( ):void
{
trace("this is Wild Animal ")
}
}
}
Now create Main class
Main.as
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main( ):void
{
var animal1:DemosticAnimal=new DemosticAnimal( );
animal1.walk( );
animal1.Type( );
animal1.Trained( );
var animal2:WildAnimal=new WildAnimal( );
animal2.walk( );
animal2.Type( );
}
}
}
Now run the flash document class window and we get the following output.
Animal Created
Parent Animal : is walking on the ground
this is Demostic Animal
Demostic aninmals are Trained to respond to the master's command

Animal Created
Parent Animal : is walking on the ground
this is Wild Animal

Looking at ouptut , both animal1 and animal2 instances display the superclass constructor message and message in superclass method wailk() , in addition to this displays the custom message by overridding Type() method of superclass and message defined in new addition method Trained().
 Inheritance is useful for code resuability with a provision to extend abilities.

Simple Compostion in AS3.0

Composition
"is the means by which one object acquires another object's functionality by controlling an instance of that object. This is an alternative to inheritance that, though requires a little more effort, is often more flexible and does a better job enforcing encapsulation. 
Also known as “Has a” relationship.
Composition, an alternative form of inter-object relationship, often competes with inheritance as an OOP design technique. In composition, one class (the "front end class") stores an instance of another class (the "back end class") in an instance property. The front end class delegates work to the back end class by invoking methods on that instance.- ( Essential ActionScript 2.0 ).
To understand composition, we will start with a simple example by creating a Backend class -->
CompositionExample.as
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
public class CompositionExample {
private var nSpeed:int;
private var mc:MovieClip;
/*constructor*/
function CompositionExample(target:Stage, mc:MovieClip, xLoc:Number, yLoc:Number,nspeed:int) {
nSpeed=nspeed;
this.mc=mc;
this.setLoc(xLoc,yLoc);
target.addChild(mc);
target.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}
private function setLoc(xLoc,yLoc:Number):void {
mc.x=xLoc;
mc.y=yLoc;
}
private function onEnterFrame(e:Event):void {
moveObjects();
}
private function moveObjects():void {
mc.x += nSpeed;
mc.y += nSpeed;
if (mc.x >= 400 || mc.x <= 0) {
nSpeed = nSpeed*-1;
}
if (mc.y >= 300 || mc.y <= 0) {
nSpeed = nSpeed*-1;
}
}
}
}
Explanation :
Constructor
function CompositionExample(target:Stage, mc:MovieClip, xLoc:Number, yLoc:Number,nspeed:int) conatins the arguments 1.targer:stage which is refrence to root (Display Object container) which is used to to dispaly Object ( works for adding child to stage)
2. mc:MovieClip used to store the instance of another class (Ball class).
3. xLoc:Number & yLoc:Number gives the intial x an y coordiantes of displyed object , which is defined by method this.setLoc(xLoc,yLoc).
4. nspeed:int gives the speed of Object on stage.
Now Create FrontEnd Class Main.as
Here we will create two new instances of Ball class as shown in code
package {
import flash.display.*;
public class Main extends MovieClip {
public function Main() {

var Object1:CompositionExample=new CompositionExample(this.stage,new Ball(0x0000cc,15),100,50,5);
var Object2:CompositionExample=new CompositionExample(this.stage,new Ball(0xff0000,25),200,150,5);


}
}
}

As we see Main class creates the new instance of Ball class (see Ball.as) thus depicting the 'has a relationship'
Ball.as
package {
import flash.display.*;
public class Ball extends MovieClip {
public var _color:int;
public var _raduis:Number;
public function Ball(color:int,rad:Number) {
/*We will set the radius and color of Ball in frontEnd Main class where instance of ball is created ( viz. new Ball(0xff0000,15)) */_color=color;
_raduis=rad;
graphics.beginFill(color);
graphics.drawCircle(0,0,rad);
}
}
}
For Fla and SWF Download visit http://flashsource.in/AS3OOPs2.html

Thursday, February 25, 2010

New flash actionscript tutorial site

www.flashsource.in

free Flash actionscript 3.0 tutorials for games , elearning, design pattern ,animation - effects physics math and Object oriented programming for Flash AS3

www.flashsource.in