use*_*898 4 runtime-error box2d cocos2d-x
我有这个代码,在行中给我运行时错误:
body->CreateFixture(&boxDef)
Run Code Online (Sandbox Code Playgroud)
我在windows中使用cocos2d-x 2.1.5和box2d 2.2.1
CCSprite *sprite = CCSprite::create(imageName.c_str());
this->addChild(sprite,1);
b2BodyDef bodyDef;
bodyDef.type = isStatic?b2_staticBody:b2_dynamicBody;
bodyDef.position.Set((position.x+sprite->getContentSize().width/2.0f)/PTM_RATIO,
(position.y+sprite->getContentSize().height/2.0f)/PTM_RATIO);
bodyDef.angle = CC_DEGREES_TO_RADIANS(rotation);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);
b2FixtureDef boxDef;
if (isCircle)
{
b2CircleShape circle;
circle.m_radius = sprite->getContentSize().width/2.0f/PTM_RATIO;
boxDef.shape = &circle;
}
else
{
b2PolygonShape box;
box.SetAsBox(sprite->getContentSize().width/2.0f/PTM_RATIO, sprite->getContentSize().height/2.0f/PTM_RATIO);
boxDef.shape = &box;
}
if (isEnemy)
{
boxDef.userData = (void*)1;
enemies->insert(body);
}
boxDef.density = 0.5f;
body->CreateFixture(&boxDef) //<-- HERE IS THE RUN TIME ERROR
Run Code Online (Sandbox Code Playgroud)
;
当我调试box2d代码即时到达方法中的b2Fixture.cpp:
void b2Fixture::Create(b2BlockAllocator* allocator, b2Body* body, const b2FixtureDef* def)
Run Code Online (Sandbox Code Playgroud)
在线:
m_shape = def->shape->Clone(allocator);
Run Code Online (Sandbox Code Playgroud)
得到运行时错误:
R6025纯虚函数调用
棘手的一个.我自己碰到了几次.它与变量范围有关.
boxDef.shape是问题所在.您可以在if/else块中将形状创建为局部变量,然后将它们分配给boxDef.一旦执行离开if/else块作用域,那么这些局部变量将是垃圾.boxDef.shape现在指向释放的内存.
解决方案是通过在if/else块之前移动圆形和框形状声明来保持形状变量的范围.
| 归档时间: |
|
| 查看次数: |
690 次 |
| 最近记录: |