【问题标题】:Rotating device while using accelerometer使用加速度计时的旋转装置
【发布时间】:2013-07-25 10:08:19
【问题描述】:

我在我的第一个 Cocos2D 游戏中使用加速度计,它工作正常,我可以使用下面的代码移动精灵但是,当我将方向从 LandscapeLeft 更改为 LandscapeRight 时,精灵停止响应 Y 坐标,并且精灵进入屏幕顶部并且没有正确响应......我相信这是因为改变了设备方向,但我不确定,因为我对应用程序开发很陌生,任何帮助将不胜感激.

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

- (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;
}

【问题讨论】:

    标签: iphone ios cocos2d-iphone uiinterfaceorientation


    【解决方案1】:

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

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

    然后使用 rotatedY 代替Acceleration.y

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多