2014-01-08 41 views
0

我有一個叫做block的類。在這個類中,我加載一個精靈,製作一個b2bodydef和一個fixturedef。 在另一類我做2個街區,將它們添加到載體中,然後環矢量創建燈具:createfixture上的exc_bad_access

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不爲空,也不是blockshapedef。有小費嗎?

回答

0

解決了這個問題。從.cpp中刪除b2PolygonShape blockPoly;並將其添加到.h文件中。