2011-04-23 23 views
2

,爲了方便起見並將對象組合在一起,我嘗試在單獨的類中創建我的box2d對象。
基本上,我有兩類:使用obj -c box2d和cocos2d添加到b2world,外部類的外部box2d物體

構造 - 的Box2D的世界裝載和OpenGL的抽籤存在這裏
門方法 - 具有多個對象,二精靈,一進門精靈和一個開關精靈一類的自己的邊界框等

目前,我有這樣的:

@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++,所以這有點令人望而生畏。
這是我的遊戲的架構非常重要,所以請幫助:)

+0

問題或問題是什麼? – Lukman 2011-04-23 11:18:00

回答

2

你在你的Constructor::init方法注意到,您在使用b2BodyDef gate.def_door創建gate.body_door和附加到它b2Fixture正在使用gate.fixtureDef產生的? 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:方法,你不必擔心訪問的屬性,因爲你可以直接訪問實例變量。

+0

啊,非常感謝你!我沒有意識到我可以將世界對象移動到另一個類的方法。我只是想到它在構建方法中被置於石頭之中。這將清理我的代碼這麼多!如果我有任何問題,我會明天實施併發布。 – Ospho 2011-04-23 12:42:25