我有一个名为block的类。在这个类中,我加载一个精灵,制作一个b2bodydef和一个fixturedef。 在另一个类中,我制作了2个块,将它们添加到矢量中,然后循环矢量以创建灯具:
Block *b = new Block(5, 20);
this->addChild(b->sprite);
blocks.push_back(b);
Block *b1 = new Block(15, 35);
this->addChild(b1->sprite);
blocks.push_back(b1);
b2Body *blockbody;
for(int i = 0; i<blocks.size();i++)
{
b2BodyDef def = blocks[i]->blockbodydef;
blockbody = world->CreateBody(&def);
if(blockbody == NULL)
cocos2d::CCLog("blockbody is null");
blockbody->CreateFixture(&blocks[i]->blockshapedef);
}
我在最后一行收到错误“exc_bad_access”。
这是我的block.h:
class Block
{
public:
cocos2d::CCSprite *sprite;
Block(float x, float y);
b2BodyDef blockbodydef;
b2FixtureDef blockshapedef;
};
和.cpp:
Block::Block(float x, float y)
{
sprite = cocos2d::CCSprite::create("Block.png");
sprite->setPosition(ccp(x,y));
blockbodydef.type = b2_kinematicBody;
blockbodydef.position.Set(x/PTM_RATIO,y/PTM_RATIO);
blockbodydef.userData = sprite;
b2PolygonShape blockPoly;
blockPoly.SetAsBox(37.5/PTM_RATIO , 37.5 / PTM_RATIO);
blockshapedef.shape = &blockPoly;
blockshapedef.density = 2.0f;
blockshapedef.friction = 0.2f;
blockshapedef.restitution = 0.8f;
}
blockbody不为null,也不是blockshapedef。有什么提示吗?
答案 0 :(得分:0)
解决了这个问题。从.cpp中删除b2PolygonShape blockPoly;
并将其添加到.h文件中就可以了。