Tuesday, July 17, 2012

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

No comments: