我得到一个 EXC_BAD_ACCESS 从 b2fixture 访问 b2body。这可能是由于我缺乏 c++ 知识,因为我是一个混合 c++ 的 ObjC 开发人员(刚从 box2d 开始):
Myb2dUserData.h
@interface Myb2dUserData : NSObject {
MyContact *climbingHoldContact;
}
@property (nonatomic, assign) MyContact *climbingHoldContact;
Myb2dUserData.mm
b2Fixture *fixA = climbingHoldContact->fixtureA;
b2Body *bodA = fixA->GetBody(); //EXC_BAD_ACCESS
我在 b2ContactListener 中分配了 crawlHoldContact,这几乎是这段代码的副本: http ://www.raywenderlich.com/505/how-to-create-a-simple-breakout-game-with-box2d-and- cocos2d-tutorial-part-22
void MyContactListener::BeginContact(b2Contact* contact) {
MyContact myContact = { contact->GetFixtureA(), contact->GetFixtureB() };
_contacts.push_back(myContact);
b2Fixture *fixtureA = contact->GetFixtureA();
b2Body *bodyA = fixtureA->GetBody();
Myb2dUserData *userDataA = (Myb2dUserData *)bodyA->GetUserData();
bodyHoldUD.climbingHoldContact = &myContact;