gpt4 book ai didi

c++ - 销毁 Box2D Body - 读取访问冲突错误

转载 作者:行者123 更新时间:2023-11-28 05:20:58 25 4
gpt4 key购买 nike

我试图摧毁一个 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());
}
}

创建正文/ block 的代码:

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;
}

最佳答案

在您删除正文的代码中,我没有看到任何关于正文是否已被销毁的检查。进行这样的检查可以防止内存问题;可能像您描述的崩溃。

关于c++ - 销毁 Box2D Body - 读取访问冲突错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41512874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com