PDA

View Full Version : override get and set


hobbis
09-29-2006, 03:13 PM
Getting some strange behaviour trying to override a private var:


private var _hit:Boolean = false;

public function get hit():Boolean { return _hit; }
public function set hit(b:Boolean):void
{
_hit = b;
}


//in child class

override public function get hit():Boolean { return _hit; }
override public function set hit(b:Boolean):void
{
_hit = b;
asset_mc.animation_mc.gotoAndPlay("capture");
}


In the child class, it doesn't know about _hit:

ReferenceError: Error #1065: Variable _hit is not defined.

So I guess I have to declare the _hit var in the child class as well? This seems a bit strange. Can someone explain how to override private properties using the get set methods? Thanks

nirth
09-29-2006, 06:02 PM
Private fields are visible only in current class, use "protected" key word, if you want to use class member in subclasses.

dr_zeus
09-29-2006, 06:55 PM
For this case, you can actually skip the protected variable:


override public function set hit(b:Boolean):void
{
super.hit = b;
asset_mc.animation_mc.gotoAndPlay("capture");
}

You don't need to override the getter if it has the exact same functionality. That should save you a line of redundant code. Also, you can use the "super" keyword in the setter to access the functionality that you're overriding.