安全地移除运行 Enterframe 的孩子
每当我尝试删除运行 EnterFrame 的对象/子对象时,我总是会收到空引用错误。 在我的特定情况下,设置是《战地》包含很多机器人:
- 一个子(机器人)dispatchEvent,它被销毁
- 父容器接收该事件并开始通过removeChild删除子级,并从机器人数组中删除子级。
- 在 Enterframe 上,在移动机器人的循环期间,有时我会得到空引用,所以我必须调用 if (robots[i] == null) continue;
如果我的 Enterframe 上的机器人都为空,如何安全地移走孩子而不洒水?
我的一个想法是在输入框内建立一个要删除的机器人列表,检查是否有要删除的机器人,如果有,则在那里进行删除,而不是对机器人爆炸事件进行回调。
Whenever I try to remove an object/child with enterframe running I always get null reference error.
In my particular case, the setup is Battlefield contains a lot of Robot:
- A child (Robot) dispatchEvent that it is destroyed
- The parent container receives the event and starts removing the child by removeChild and remove the child from an array of Robots.
- on enterframe, during a loop to move the robots around, sometimes I would get null reference, so I have to call if (robots[i] == null) continue;
How do you safely remove the child without sprinkling if robot is null all over my enterframe?
one idea I have is to have a list of robots to be removed inside the enterframe that checks whether there is a robot to be removed, and if there is, do the removal there instead of the callback on robot exploded event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常做的两件事:
1)在更新的对象中有一个
active
bool。当您调用destroy()
函数或其他要清理的函数时,请将active
设置为false
。在update()
函数中,在开始时进行检查。如果active
为false
,则退出。2) 当您想要删除对象时,将
removeFromUpdate
属性设置为 true。在您的 UpdateManager(或在您的对象上调用update()
的任何一个)中执行如下操作:这对我来说非常有效。如果您想保存参数,则可以选择让对象的
update()
函数返回 true(如果应将其删除)Two things I normally do:
1) Have an
active
bool in the object getting updated. When you call yourdestroy()
function or whatever to cleanup, setactive
tofalse
. In theupdate()
function, make a check at the start. ifactive
isfalse
, quit out.2) Have a
removeFromUpdate
property that's set to true when you want to delete your object. In your UpdateManager (or whichever calls theupdate()
on your objects) do something like this:That pretty much works for me. You can optionally have the object's
update()
function return true if it should be removed if you want to save on a parameter好吧,对于 haXe 中的
Array
实际上有一个remove
方法。您可以用它来永久删除机器人。该方法还会报告其成功,因此您的代码将类似于:请注意,但是,在迭代数组时不应发生对
Array
进行删除的调用,否则您肯定会得到奇怪的行为。Well, for one in haXe the
Array
actually has aremove
-method. You can use that to remove the robots for good. The method also reports of its success, so your code would look something like:Please note however, the call to remove on the
Array
should not occur while it is being iterated over, or you most certainly will get weird behavior.我还不能发表评论,所以必须这样做。
我可以想到两件事:
您说您从机器人列表中删除了孩子。因此,在列表中遇到空值似乎很奇怪;除非删除实现有错误?
是否有可能在 ENTER_FRAME 内通过机器人时,机器人实例被删除?如果是这样,从最后一个索引开始遍历列表可能会有所帮助。如果实例被删除,它不会更改索引较低的项目。
像这样的东西:
I can not post comments yet, so have to do it this way.
Two things I can think off:
You say you remove the child from Robots list. So it seems strange that you encounter null values in the list; unless the remove implementation has a mistake?
Is it possible that inside the ENTER_FRAME while going trough the robots, robot instances get removed? If so, it might help if you go trough the list starting at the last index. If an instance gets removed, it will not change the items at lower index.
Something like: