PDA

View Full Version : Problems with Checking if an Element is on Stage?


ajwei810192
04-29-2009, 09:18 PM
Hi,

I have a code snippet here that I have in my application that I am trying to check to see if the clear button exists on the stage? (I think this is the term to use)


[Bindable]private var clear:Button;
private function clear_filter(event:FlexEvent):void{
box3.removeChild(clear);
}
private function clear_create():void{

if (clear) {
Alert.show("The button exist!");
}
else{
clear = new Button();
clear.label = "Clear Filter";
clear.addEventListener(FlexEvent.BUTTON_DOWN,clear _filter);
clear.y= 200;
box3.addChild(clear);
}
}

However, whenever I click on the button that activates the clear_filter function, the clear button disappears from the stage from the stage. I thought that this means that then when I call the clear_create() again with another button from the app, it would give me the clear button back. Instead, I get the Alert.show message pop up from what I have in the code where it should only appear if the clear button is on the stage.

What have I done wrong here?
Is this how I should check if an element exist on the stage?

Thanks for your help.

wvxvw
04-29-2009, 10:39 PM
When you remove DisplayObject from display list it becames invisible, but it is not removed from the memory, so, you'd do something like this:
[Bindable]
private var clear:Button;
private function clear_filter(event:FlexEvent):void
{
box3.removeChild(clear);
}
private function clear_create():void
{
if (clear && clear.stage)
{
// or, if you may add it to different containers:
// if (clear && clear.parent === box3)
Alert.show("The button is on the stage!");
}
else if (clear)
{
Alert.show("The button exists elsewhere!");
box3.addChild(clear);
}
else
{
Alert.show("The button has not been created yet!");
clear = new Button();
clear.label = "Clear Filter";
clear.addEventListener(FlexEvent.BUTTON_DOWN, clear_filter);
clear.y = 200;
box3.addChild(clear);
}
}

ajwei810192
04-30-2009, 01:37 PM
Hi,

I didn't realize that when it is not on the stage, it may still be in memory. Looks like everything is now working the way I wanted it to.

Thanks for the tip.