简体   繁体   English

box2d加速度计如何使其工作

[英]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. 我正在尝试使box2d加速度计正常工作,我有一个汽车精灵,想让它在iPhone倾斜时左右移动。

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? 如何实现加速度计左右工作?

Just do this... 只是做这个...

in your init add 在您的init中添加

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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM