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)