0

这听起来可能很愚蠢,但我怎样才能从舞台上移除一个明确的孩子呢?例如

function giveMeResult(e:MouseEvent):void
{

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

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

它在前一个结果的顶部添加一个结果。

如果在舞台上,我希望函数“giveMeResult:删除”并添加一个新的。

更新:* 结果是一个文本字段,并且 result.txt ="" 不时更改...

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

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

什么时候

result.parent.removeChild(result);

编写时没有 if 语句 - 给出错误TypeError: Error #1009: Cannot access a property or method of an null object reference。

当写在里面时:

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

什么都没有发生,新的孩子被添加到前一个孩子的顶部。

谢谢大家!!!结果很简单 :) 我所要做的就是更改 result.txt ,甚至无需将其从舞台上删除 :)

4

5 回答 5

2

您需要键入stage.removeChild(result);,然后stage.addChild(result);

编辑:

查看与您类似的功能:

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

    stage.addChild(result);
}

将 TextField 的新实例添加result到舞台而不删除旧实例的唯一方法是结果已更改。看这个执行流程:

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.
于 2012-02-16T18:21:24.373 回答
1

如果我清楚地了解你想要什么,那么这段代码可以帮助你:

if (result.parent != null && result.parent == result.stage)
{
    // stage itself contains result
    result.parent.removeChild(result);
}
于 2012-02-16T18:52:40.443 回答
1

来自DisplayObjectContainer.contains()的文档:“孙子、曾孙等每个都返回 true。”

所以包含表示显示列表上的任何位置,而不仅仅是直接子级。您想要的是 Manque 指出的父检查,或者只是从可能的任何地方删除结果:

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

尽管奇怪的是, addChild(result) 会自动将其从其先前的父级中删除- DisplayObject 一次只能位于 DisplayList 中的一个位置,因此我不确定您为什么会看到多个结果...

难道你传递的“结果”不是已经在舞台上的结果吗?

于 2012-02-16T19:17:48.383 回答
0

你有没有尝试过?

MovieClip(root).removeChild(result)

[编辑]

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

   stage.addChild(result); 
}
于 2012-02-16T19:06:22.243 回答
0

我所要做的就是更改 result.txt 甚至不将其从舞台上删除

于 2012-02-17T10:41:18.050 回答