scottc
December 18th, 2008, 09:52 AM
What is the best way to use the getters/setters inside of a constructor?
//a simple class that draws a line from the origin...
package au.com.scottcampbell.draw
{
import flash.display.Sprite;
public class line extends Sprite{
//Private vars
private var _c:uint;//color
private var _a:uint;//alpha
private var _w:Number;//width
private var _h:Number;//height
public function line(nw:Number = 0,nh:Number = 0,nc:uint = 0x000000,na:uint = 100){
a(na);//set the alpha
c(nc);//set the color
w(nw);//set the width
h(nh);//set the height
render();//we want it rendered off the bat? yes
}
public function render():void{//update the graphics
graphics.clear();
graphics.lineStyle(0,_c,_a*0.01); //calculate the alpha into a decimal
graphics.moveTo(0, 0);
graphics.lineTo(_w, _h);
}
// Getters/Setters
public function set a(n:Number):void{
if(n > 100){//make sure the alpha is less then 100%
_a = 100;//should i cap this to 255 instead?
}else if(n < 0){//make sure its greater then 0%
_a = 0;
}else{
_a = n;
}
//render(); //should we render it once we have set the a value?
}
public function get a():uint{
return _a;
}
//...etc..etc...
I'm trying to improve my oops/as3 so try be as critical as possible. :P
Also whats a good naming convention for the parameters for the constructor. I start them all with "n" for new. (which i dont feel is a good way of doing things)
//a simple class that draws a line from the origin...
package au.com.scottcampbell.draw
{
import flash.display.Sprite;
public class line extends Sprite{
//Private vars
private var _c:uint;//color
private var _a:uint;//alpha
private var _w:Number;//width
private var _h:Number;//height
public function line(nw:Number = 0,nh:Number = 0,nc:uint = 0x000000,na:uint = 100){
a(na);//set the alpha
c(nc);//set the color
w(nw);//set the width
h(nh);//set the height
render();//we want it rendered off the bat? yes
}
public function render():void{//update the graphics
graphics.clear();
graphics.lineStyle(0,_c,_a*0.01); //calculate the alpha into a decimal
graphics.moveTo(0, 0);
graphics.lineTo(_w, _h);
}
// Getters/Setters
public function set a(n:Number):void{
if(n > 100){//make sure the alpha is less then 100%
_a = 100;//should i cap this to 255 instead?
}else if(n < 0){//make sure its greater then 0%
_a = 0;
}else{
_a = n;
}
//render(); //should we render it once we have set the a value?
}
public function get a():uint{
return _a;
}
//...etc..etc...
I'm trying to improve my oops/as3 so try be as critical as possible. :P
Also whats a good naming convention for the parameters for the constructor. I start them all with "n" for new. (which i dont feel is a good way of doing things)