如何在iphone / ipad中使用Accelerometer in Landscape

时间:2012-09-12 08:04:44

标签: iphone cocos2d-iphone box2d-iphone

我正在制作游戏,我希望将CCSprite移动到加速度计倾斜的方向。我已经参考了这个教程http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls,但我不清楚它。请任何人帮助我。

先谢谢。

1 个答案:

答案 0 :(得分:0)

参考此解决方案。我在景观游戏中用左右移动精灵:See answer here

仅横向模式的更改是使用加速度计y代替x。

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{   
    if(self.isGamePaused || self.isGameOver)
        return;

    float deceleration = 1.0f, sensitivity = 15.0f, maxVelocity = 450;

    if(sGame.IsIpad)
    {
        sensitivity = 14.0f;
        maxVelocity = 850;
        deceleration = 1.5f;
    }

    // adjust velocity based on current accelerometer acceleration
    //    vel.x = vel.x * deceleration + acceleration.x * sensitivity;

    AppController *app = (AppController*)[UIApplication sharedApplication].delegate;

    float velocity_x = self.gameActor.velocity.x ;

    self.gameActor.accelVal = ABS(acceleration.y);

    if(app.orient == UIInterfaceOrientationLandscapeLeft)
    {
        velocity_x = velocity_x * deceleration + acceleration.y * sensitivity;        
    }
    else 
    {
        velocity_x = velocity_x * deceleration + (-acceleration.y) * sensitivity;
    }

    //limit the maximum velocity of the player sprite, in both directions (positive & negative values)
    velocity_x = fmaxf(fminf(velocity_x, maxVelocity), -maxVelocity);

    self.gameActor.velocity  = ccp(velocity_x, self.gameActor.velocity.y);

}