ActionScript Property - Public Getter, Protected Setter

Is it possible to have a property with an open getter and a secure installer?

I have the following code:

public class Mob extends Sprite { // snip private var _health:Number; // tried making this protected, didn't work public function get health():Number { return _health; } protected function set health(value:Number):void { _health = value; } // snip public function takeDamage(amount:Number, type:DamageType, ... additionalAmountAndTypePairs):void { var dmg:Number = 0; // snip var h:Number = this.health; // 1178: Attempted access of inaccessible property health through a reference with static type components.mobs:Mob. this.health = h - dmg; // 1059: Property is read-only. } } 

I had this.health -= dmg; , but I split it to get more detailed information about compiler errors.

I do not understand how a property will be considered read-only in the same class. I also do not understand how this is not available.

If I make the security field, getter and setter protected, it compiles, but this is not the result that I want; I need health to be readable from the outside.

+4
source share
3 answers

No. Accessories must have the same privilege levels as each other. You can have your public get set functions, then have the setHealth, getHealth protected function. You can change it if you want, but the key point is that you have one set of methods for accessing the public privilege, and another for access at the level of protected privileges.

+5
source

Since you will only be updating _health inside your Mob class, you can write a private function to configure it.

 private function setHealth(value:Number):void { _health = value; } 

And keep the public getter as such.

+3
source

I am not an expert, but I think you can use

 internal var _health:Number; public function get health():Number { return _health; } internal function set health(value:Number):void { _health = value; } 
0
source

All Articles