Swift / iOS - Coremotion,手机方向垂直

时间:2015-04-03 13:07:41

标签: ios iphone swift sprite-kit core-motion

我正在尝试使用Coremotion来控制游戏中的精灵。

我已经能够使用Coremotion让精灵四处移动。问题是从Coremotion收到的x和y值是指电话处于平坦的水平位置(即平放在桌子上)。

当用户玩游戏时,手机将垂直握住,屏幕朝向用户。如果他们将设备倾斜远离它们,则精灵向前移动,如果他们将设备向他们倾斜,则精灵向后移动。如果他们向左或向右倾斜手机,精灵会向侧面移动。

如何修改我的代码,以便在屏幕面向用户的情况下从手机处于垂直位置引用动态数据?

if motionManager.deviceMotionAvailable {
    motionManager.deviceMotionUpdateInterval = 0.01
    motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue()){
        [weak self] (data: CMDeviceMotion!, error: NSError!) in

        var currentX = yLine.position.x
        var currentY = xLine.position.y

        // Translate yLine coordinates
        if data.gravity.x < 0 {
            destX = currentX + CGFloat(data.gravity.x * 1000)
        }else if data.gravity.x > 0 {
            destX = currentX + CGFloat(data.gravity.x * 1000)
        }

        // Translate xLine coordinates
        if data.gravity.y < 0 {
            destY = currentY + CGFloat(data.gravity.y * 1000)
        }else if data.gravity.x > 0 {
            destY = currentY + CGFloat(data.gravity.y * 1000)
        }

    }
}

0 个答案:

没有答案