创建一个返回b2Body问题的类

时间:2012-05-19 08:53:07

标签: iphone objective-c cocos2d-iphone physics box2d-iphone

我必须创建一个返回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 );

我不知道我做错了什么。我也一直以下列方式访问HelloWorldLayer.mm中的b2Body

[Circle rCircle];

这也会导致EXC_BAD_ACCESS错误。

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

0 个答案:

没有答案