Strangeness in custom component parameters

I decided today to systematically analyse a problem that’s bothered me for several years: What determines whether a parameter setter for a custom component will be called?

Today I’ve finally worked it out, and discovered that my lack of clarity was justified…

So I have a simple custom component with one Number parameter:

package  {

     import flash.display.MovieClip;
     import flash.events.Event;

     public class CustomComponent extends MovieClip {

          private var _aNumber:int;
          [Inspectable (type="Number")]
          public function get aNumber():int {return _aNumber;}
          public function set aNumber(value:int):void {
               trace("set number "+value);
               _aNumber = value;
          }

          public function CustomComponent() {
               loaderInfo.addEventListener(Event.INIT, onInit);
          }
          private function onInit(event:Event):void {
               trace("aNumber="+aNumber);
          }
     }
}

This gives the parameter aNumber:

Now when this parameter is set to ‘1’, and we test the movie, we see the following trace:

set number 1
aNumber=1

When this parameter is set to ‘0’ we see the following:

aNumber=0

So the INIT event is still dispatched, but the parameter setter is NOT called! Why? My hypothesis is that it’s not called as it is the same as the default value for the parameter. To test this hypothesis, I change the default for the variable:

          [Inspectable (type="Number",defaultValue=100)]

Now, with the parameter set to ‘0’ the trace is:

set number 0
aNumber=0

So obviously the default does affect whether the setter is called. It appears as though:

  • if the defaultValue and the parameter values are the same, the parameter setter is not called.
  • if the defaultValue and the parameter values are different, the parameter setter is called.

BUT – so far we’ve only looked at ONE parameter, what if there are several? this is where the plot gets thicker!

I’ve added two additional parameters to my custom component, a String, and a Boolean:

package  {

     import flash.display.MovieClip;
     import flash.events.Event;

     public class CustomComponent extends MovieClip {

          private var _aBoolean:Boolean;
          [Inspectable (type="Boolean")]
          public function get aBoolean():Boolean {return _aBoolean;}
          public function set aBoolean(value:Boolean):void {
               trace("set boolean "+value);
               _aBoolean = value;
          }

          private var _aNumber:int;
          [Inspectable (type="Number")]
          public function get aNumber():int {return _aNumber;}
          public function set aNumber(value:int):void {
               trace("set number "+value);
               _aNumber = value;
          }

          private var _aString:String;
          [Inspectable (type="String")]
          public function get aString():String {return _aString;}
          public function set aString(value:String):void {
               trace("set string "+value);
               _aString = value;
          }

          public function CustomComponent() {
               loaderInfo.addEventListener(Event.INIT, onInit);
          }
          private function onInit(event:Event):void {
               trace("aNumber="+aNumber+", aString="+aString+", aBoolean="+aBoolean);
          }
     }
}

Now we have three parameters:


If we leave the three parameters with their defaults, we only see the trace from the constructor:

aNumber=0, aString=null, aBoolean=false

But if we change just one parameter, in addition to the constructor trace, we see the trace from all three parameter setters:

set boolean true
set number 0
set string
aNumber=0, aString=, aBoolean=true

So, long story short, the custom component rule of strangeness breaks down to the following:

  • If none of the parameters are different to their defaults, none of the parameter setters run.
  • If JUST ONE of the parameters is different to the default, ALL of the parameter setters run.

strrrange…

Interactive developer, Author - iOS development with Swift - book coming 2017 https://manning.com/books/ios-development-with-swift

Tagged with: ,
Posted in Flash

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: