从外部类添加到b2world,外部box2d主体

时间:2011-04-23 07:48:44

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

使用obj-c box2d和cocos2d,我试图在一个单独的类中创建我的box2d对象,以方便起见并将对象组合在一起。
基本上,我有两个类:

构造函数 - 加载Box2d世界并且此处存在OpenGL绘制方法 Gate - 一个具有多个对象,两个精灵,一个门精灵和一个带有自己的边界框等的Switch精灵的类。

目前我有这个:

@interface Gate : CCNode {
@private
    //Door Gadget:
    b2Body *body_door;
    b2BodyDef def_door;
    b2FixtureDef fixtureDef;
    CGRect boundingBox_door;
    CCSprite *sprite_door;

}
-(void)createGate:(CGPoint)gateLoc:(CGPoint)switchLoc;
@property (assign) CGRect boundingBox_door;
@property (assign) CCSprite *sprite_door;
@property (assign) b2Body *body_door;
@property (assign) b2BodyDef def_door;
@property (assign) b2FixtureDef fixtureDef;


@end


@implementation Gate

-(void) createGate:(CGPoint)doorLoc:(CGPoint)switchLoc{
//please note that these variables are global and synthesized inside the header file

    sprite_door = [CCSprite spriteWithFile:@"eggBIG.png"];
    sprite_door.position = gateLoc;

    //Gadget 1 - Door Physics Body
    def_door.type = b2_dynamicBody;                 
    def_door.position.Set(gateLoc.x/PTM_RATIO,gateLoc.y/PTM_RATIO);
    def_door.userData = sprite_door;                

    b2PolygonShape dynamic_block1;              
    dynamic_block1.SetAsBox(15.0f/PTM_RATIO, 15.0f/PTM_RATIO);
    fixtureDef.shape = &dynamic_block1;
    fixtureDef.density = 3.0f;
    fixtureDef.friction = 1.0f;

}

构造函数类.mm -

@implementation Constructor
-(id) init{
//Create Gate
        Gate *gate = [[Gate alloc]init];
        [gate createGate:ccp(300,300) :ccp(400,400)];
        [self addChild:gate.sprite_door];
        b2BodyDef def_door = gate.def_door;
        gate.body_door = world->CreateBody(&def_door);
        b2Fixture *doorFixture;
        b2FixtureDef fixtureDef = gate.fixtureDef;
        doorFixture = gate.body_door->CreateFixture(&fixtureDef);
        doorFixture->SetUserData(@"sprite_door");
}

因为这是objective-c ++,所以我无法直接向C Method中的变量添加属性。例如:
doorFixture = gate.body_door-> CreateFixture(& fixtureDef); 不能是:
doorFixture = gate.body_door-> CreateFixture(& gate.fixtureDef);

我对C ++并不十分熟练,所以这有点令人生畏 这对我的游戏架构非常重要,所以请帮助:)。

1 个答案:

答案 0 :(得分:2)

您是否注意到在Constructor::init方法中,使用b2BodyDef gate.body_door创建gate.def_door并附加了使用gate.fixtureDef创建的b2Fixture? Constructor对象贡献该进程的唯一值是world对象。

使用这个类比来看待它:

  

你正在做蛋糕。你有面粉,鸡蛋,糖,   黄油等,但你没有   烤锅;你的邻居有它。做   你把所有的成分都带到了你的身上   邻居家和烤蛋糕   你或者只是借用烘焙   把它烤到你家里烤?哪一个   是更合乎逻辑的选择吗?

我想要传达的是为什么当你在Constructor类中创建一个方法时,你需要Gate类中的方法来设置主体?如下所示:

@implementation Gate

-(void) createBodyInWorld:(b2World*)world andUserData:(void*)userData {
        body_door = world->CreateBody(&def_door);
        b2Fixture *doorFixture = body_door->CreateFixture(&fixtureDef);
        doorFixture->SetUserData(userData);
}

...

@end

@implementation Constructor
-(id) init{
//Create Gate
        Gate *gate = [[Gate alloc]init];
        [gate createGate:ccp(300,300) withSwitchLoc:ccp(400,400)];
        [gate createBodyInWorld:world andUserData:@"sprite_door"];
        [self addChild:gate.sprite_door];
}

createBodyInWorld:andUserData:方法中,您不必担心访问属性,因为您可以直接访问ivars。