Flash AS3 – Button on stage will not hide if I change the label – bug or missing something

buttoncomponentscs3flash

OK – weird problem which happens each time – and so I'm thinking I must just be missing something very obvious.

If in Flash CS3 I drag a Button component to the stage, and in the Document Class I hide that button with visible = false; – it works fine.
However, if I change the label of that Button from it's default 'Label' to anything else, the button does not go invisible…

eg. Button named hide_btn, Document Class Foo:

package {   
    import flash.display.MovieClip;

    public class Foo extends MovieClip{
        public function Foo(){
            hide_btn.visible = false; // Works ONLY if I didn't change the button label!!
        }
    }   
}

The Button is (correctly) invisible when I run the movie…
If I change the label to, say, 'LabelX' – then the Button is still there…

So surely someone would have noticed this before if it were a bug, right??
So – can someone explain what is going on?

Cheers.

PS. A trace of trace(hide_btn.visible) says false, even though it clearly isn't…

Best Answer

Funky one!

Didn't expect that :)

I had sort of a similar issue...well not quite. I was trying to access some clips in a later frame of a movieclip, just like in as2, but unless the playhead goes to that frame, in as3, the clips are null.

The workaround is to force the stage to invalidate and access the objects in the RENDER event ( which I think happens right before all the stuff gets rendered on stage, but after all your stuff is ready/accessible )

My guess is since you set the label in the Parameters tab, that gets evaluated after the Document class constructor, so there might be something in the Button Component invalidation that tells it to be visible. It's safer to set it invisible after all that is done.

Here is the updated code...famous last words: 'it works for me' :)

   package {   
    import flash.display.MovieClip;
    import flash.events.Event;
    public class Foo extends MovieClip{
        public function Foo(){
            //stage.invalidate() forces the stage to re-evaluate itself
            stage.invalidate();
            stage.addEventListener(Event.RENDER, stageRenderHandler);
        }
        //the RENDER events gets fired when invalidation is done
        //and everything is ready to be displayed/rendered
        private function stageRenderHandler(event:Event):void{
            hide_btn.visible = false; // Works
        }
    }   
}
Related Topic