【发布时间】:2014-10-20 06:59:50
【问题描述】:
我正在尝试使用设备运动仅在 x 轴上移动 SKSpiteNode。向左或向右倾斜设备以移动精灵。我正在阅读有关 CoreMotion 的信息,但我对如何在我的游戏中实现这一点存有疑问。CoreMotion 是否是正确的工具?以及使用哪些功能? 感谢您的任何帮助。
【问题讨论】:
标签: xcode swift sprite-kit core-motion
我正在尝试使用设备运动仅在 x 轴上移动 SKSpiteNode。向左或向右倾斜设备以移动精灵。我正在阅读有关 CoreMotion 的信息,但我对如何在我的游戏中实现这一点存有疑问。CoreMotion 是否是正确的工具?以及使用哪些功能? 感谢您的任何帮助。
【问题讨论】:
标签: xcode swift sprite-kit core-motion
您应该使用 CoreMotion 框架。以下是收听核心运动更新的方法。
1- 实例化CMMotionManager 实例。这个类负责传递运动事件。
lazy var motionManager: CMMotionManager = {
let motion = CMMotionManager()
motion.accelerometerUpdateInterval = 1.0/10.0 // means update every 1 / 10 second
return motion
}()
2- 调用运动管理器的startAccelerometerUpdates 方法。这将启动您的运动管理器。
self.motionManager.startAccelerometerUpdates()
3- 在SKScene 子类的update 方法中,您可以从运动管理器中读取加速度计值。这是我的使用方法。
let xForce = self.motionManager.accelerometerData.acceleration.x
self.playerNodeBody.velocity = CGVector(dx: xForce, dy: 0)
【讨论】: