无法从舞台中删除子级 - if(stage.contains(child1)){removeChild(child1);} 不起作用

发布于 2025-01-06 07:17:54 字数 990 浏览 0 评论 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 是一个 TextField,并且 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:错误 #1009:无法访问空对象引用的属性或方法。

当写入内部时:

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

什么也没有发生,并且新的子级被添加到前一个子级的顶部。

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

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烂柯人 2025-01-13 07:17:54

您需要输入 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.

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.
北音执念 2025-01-13 07:17:54

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

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

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);
}
安静 2025-01-13 07:17:54

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

因此包含意味着显示列表中的任何位置,而不仅仅是直接子项。你想要的是 Manque 指出的父级检查,或者只是从任何可能的地方删除结果:

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

虽然奇怪, addChild(result) 会自动将其从其前一个父级中删除 - DisplayObject 只能位于 DisplayList 中的一个位置一次,所以我不确定为什么你会看到多个结果...

是否有可能你传入的“结果”不是已经在舞台上的结果?

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?

浅听莫相离 2025-01-13 07:17:54

你尝试过吗?

MovieClip(root).removeChild(result)

[编辑]

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

   stage.addChild(result); 
}

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); 
}
入怼 2025-01-13 07:17:54

我所要做的只是更改 result.txt,甚至无需将其从舞台上删除

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文