Inspectable and Inheritance

While I’ve had problems with Flash’s Inspectable tag in the past, I still actively use it in my daily component development. With highly complex components, Inspectable let’s me finish property definition quicker, and I can spend less time messing with details in the FLA. Just today, I discovered something that speeds things up even more, so I thought I’d share.

I’ve always been a little annoyed that subclasses couldn’t redefine the default value for Inspectable getter/setters without redefining them. Often, I’ll have a complex setter function in a base class, and multiple subclasses inheriting from it. In some cases, a particular subclass might need a different default value for that property than the others. I have two options. Previously, I’ve manually set the different value in the FLA. Unfortunately, with a growing class tree with many classes needing alternate defaults, it’s becoming a hassle. Overriding the setter in the subclass can create redundant code. Even if I make an extra function that gets called from each setter, I feel like I’m building a hack that makes things more confusing.

Today, I took a break to experiment with Flash’s behaviors with getters/setters, and I discovered a more elegant method to redefine an alternate default value:

class SuperClass
{
   [Inspectable(defaultValue="Some General Default")]
   public function get myProperty():String
   {
      return this._myProperty;
   }

   public function set myProperty(newValue:String):Void
   {
      //here's a lot of code, and I'd rather not use any hacks
   }
}

class SubClass extends SuperClass
{
   [Inspectable(defaultValue="A Specialized Default")]
   public var myProperty:String;
}

That’s not an obvious solution, I’ll admit. You wouldn’t expect it to work, but the getter and setter defined in the super class are still called in the subclass even if myProperty is defined as a regular variable. I guess it’s still a hack, but it’s more elegant than the alternatives. Not only that, but it’s the simple solution I wanted to find in the first place. Normally, I’d make a warning that this probably won’t work with MTASC, but you wouldn’t be using Inspectable with MTASC anyway.

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. michelle

    if i place this in my docClass:

    [Inspectable(defaultValue=”A Specialized Default”)]
    public var myProperty:String;

    and this in my constructor:
    trace(myProperty);

    nothing happens, i am not so good in as3, do you how i can trace an component parameter. the livedocs are driving me crazy.

  2. Josh Tynjala

    Michelle, Inspectable is only used by components. In AS3, if you want to set a default value, you can just set it directly.

    public var myProperty:String = "A Specialized Default";
  3. Maverick

    GOAL:
    I’m using CS3 to build a Flash component. I have an Inspectable parameter who’s default value I want to load from a JSON file.

    PROBLEM:
    I want to set the defaultValue of the Inspectable component from a JSON file and have that value retained during runtime.

    What is the syntax to do that?

    [Inspectable(name=’title text’,type=’String’, defaultValue='{textFromJsonFile}’)] ?????
    public var titleText:String = ”;