Hi,
In the following code: I'm getting the following "static" from the
ouput window:
**Error** /Users/user/Desktop/code/oop_review/BOX/Box.as: Line 34:
Instance variables cannot be accessed in static functions.
return width*height;
Total ActionScript Errors: 1 Reported Errors: 1
How could I access these instance variables from within this private
static method? Is it advisable using Private Static? Would I have to
create an instance of the class then access the properties?
//AS
class Box {
//accessing a static property through a method.
//The box class, with methods to set and retreive the
//value of the class property, numSides.
//
//The class property numSides
private static var numSides:Number = 4;
//
private var width:Number;
private var height:Number;
private var area:Number;
//
//all CAPS denotes constants, values that never change.
private static var DEFAULT_WIDTH:Number = 30;
private static var DEFAULT_HEIGHT:Number = 20;
//
public static var maxWidth = 250;
public static var maxHeight = 250;
//
//The constructor function.
public function Box(w:Number, h:Number) {
if (w == undefined) {
w = DEFAULT_WIDTH;
}
if (h == undefined) {
h = DEFAULT_HEIGHT;
}
width = w;
height = h;
//initialize area. This is perfectly legal within a constructor
//area = width*height;
}
private static function getArea():Number {
return width*height;
}
//Method for setting numSides.
public function setNumSides(newNumSides:Number):Void {
numSides = newNumSides;
}
//Method for getting numSides.
public function getNumSides():Number {
return numSides;
}
//Accessor to retreive width
public function getWidth():Number {
return width;
}
//Accessor to assign width
public function setWidth(w:Number):Void {
width = w;
}
//Accessor to retreive height
public function getHeight():Number {
return height;
}
//Accessor to set height
public function setHeight(h:Number):Void {
height = h;
}
//reset the dimensions
public function resetDimensions():Void {
height = 1;
width = 1;
trace("Dimensions reset !");
}
public function onMouseDown():Void {
resetDimensions();
}
public function enableReset():Void {
Mouse.addListener(this);
}
public function disableReset():Void {
Mouse.removeListener(this);
}
//this gurantees a safe means of deleting its instances.
public function die():Void {
disableReset();
//Unregister the object so the Mouse class
//deletes the reference to it.
}
}
//End AS
_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com