OOP object creation question

Hi!

I have the following:

Code: Select all

Hero.h

Code: Select all

enum {STATE_STILL,STATE_MOVE_ACCELERATING,STATE_MOVE_BRAKING,STATE_AIM,STATE_STOP};

@interface Hero : NSObject {
    int state;
    b2Body *body;
    CCSprite *sprite;
    CGPoint speedVector;
    CGPoint accelerationVector;
    CGPoint lastPosition;
    CGPoint currentPosition;
    float maxSpeed;
    float frictionMultiplier;
    BOOL isMaxSpeedReached;
    BOOL isStopped;
    BOOL isDragging;
    float power;
}

@property (assign) int state;
@property (assign) b2Body *body;
@property (nonatomic,retain) CCSprite *sprite;
@property (assign) CGPoint speedVector;
@property (assign) CGPoint accelerationVector;
@property (assign) CGPoint lastPosition;
@property (assign) CGPoint currentPosition;
@property (assign) float maxSpeed;
@property (assign) float frictionMultiplier;
@property (assign) BOOL isMaxSpeedReached;
@property (assign) BOOL isStopped;
@property (assign) BOOL isDragging;
@property (assign) float power;

Code: Select all

Hero.m

Code: Select all

@implementation Hero

@synthesize state;
@synthesize body;
@synthesize sprite;
@synthesize speedVector,accelerationVector;
@synthesize lastPosition, currentPosition;
@synthesize maxSpeed;
@synthesize frictionMultiplier;
@synthesize isMaxSpeedReached,isStopped,isDragging;
@synthesize power;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        self.state = STATE_STILL;
        self.sprite = [CCSprite spriteWithFile:@"icon.png"];
        self.speedVector = CGPointZero;
        self.accelerationVector = CGPointZero;
        self.maxSpeed = 50;
        self.power = 1;
        self.isMaxSpeedReached = NO;
        self.isStopped = YES;
        self.frictionMultiplier = 1;
        self.isDragging = NO;
        // body creation
        b2BodyDef heroBodyDef;
        heroBodyDef.type = b2_dynamicBody;
        heroBodyDef.position.Set(160/PTM_RATIO,240/PTM_RATIO);
        heroBodyDef.userData = sprite; // ???
        // ???
    }
   
    return self;
}



And don't know how to write init method correctly so i just call call init and it adds body and everything else to the world?

Can't understand how to describe the Hero class to hold all data about Box2D. 🙁
I guess you will need to pass a reference to the world so this init function can use it to CreateBody.
I did it with

Code: Select all

- (id)initWithWorld:(b2World *)w


method and all things are inside my class. All i need now is to call this method inside game scene.

Thanks!

Original thread: https://box2d.org/forum/viewtopic.php?t=7717