xcode - 在 swift 中使用 Core Motion

标签 xcode swift sprite-kit core-motion

<分区>

我正在尝试使用设备运动仅在 x 轴上移动 SKSpiteNode。向左或向右倾斜设备以移动 Sprite 。我正在阅读有关 CoreMotion 的信息,但我对如何在我的游戏中实现它有疑问。CoreMotion 是正确的工具吗?以及使用哪些功能? 感谢您提供的任何帮助。

最佳答案

您应该使用 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)

关于xcode - 在 swift 中使用 Core Motion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26460045/

相关文章:

ios - base64编码的图像并将其添加到UIImage

objective-c - 以编程方式创建 NSPopUpButton

arrays - 为什么 Array<Any>.flatMap() 将转换闭包参数包装在可选中?

swift SpriteKit : Use of unresolved identifier "player"?

ios - SpriteKit 手势问题

ios - 检测 SKSpriteNodes 上的触摸事件

xcode - 我该如何解决这个问题 : "this class is not key value coding-compliant for the key imageview"?

ios - Twitter 趋势 API 1.1 错误 "unrecognized selector sent to instance"

iOS: Realm 文件的大小呈指数级增长超过 GB

iOS Swift 3 通用自动布局