销毁Box2D正文 - 读取访问冲突错误

时间:2017-01-06 19:26:12

标签: c++ box2d

我试图摧毁Box2D机身,但每次我试图摧毁它时,由于读取访问冲突错误,我的游戏崩溃了。

删除正文的代码:

if (event.mouseButton.button == sf::Mouse::Left) {
    if (((chunk.getChunkPosition().x + MAX_CHUNK_WIDTH * 16 > block.x * 16)
      && (block.x * 16 >= chunk.getChunkPosition().x))
      && ((chunk.getChunkPosition().y + MAX_CHUNK_HEIGHT * 16 > block.y * 16)
      && (block.y * 16 >= chunk.getChunkPosition().y)))
    {
        chunk.blockPhysic[block.x][block.y]->setCreatedState(false);
        world.getBWorld()->DestroyBody(chunk.blockPhysic[block.x][block.y]->getBody());
    }
}

创建正文/块的代码:

BlockPhysic::BlockPhysic(b2World* world, const sf::Vector2f& _position, const int& _textureKeyID, const sf::IntRect& _textRect, sf::Vector2f _size, bool _dynamicBody)
{
    b2BodyDef bBodyDef;
    dynamicBody = _dynamicBody;
    bBodyDef.type = _dynamicBody == true ? b2_dynamicBody : b2_staticBody;
    bBodyDef.position.Set(_position.x, _position.y);
    rBody = world->CreateBody(&bBodyDef);

    b2PolygonShape bShape;
    bShape.SetAsBox(_size.x / 2, _size.y / 2);
    b2FixtureDef bFixtureDef;
    bFixtureDef.shape = &bShape;
    bFixtureDef.density = 0.0f;
    bFixtureDef.friction = 0.0f;
    rFixture = rBody->CreateFixture(&bFixtureDef);

    sf::Texture *texture = &Global::txtManager.textures.at(_textureKeyID);
    rSprite.setTexture(*texture);
    rSprite.setOrigin(_size.x / 2, _size.y / 2);
    rSprite.setTextureRect(sf::IntRect(_textRect.left, _textRect.top, _textRect.width, _textRect.height));
    rSprite.setPosition(_position.x, _position.y);
    created = true;
}

1 个答案:

答案 0 :(得分:0)

在您删除身体的代码中,我没有看到身体是否已经被摧毁的任何检查。进行这样的检查可以防止内存问题;可能就像你描述的崩溃一样。