无法从舞台中删除子级 - if(stage.contains(child1)){removeChild(child1);} 不起作用
这听起来可能很愚蠢,但我怎样才能把一个特定的孩子从舞台上移走呢? 例如,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要输入
stage.removeChild(result);
,然后输入stage.addChild(result);
编辑:
查看与您类似的函数:
这是添加的唯一方法如果结果发生更改,则将 TextField
result
的新实例添加到舞台,而不删除旧实例。看一下这个执行流程:You need to type
stage.removeChild(result);
and subsequentlystage.addChild(result);
Edit:
Looking at a function similar to yours:
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:如果我清楚地了解你想要什么,那么这段代码可以帮助你:
If I am clearly understood what you want, then this code can help you:
来自 DisplayObjectContainer 的文档.contains():“孙子、曾孙等等都返回 true。”
因此包含意味着显示列表中的任何位置,而不仅仅是直接子项。你想要的是 Manque 指出的父级检查,或者只是从任何可能的地方删除结果:
虽然奇怪, 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:
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?
[EDIT]
我所要做的只是更改 result.txt,甚至无需将其从舞台上删除
All I had to do is just change result.txt without even removing it from the stage