Can't remove child from the stage - if(stage.contains(child1)){removeChild(child1);} doesn't work

StackOverflow https://stackoverflow.com/questions/9316758

  •  26-10-2019
  •  | 
  •  

Question

It may sound stupid, but how can I remove a definite child from the stage? e.g.

function giveMeResult(e:MouseEvent):void
{

if(stage.contains(result))
    {removeChild(result);}

    addChild(result); // this part works fine, but it adds one over another
}

it adds one result on the top of the previous one.

I want the function "giveMeResult: to remove "result" if is on the stage and add a new one.

UPDATE:* result is a TextField, and result.txt ="" changes from time to time ...

trace (result.parent); /// gives  [object Stage]
trace (result.stage); /// gives[object Stage]

trace (result.parent != null && result.parent == result.stage); // gives true

when

result.parent.removeChild(result);

is written without if statement - gives error TypeError: Error #1009: Cannot access a property or method of a null object reference.

when written inside:

if (result.parent !=null && result.parent == result.stage)
{
result.parent.removeChild(result);
}

nothing happens and new child is added on the top of the previous one.

Thanks to all!!! The result is simple :) All I had to do is just change result.txt without even removing it from the stage :)

Was it helpful?

Solution 5

All I had to do is just change result.txt without even removing it from the stage

OTHER TIPS

You need to type stage.removeChild(result); and subsequently stage.addChild(result);

Edit:

Looking at a function similar to yours:

private function func(e:Event) : void {
    if(stage.contains(result)) {
        stage.removeChild(result);
    }

    stage.addChild(result);
}

The only way this would add a new instance of the TextField result to the stage, without removing the old one, would be if result has changed. See this flow of execution:

var result : TextField = new TextField();

// An event occurs and func get's called.
// now result will be added to stage.

result = new TextField();

// An event occurs and func get's called again
// This time the stage.contains(..) will return false, since the current result
// is not actually on stage. This will add a second TextField to the stage.

If I am clearly understood what you want, then this code can help you:

if (result.parent != null && result.parent == result.stage)
{
    // stage itself contains result
    result.parent.removeChild(result);
}

From the docs for DisplayObjectContainer.contains(): "Grandchildren, great-grandchildren, and so on each return true."

So contains means anywhere on the display list, not just direct children. What you want is the parent check Manque pointed out, or simply to remove result from anywhere it might be:

if (result.parent) {result.parent.removeChild(result); }

Though oddly, addChild(result) will automatically remove it from its previous parent - a DisplayObject can only be at one place in the DisplayList at a time, so I'm not sure why you're seeing multiple results...

Is it possible that the "result" you've passed in isn't the result that's already on the stage?

Have you tried?

MovieClip(root).removeChild(result)

[EDIT]

function giveMeResult(e:MouseEvent):void{
  if(result.parent != null && result.parent == result.stage){
    stage.removeChild(result);
  }

   stage.addChild(result); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top