Visibility and parent clips

Sunday, April 27, 2008 By Malcolm

“Visible” works a little differently in as3. Seems that it disables button actions and turns the alpha to 0, but keeps the item as part of the display list. This means that old tricks of turning things visible = false then getting the parent size do not work correctly anymore. At first this was frustrating until I realized that removing an object from the display list is much more clear and effective.

Here’s a slightly different way of thinking:


package{

  import flash.display.*;

  public class SomeObject extends Sprite{

    public function SomeObject(){
      super();
    }

    /**
     * Will add and remove the child object from the sprite if needed.
     * @param  targetChild  The child to add or remove.
     * @param  isVisible  Add or remove it.  Default is true.
     */
    public function setChildVisibility(targetChild:DisplayObject,
             isVisible:Boolean = true):void{
      if(isVisible){
        if( !contains(targetChild) ){
          addChild(targetChild);
        }
      }else{
        if( contains(targetChild) ){
          removeChild(targetChild);
        }
      }
    }
  }
}

As usual a tad bit more code, but a lot clearer about what results we want. The hardest part is the transition in a production environment where all the old shortcuts just don’t work anymore.

Leave a Reply

About

The strongest, most experienced one-to-one marketing network in the world.

We believe in building relationships with customers that last a lifetime. And everything we do contributes to that end.

With 115 offices in 56 countries, OgilvyOne worldwide is the global leader in customer relationship management (CRM) and interactive marketing.

We know that staying profitable in today’s market is no easy feat. Businesses need to reach the customers that count and stay in regular contact—they need to better connect the customer with the brand and the brand with the customer.

ADDRESS

111 Sutter Street
San Francisco, CA 94010
hello@ogilvyonesf.com