,爲了方便起見並將對象組合在一起,我嘗試在單獨的類中創建我的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++,所以這有點令人望而生畏。
這是我的遊戲的架構非常重要,所以請幫助:)
問題或問題是什麼? – Lukman 2011-04-23 11:18:00