0
我想仅使用设备动作在x轴上移动SKSpiteNode。将设备向左或向右倾斜以移动精灵。我正在阅读关于CoreMotion的内容,但是我对如何在我的游戏中实现这一点怀疑。是否CoreMotion是正确的工具?以及使用哪些功能? 谢谢任何帮助表示赞赏。使用Core Motion with swift
我想仅使用设备动作在x轴上移动SKSpiteNode。将设备向左或向右倾斜以移动精灵。我正在阅读关于CoreMotion的内容,但是我对如何在我的游戏中实现这一点怀疑。是否CoreMotion是正确的工具?以及使用哪些功能? 谢谢任何帮助表示赞赏。使用Core Motion with swift
您应该使用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)