b-log – betriebsraum weblog

Software Development, Human-Computer Interaction, Projects…

Overriding getter/setter methods in AS 2.0

April 14, 2006

Maybe I’ve missed something but I’m sure someone can explain this one: Create a AS 2.0 class in flash which extends MovieClip, link it to a library symbol, create two frames in the MovieClip (“up” and “disabled”) and try to override the MovieClip.enabled getter/setter.

The class could look like this:

class TestButton extends MovieClip {
 
	public function TestButton() {
		super();
	}
 
	private function onRelease():Void {
		trace("onRelease");
	}
 
	public function get enabled():Boolean {
		return super.enabled;
	}
 
	public function set enabled(mode:Boolean):Void {
		trace(mode);
		super.enabled = mode;
		this.gotoAndStop(mode ? "up" : "disabled");
	}
 
}

If you instantiate your button via attachMovie and set it to enabled=false it will jump to the disabled frame but the super class behaviour (i.e. really disabling the mc) is never called. Why?
Other strange things occur if you try to override just a setter method in a subclass but not the corresponding getter method. I have the feeling you shouldn’t override getter/setter methods in flash but by now I haven’t heard or read anything about this. The workaround for the above example would be to make java like accessor methods (getEnabled() and setEnabled()).
Some people prefer them to flash getter/setter methods anyway (I myself change from myClass.myProp to myClass.getMyProp() style and vice versa approximately every six months…;)).

Filed under: Flex/AS3

8 Responses to “Overriding getter/setter methods in AS 2.0”

  1. flashape says:

    wierd…i tried out and couldnt get it working. tried it with _visible to, same thing.

  2. Ash says:

    I think its because enabled is treated as a property, and you can’t set properties of a superclass (its a prototype, not an instance).

    I don’t know if its possible without some sneaky hackery.

  3. JesterXL says:

    enabled is basically marked as “final”; it gets confused because the getter/setter name is the same as the base property.

    The solution is to use a watcher instead. It’s heavier, but it works. Look at how it’s done via mx.core.UIComponent (Flash MX 2004 or 8).

  4. Ash says:

    This is the aforementioned hackery..

    It accesses the prototypes directly rather than using super.

    public function get enabled():Boolean {
    return this.__proto__.__proto__.enabled;
    }

    public function set enabled(mode:Boolean):Void {
    trace(mode);
    this.__proto__.__proto__.enabled = mode;
    this.gotoAndStop(mode ? “up” : “disabled”);
    }

  5. felix says:

    thanks for your comments…makes sense now…

  6. Josh says:

    It’s been my experience that you can’t call a super getter/setter. You can override it but you can’t call the parent version with super. Ash’s method probably works though. It’s good to know.

  7. brian says:

    ash’s method will work, but unfortunately it will affect EVERY instance of the same class on the stage, not just the one that is being set.

    any other good ideas?

  8. To override a getter / setter:

    override public function set data(value:Object):void {

    super.data = value;
    }

    override public function get data():Object {

    return super.data;
    }

Add a comment

Follow comments to this post by subscribing to the comment feed.