Component/sub-movieclips initialization in AS2

Here’s a thing. You attach some MovieClip to the stage and try to style or set a components properties that resides within it, only to find your efforts are ignored. Imagine you have a library symbol containing a button, and with the following AS2 clas…

Here’s a thing. You attach some MovieClip to the stage and try to style or set a components properties that resides within it, only to find your efforts are ignored. Imagine you have a library symbol containing a button, and with the following AS2 class bound to it:

import mx.controls.*;

class App extends MovieClip
{
  var a_button:Button;

  public function App()
  {
    a_button.enabled = false;  // ignored
  }
}

The reason the setting of .enabled is ignored, according to EAS2 is that Flash works from the outside in, so a child movie can see it’s parent upon creation, but not the other way around. Now this doesn’t apply to intrinsic properties, such as _rotation or _alpha, just properties and methods you have attached to a MovieClip sub-class/component through a class.

There are a few ways around it. The most simple is perhaps the temporary onEnterFrame loop:

import mx.controls.*;

class App extends MovieClip
{
  var a_button:Button;

  public function App()
  {
    this.onEnterFrame = function()
    {
      a_button.enabled = false;
      this.onEnterFrame = undefined;
    }
  }
}

All I can say is… YUK! Colin Moock illustrates a much better method (note this isn’t the exact code for plagiarism reasons):

import mx.controls.*;

class App extends MovieClip
{
  var a_button:Button;

  public function App()
  {
    var initInt = setInterval(function (app:App):Void 
    {
      if (app.a_button.label != undefined) app.init();

    }, 10, this);
    
  }
  
  public function init():Void
  {
    a_button.enabled = false;
  }
}

Now that works, but I’m really weary of declaring a function within a method in AS2 (yes you could just Delegate it or just assign to another function in the App class) but I also dislike creating a setInterval simply to check for values. Thanks to Helen Triolo for pointing me towards chapter 13 in EAS2 in the first place (from which I came up with my own solution, which is merely a different method, it offers only aesthetical “improvements” with regards to personal taste:

import mx.controls.*;

class App extends MovieClip
{
  var a_button:Button;

  public function App()
  {
    a_button.watch("initializing", init, a_button); 
  }
  
  public function init(prop, oldVal, newVal, target):Void
  {
    target.enabled = false;
  }
}

The only thing I can see here, is that you can use the same “init” method for multiple components in the 2nd example. You can also use it for multiple types of components by incorporating “typeof” if statements, to provide for custom skinning of individual component types etc.

Perhaps the easiest method tho (spurred into action by Carl-Alexandre Malartre), is just to look out for the superclass’ (MovieClip’s) onLoad event:

import mx.controls.*;

class App extends MovieClip
{
  var a_button:Button;

  public function App()
  {
    this.onLoad = init; 
  }
  
  public function init():Void
  {
    a_button.enabled = false;
  }
}

So easily missed! You can also make your App extend UIObject and subscribe addEventListener) for a “load” event. Whichever you prefer.

If I’ve missed anything (I’m pretty tired) gimme an email!