1

我必须创建一个返回 b2Body 的类,我试图在 HelloWorldLayer.mm 文件中访问它。请参阅下面的圆圈类:

Circle.h

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "PhysicsSprite.h"

#define PTM_RATIO 32

@interface Circle : CCLayer   {

}

+(b2Body *)rCircle;

@end

和 Circle.mm

#import "Circle.h"

@implementation Circle


+(b2Body *)rCircle    {

//body and fixture defs for circleSprite
b2World *world_;
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
b2FixtureDef fixtureDef;
fixtureDef.density = 0.1;
b2PolygonShape polygonShape;
fixtureDef.shape = &polygonShape;

PhysicsSprite* circleBodySprite = [PhysicsSprite spriteWithFile:@"circleB.png"];
//[self addChild:circleBodySprite z:1];

//body with circle fixture
b2CircleShape circleShape;
circleShape.m_radius = circleBodySprite.contentSize.width/2 / PTM_RATIO;
fixtureDef.shape = &circleShape;
b2Body* chainBase = world_->CreateBody( &bodyDef );
chainBase->CreateFixture( &fixtureDef );
[circleBodySprite setPhysicsBody:chainBase];


return chainBase;

}


@end

我不断收到指向该行的 EXC_BAD_ACCESS 错误:

    b2Body* chainBase = world_->CreateBody( &bodyDef );

我不知道我做错了什么。我也一直b2Body在通过以下方式访问 HelloWorldLayer.mm :

[Circle rCircle];

这也给出了一个EXC_BAD_ACCESS错误。

我希望该类返回 a b2Body,以便我可以在 HelloWorldLayer.mm 中创建一个关节。

4

0 回答 0