используя 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");
}
Поскольку это target-c ++, я не могу напрямую добавлять свойства к переменным внутри метода C. Например:
doorFixture = gate.body_door-> CreateFixture (& fixtureDef);
не может быть:
doorFixture = gate.body_door-> CreateFixture (& gate.fixtureDef);
Я не очень хорошо разбираюсь в C ++, так что это немного утомительно.
Это очень важно для архитектуры моей игры, поэтому, пожалуйста, помогите:)