使用加速度计时旋转设备

时间:2013-07-25 10:08:20

标签: iphone ios cocos2d-iphone uiinterfaceorientation

我在我的第一个Cocos2D游戏中使用加速计,它工作正常,我可以使用下面的代码移动精灵但是,当我将方向从landscapeLeft更改为landscapeRight时,精灵停止响应Y协调,精灵走到屏幕的顶部并没有正确回应...我相信这是因为改变了设备方向,但我不确定,因为我是App Development的新手,任何帮助将不胜感激

以下是我正在使用的示例代码......

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{    
    //this determines how sensitive the accelerometer reacts (higher = more sensitive)
    NSUserDefaults *defaulObj = [NSUserDefaults standardUserDefaults];
    float sensitivity = [defaulObj floatForKey:@"Sensitivity"]; //10.0f
    if (!sensitivity) {
        sensitivity = 6;
    }

    // this controls how quickly the velocity decelerates (lower = quicker to change direction)
    float deceleration = 0.4f;
    static float xVal = 0;

    if (!self.isFlag) {
        xVal = -acceleration.x;
        self.isFlag = TRUE;
    }

    playerVelocity.x = playerVelocity.x * deceleration + (xVal + acceleration.x) * sensitivity;
    playerVelocity.y = playerVelocity.y * deceleration + acceleration.y * sensitivity;
    playerVelocity.y = playerVelocity.y*-1;
}

1 个答案:

答案 0 :(得分:1)

也许这会有所帮助,在您当前的设备方向上翻转Y.

float rotatedY = acceleration.y;
if ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationLandscapeLeft) {
rotatedY *= -1;
}

然后使用 rotateY 加速内容。