using obj-c box2d and cocos2d, im attempting to create my box2d objects in a separate Class, for convenience sake and to group objects together.
Basically, I have two Classes:
Constructor - The Box2d world is loaded and the OpenGL draw methods exist here
Gate - A Class that has multiple objects, two sprites, a Door sprite and a Switch sprite with their own bounding boxes etc.
At the moment I have this:
@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;
}
Constructor Class .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");
}
Because this is objective-c++, i cannot directly add properties to the variables inside C Method. For example:
doorFixture = gate.body_door->CreateFixture(&fixtureDef);
cannot be:
doorFixture = gate.body_door->CreateFixture(&gate.fixtureDef);
I'm not very proficient with C++, so this is a little daunting.
This is quite important for the architecture of my game, so please help :)