安全地移除运行 Enterframe 的孩子

发布于 2024-12-11 01:40:22 字数 386 浏览 0 评论 0原文

每当我尝试删除运行 EnterFrame 的对象/子对象时,我总是会收到空引用错误。 在我的特定情况下,设置是《战地》包含很多机器人:

  1. 一个子(机器人)dispatchEvent,它被销毁
  2. 父容器接收该事件并开始通过removeChild删除子级,并从机器人数组中删除子级。
  3. 在 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:

  1. A child (Robot) dispatchEvent that it is destroyed
  2. The parent container receives the event and starts removing the child by removeChild and remove the child from an array of Robots.
  3. 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 技术交流群。

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

发布评论

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

评论(3

以可爱出名 2024-12-18 01:40:22

我通常做的两件事:

1)在更新的对象中有一个 active bool。当您调用 destroy() 函数或其他要清理的函数时,请将 active 设置为 false。在 update() 函数中,在开始时进行检查。如果activefalse,则退出。

2) 当您想要删除对象时,将 removeFromUpdate 属性设置为 true。在您的 UpdateManager(或在您的对象上调用 update() 的任何一个)中执行如下操作:

private function _update():void
{
    // update our objects
    var remove:Boolean = false;
    var len:int = this.m_objects.length;
    for( var i:int = 0; i < len; i++ )
    {
        this.m_objects[i].update();
        if( this.m_objects[i].removeFromUpdate )
            remove = true;
    }

    // if we don't have anything to remove, quit
    if( !remove )
        return;

    // we have something to remove, so traverse the array
    // backwards (so we don't have index problems)
    for( i = len - 1; i >= 0; i-- )
    {
        if( this.m_objects[i].removeFromUpdate )
        {
            // you can optionally call the object's destroy() function
            // here

            // splice the array
            this.m_objects.splice( i, 1 );
        }
    }
}

这对我来说非常有效。如果您想保存参数,则可以选择让对象的 update() 函数返回 true(如果应将其删除)

Two things I normally do:

1) Have an active bool in the object getting updated. When you call your destroy() function or whatever to cleanup, set active to false. In the update() function, make a check at the start. if active is false, 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 the update() on your objects) do something like this:

private function _update():void
{
    // update our objects
    var remove:Boolean = false;
    var len:int = this.m_objects.length;
    for( var i:int = 0; i < len; i++ )
    {
        this.m_objects[i].update();
        if( this.m_objects[i].removeFromUpdate )
            remove = true;
    }

    // if we don't have anything to remove, quit
    if( !remove )
        return;

    // we have something to remove, so traverse the array
    // backwards (so we don't have index problems)
    for( i = len - 1; i >= 0; i-- )
    {
        if( this.m_objects[i].removeFromUpdate )
        {
            // you can optionally call the object's destroy() function
            // here

            // splice the array
            this.m_objects.splice( i, 1 );
        }
    }
}

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

清晨说晚安 2024-12-18 01:40:22

好吧,对于 haXe 中的 Array 实际上有一个 remove 方法。您可以用它来永久删除机器人。该方法还会报告其成功,因此您的代码将类似于:

if (robotsArray.remove(explodedRobot)) 
    robotsLayer.removeChild(explodedRobot)

请注意,但是,在迭代数组时不应发生对 Array 进行删除的调用,否则您肯定会得到奇怪的行为。

Well, for one in haXe the Array actually has a remove-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:

if (robotsArray.remove(explodedRobot)) 
    robotsLayer.removeChild(explodedRobot)

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.

寄人书 2024-12-18 01:40:22

我还不能发表评论,所以必须这样做。

我可以想到两件事:
您说您从机器人列表中删除了孩子。因此,在列表中遇到空值似乎很奇怪;除非删除实现有错误?

是否有可能在 ENTER_FRAME 内通过机器人时,机器人实例被删除?如果是这样,从最后一个索引开始遍历列表可能会有所帮助。如果实例被删除,它不会更改索引较低的项目。

像这样的东西:

for(var index: int = list.length; index--; ) doSomething(list[index]);

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:

for(var index: int = list.length; index--; ) doSomething(list[index]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文