sahil
sahil

Reputation: 141

box2d accelerometer how to make it work

I'm trying to make the box2d accelerometer work, I have a car sprite and want it to move left and right, when the iPhone is tilted.

Here is the code for the sprite:

- (void)spawnCar {

car = [CCSprite spriteWithSpriteFrameName:@"car.jpg"];
car.position = ccp(160, 250);
car.tag = 2;

[self addBoxBodyForSprite:car];

[_spriteSheet addChild:car];

}

How can implement the accelerometer to work for left and right?

Upvotes: 1

Views: 627

Answers (1)

Jer In Chicago
Jer In Chicago

Reputation: 828

Just do this...

in your init add

self.isAccelerometerEnabled = YES;

and then add this method...

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    b2Vec2 gravity(-acceleration.y * 15, acceleration.x *15);
    world->SetGravity(gravity);

}

Upvotes: 1

Related Questions