Visibility and parent clips
Sunday, April 27th, 2008“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 my 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.
It’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.