Reputation: 2796
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 :)
Upvotes: 2
Views: 1179
Reputation: 19164
Did you notice in the your Constructor::init
method, you create gate.body_door
using b2BodyDef gate.def_door
and attach to it b2Fixture that is created using gate.fixtureDef
? The only value the Constructor
object contribute the process is the world
object.
Look at it using this analogy:
You are making a cake. You have the flour, eggs, sugar, butter etc but you don't have the baking pan; your neighbour has it. Do you bring all your ingredients to your neighbour house and bake the cake there or do you just borrow the baking pan and bake it at your house? Which is the more logical option to take?
What I'm trying to convey is why do you need a method in Constructor
class to set up the body when you can just create a method in the Gate
class to do it? Like below:
@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];
}
See in the createBodyInWorld:andUserData:
method you don't have to worry about accessing the properties since you can directly access the ivars.
Upvotes: 2