花栗鼠:如何删除尸体?

发布于 2024-08-20 21:58:31 字数 338 浏览 6 评论 0原文

删除花栗鼠尸体的正确方法是什么?简单地调用 cpBodyFree 或 cpBodyDestroy 似乎不起作用,因为主体仍然显示在 cpSpaceEachBody 迭代中。

if(body->p.y < -260 || fabsf(body->p.x) > 340) {
    /* body is permanently off the screen    */
    /* so it needs to be permanently deleted */
    cpBodyFree(body);      ??
    cpBodyDestroy(body);   ??
}

What's the right way to delete a Chipmunk body? Simply calling cpBodyFree or cpBodyDestroy doesn't seem to work, as the body still shows up in the cpSpaceEachBody iteration.

if(body->p.y < -260 || fabsf(body->p.x) > 340) {
    /* body is permanently off the screen    */
    /* so it needs to be permanently deleted */
    cpBodyFree(body);      ??
    cpBodyDestroy(body);   ??
}

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

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

发布评论

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

评论(3

勿挽旧人 2024-08-27 21:58:31

删除主体的方法如下:

  1. 如果存在与主体关联的形状,请从空间中删除该形状并将其删除。
  2. 将尸体从空间中移出。 (这是我遗漏的部分。)
  3. 最后,如果不再需要空格,请将其删除。

以下是如何让 Plink 演示降下一阵五边形并在以下情况下清理它们:
他们离开了屏幕。

  1. 将此行添加到“//添加大量五边形”循环中。这样我们就可以释放附着在身体上的形状。

    body->data=shape;
    
  2. 从空间中移除形状和主体,然后释放形状和主体。事实并非如此
    如果您首先删除/释放形状或首先删除主体,似乎很重要,只要您
    请记住,当您释放主体时,您会丢失指向形状的指针。将 eachBody 函数更改为:

    if (body->py < -260 ) {
        cpSpaceRemoveShape(空间,主体->数据);
        cpSpaceRemoveBody(空间, 主体);
        cpShapeFree(主体->数据);
        cpBodyFree(身体);
    }
    

Here's how to delete a body:

  1. if there's a shape associated with the body, remove the shape from the space and delete it.
  2. remove the body from the space. (this is the part I was missing.)
  3. finally, delete the space if it is not needed anymore.

Here's how to make the Plink demo rain down a single shower of pentagons and clean them up when
they go off screen.

  1. Add this line to the "//Add lots of pentagons" loop. This is so we can free the shape attached to the body.

    body->data=shape;
    
  2. remove the shape and body from the space, then free the shape and body. It doesn't
    seem to matter if you remove/free the shape first or the body first, so long as you
    keep in mind that you lose the pointer to the shape when you free the body. Change the eachBody function to:

    if (body->p.y < -260 ) {
        cpSpaceRemoveShape(space, body->data);
        cpSpaceRemoveBody(space, body);
        cpShapeFree(body->data);
        cpBodyFree(body);
    }
    
寄意 2024-08-27 21:58:31

查看lib代码后,

void cpBodyDestroy(cpBody *body){}

void
cpBodyFree(cpBody *body)
{
    if(body){
        cpBodyDestroy(body);
        cpfree(body);
    }
}

调用cpBodyFree< /code> (它在内部调用 cpBodyDestroy。

更新:除非您不需要验证和 cpfree(body) 调用;)

After looking in the lib code

void cpBodyDestroy(cpBody *body){}

void
cpBodyFree(cpBody *body)
{
    if(body){
        cpBodyDestroy(body);
        cpfree(body);
    }
}

Call cpBodyFree (it calls internally cpBodyDestroy internally.

UPDATE: Except in cases where you don't need the validation and the cpfree(body) call ; )

红玫瑰 2024-08-27 21:58:31

您必须确保形状是否已添加为静态,可能此代码会有所帮助:

    if(shape != NULL)
    {
        int isStatic = 1;
        cpBody *bd = cpShapeGetBody(shape);
        if(bd != NULL)
        {
            if(!cpBodyIsRogue(bd) && !cpBodyIsStatic(bd)) //second condition is just to make sure
            {
                isStatic = 0;
                cpSpace *sp1 = cpBodyGetSpace(bd);
                if(sp1 != NULL)
                {
                    cpSpaceRemoveBody(sp1, bd); //remove body from space and then free it
                }
            }
            cpBodyFree(bd);
        }

        cpSpace *sp = cpShapeGetSpace(shape);
        if(sp != NULL)
        {
            if(isStatic)
                cpSpaceRemoveStaticShape(sp, shape);
            else
                cpSpaceRemoveShape(sp, shape); //remove shape from space and then free it
        }

        cpShapeFree(shape);
        shape = NULL;
    }

you have to make sure whether the shape has been added as static or not, may be this code will help a bit:

    if(shape != NULL)
    {
        int isStatic = 1;
        cpBody *bd = cpShapeGetBody(shape);
        if(bd != NULL)
        {
            if(!cpBodyIsRogue(bd) && !cpBodyIsStatic(bd)) //second condition is just to make sure
            {
                isStatic = 0;
                cpSpace *sp1 = cpBodyGetSpace(bd);
                if(sp1 != NULL)
                {
                    cpSpaceRemoveBody(sp1, bd); //remove body from space and then free it
                }
            }
            cpBodyFree(bd);
        }

        cpSpace *sp = cpShapeGetSpace(shape);
        if(sp != NULL)
        {
            if(isStatic)
                cpSpaceRemoveStaticShape(sp, shape);
            else
                cpSpaceRemoveShape(sp, shape); //remove shape from space and then free it
        }

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